Code Monkey home page Code Monkey logo

swagger's Introduction

Swagger for the Iris web framework

Iris middleware to automatically generate RESTful API documentation with Swagger 2.0 as requested at #1231.

build status Go Report Card

Usage

Start using it

  1. Add comments to your API source code, See Declarative Comments Format.
  2. Download Swag for Go by using:
$ go install github.com/swaggo/swag/cmd/swag@latest

# if you find swag cli not work, you can try to install swag cli from source
git clone [email protected]:swaggo/swag.git
cd swag
# tag variable should match with github.com/swaggo/swag in go.mod
# here we use v1.8.10
git checkout -b ${tag} tags/${tag}
go install ./cmd/swag
  1. Run the Swag in your Go project root folder which contains main.go file, Swag will parse comments and generate required files(docs folder and docs/doc.go).
$ swag init
  1. Download swagger for Iris by using:
$ go get github.com/iris-contrib/swagger/v12@master

And import following in your code:

import "github.com/iris-contrib/swagger" // swagger middleware for Iris 
import "github.com/iris-contrib/swagger/swaggerFiles" // swagger embed files

Example Code:

package main

import (
    "github.com/kataras/iris/v12"

    "github.com/iris-contrib/swagger"
    "github.com/iris-contrib/swagger/swaggerFiles"

    _ "github.com/your_username/your_project/docs"
    // docs folder should be generated by Swag CLI (swag init),
    // you have to import it.
)

// @title Swagger Example API
// @version 1.0
// @description This is a sample server Petstore server.
// @termsOfService http://swagger.io/terms/

// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email [email protected]

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @host localhost:8080
// @BasePath /v2
func main() {
    app := iris.New()

    swaggerUI := swagger.Handler(swaggerFiles.Handler,
		swagger.URL("/swagger/doc.json"),
		swagger.DeepLinking(true),
		swagger.Prefix("/swagger"),
	)

    // Register on http://localhost:8080/swagger
    app.Get("/swagger", swaggerUI)
    // And the wildcard one for index.html, *.js, *.css and e.t.c.
    app.Get("/swagger/{any:path}", swaggerUI)

    app.Listen(":8080")
}
  1. Run it, and navigate through http://localhost:8080/swagger/index.html, you should see the Swagger 2.0 API documentation page.

  2. If you want to disable swagger when some environment variable is set, use DisablingHandler instead of Handler.

swagger.DisablingHandler(swaggerFiles.Handler, "THE_OS_VARIABLE_NAME_HERE", configurators ...Configurator)
  1. If you want to change swagger-ui theme, you can add swagger.SetTheme(swagger.Monokai) when init swaggerUI
swaggerUI := swagger.Handler(swaggerFiles.Handler,
    swagger.URL("/swagger/doc.json"),
    swagger.DeepLinking(true),
    swagger.Prefix("/swagger"),
    // ref: https://github.com/ostranme/swagger-ui-themes
    // current we support 7 themes
    // theme is a optional config, if you not set, it will use default theme
    swagger.SetTheme(swagger.Monokai),
)

swagger's People

Contributors

dependabot[bot] avatar fushengliangnian avatar kataras avatar warjiang avatar xieziheng1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

swagger's Issues

Can anyone please tell me what am I doing wrong here ?

image
image

The rest of the main.go file

package main

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/middleware/recover"

	"github.com/iris-contrib/swagger/v12"              // swagger middleware for Iris
	"github.com/iris-contrib/swagger/v12/swaggerFiles" // swagger embed files

	"go.api.backend/api/endpoints"
	_ "go.api.backend/docs"
)

// @title Swagger Example API
// @version 1.0
// @description This is a sample server Petstore server.
// @termsOfService http://swagger.io/terms/

// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email [email protected]

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @host localhost:8080
// #@BasePath /v2
func main() {
	app := iris.New()

	// Built-in
	app.UseRouter(recover.New()) //Recovery middleware recovers from any panics and writes a 500 if there was one.

	endpoints.BookRegister(app)

	//region ======== SWAGGER REGISTRATIONS =================================================

v11 can not work

go get github.com/iris-contrib/swagger: no matching versions for query "upgrade"

How to configure swagger comments with iris using mvc

....
	mvc.Configure(app.Party("/api/v1"), func(m *mvc.Application) {
		m.Party("/test").Handle(new(v1.Test))
		m.Party("/xiaomai").Handle(new(v1.XiaoMai))
	})
.....
type XiaoMai  struct {
	Ctx iris.Context
}

// @Summary
// @Produce json
// @Param text string true "text"
// @Success 200 {object} restful.JsonResult
// @Failure 500 {object} restful.JsonResult
// @Router /api/v1/tiktok/parse/video
func (t *XiaoMai) PostParseVideo() *restful.JsonResult {
......
......
}

the questions about api operation .

When i set api operations->params type-> array . The Ui notice me
Could not render this component, see the console. ? So, it's means params type is not support array?.

Request Method is OPTIONS when Post Json

When I use the POST method to accept the JSON parameter, click the Try it out button, and swagger sends an OPTIONS type request.

here is my code:

// @Accept  json
// @Param req body bean.Dog true "InstanceCreateReq"
// @Success 200 {string} string	"ok"
// @Router /demo/dog [post]

the request by swagger try is:

Request URL: http://127.0.0.1:8888/demo/dog
Request Method: OPTIONS
Status Code: 404 Not Found
Remote Address: 127.0.0.1:8888
Referrer Policy: no-referrer-when-downgrade

The module value in the go.mod is wrong

In the release: v12.2.0-alpha2 , the value in the go.mod is module github.com/iris-contrib/swagger

should it be module github.com/iris-contrib/swagger/v12 ?

I can not download it in my project with go mod with error:

go: github.com/iris-contrib/swagger/[email protected]: go.mod has non-.../v12 module path "github.com/iris-contrib/swagger" (and .../v12/go.mod does not exist) at revision v12.2.0-alpha2

Endpoint grouping/collection

How can I group the endpoints by Party ? Which declaration/annotation comment should I use ? I'm having this:

image

I need the endpoints grouped by something like "Books", "Catalogs" and so on,... not one unique unnamed "default" group. How can I achieve that?

PS: My problem is similar to this link but with Iris

How to use swagger with different instance names

Describe the bug
I'm generating swagger documentation by running swag init --instanceName auth

To Reproduce
Steps to reproduce the behavior:

  1. Create a simple go project, using iris
  2. Create a swagger folder in your project root
  3. Generate multiple swagger docs using different instancenames. For example:
cd <Project-Root>/swagger
swag init -dir ../cmd/myproject --instanceName main
swag init -dir ../pkg/auth --instanceName auth
swag init -dir ../pkg/anotherPackage --instanceName anotherPackage
  1. In your main.go, one should be able to register multiple instance names against differnet url paths. E.g. ginSwagger does it this way:
router.GET("/swagger/main/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.InstanceName("main")))
router.GET("/swagger/auth/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.InstanceName("auth")))
router.GET("/swagger/anotherPackage/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.InstanceName("anotherPackage")))

For more details, please refer to this:
https://myschools.me/suguo/gin-swagger/src/commit/88c9ed2643de9f3b9fd2ac9ad03d9e63bd7721d3/example/multiple/main.go

Expected behavior
I should be able to define and use different instance names as I need to consolidate and generate swagger documentation for all of my packages at one place.

Screenshots
Not applicable

Desktop (please complete the following information):

  • OS: MAC

iris.Version

  • github.com/iris-contrib/swagger/v12 v12.2.0-alpha
  • github.com/kataras/iris/v12 v12.2.0-beta7.0.20230219194850-dccd57263617
  • github.com/swaggo/swag v1.8.10

[FEATURE REQUEST] Custom oauth2RedirectUrl

Is your feature request related to a problem? Please describe.
My project is running locally with port 44000 but Swagger always redirects to localhost:3200. I want to change oauth2RedirectUrl so it can redirect properly.

Describe the solution you'd like

swaggerUI := swagger.WrapHandler(swaggerFiles.Handler, swagger.Config{
  Oauth2RedirectUrl: "http://localhost:44000",
})

Cannot get latest version (v12.2.0)

Describe the bug
The latest version (v12.2.0 at the moment) is effectivly un-gettable.

To Reproduce
Steps to reproduce the behavior:

  1. go get github.com/iris-contrib/swagger/v12@master
    We get the following line in go.mod:
    require github.com/iris-contrib/swagger/v12 v12.2.0-alpha //wrong version

  2. go get github.com/iris-contrib/swagger/[email protected]
    We get this error:
    go: github.com/iris-contrib/[email protected]: invalid version: module contains a go.mod file, so module path must match major version ("github.com/iris-contrib/swagger/v12")

Expected behavior
We should get the correct version in go.mod:
require github.com/iris-contrib/swagger/v12 v12.2.0

Desktop (please complete the following information):

  • OS: all majior [linux, windows, osx]

iris.Version

  • v12.2.1

Additional context
The problem is due to the fact that, in the master branch, the module declaration in go.mod lacks the v12 suffix.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.