swaggo / swag

Automatically generate RESTful API documentation with Swagger 2.0 for Go.
MIT License
10.71k stars 1.2k forks source link

Security annotations cannot work #1336

Open w570955342 opened 2 years ago

w570955342 commented 2 years ago

Describe the bug // @title 网络检查服务 // @description 网络检查服务接口文档 // @securityDefinitions.apikey ApiKeyAuth // @in header // @name Authorization // @schemes http https // @BasePath /check // @contact.name zq // @contact.email z397435052@gmail.com func main() {}

swag v1.8.0 doc.go: // SwaggerInfo holds exported Swagger Info so clients can modify it var SwaggerInfo = &swag.Spec{ Version: "", Host: "", BasePath: "/check", Schemes: []string{"http", "https"}, Title: "网络检查服务", Description: "网络检查服务接口文档", InfoInstanceName: "swagger", SwaggerTemplate: docTemplate, }

swag v1.8.6 doc.go: // SwaggerInfo holds exported Swagger Info so clients can modify it var SwaggerInfo = &swag.Spec{ Version: "", Host: "", BasePath: "", Schemes: []string{}, Title: "网络检查服务", Description: "网络检查服务接口文档", InfoInstanceName: "swagger", SwaggerTemplate: docTemplate, }

but when I chage main.go,just add a blank line , v1.8.6 works well。 // @title 网络检查服务 // @description 网络检查服务接口文档 // @securityDefinitions.apikey ApiKeyAuth // @in header // @name Authorization

// @schemes http https // @BasePath /check // @contact.name zq // @contact.email z397435052@gmail.com func main() {}

swag v1.8.6 doc.go: // SwaggerInfo holds exported Swagger Info so clients can modify it var SwaggerInfo = &swag.Spec{ Version: "", Host: "", BasePath: "/check", Schemes: []string{"http", "https"}, Title: "网络检查服务", Description: "网络检查服务接口文档", InfoInstanceName: "swagger", SwaggerTemplate: docTemplate, }

To Reproduce Steps to reproduce the behavior:

  1. go install github.com/swaggo/swag/cmd/swag@v1.8.6
  2. swag init --generalInfo ./cmd/check/main.go --output ./internal/app/swagger
  3. go install github.com/swaggo/swag/cmd/swag@v1.8.0
  4. swag init --generalInfo ./cmd/check/main.go --output ./internal/app/swagger

Expected behavior swag v1.8.6 can work well.

Your swag version 1.8.0 and 1.8.6

Your go version go version go1.18.3 windows/amd64

Desktop (please complete the following information):

maljb commented 2 years ago
// @securitydefinitions.apikey ApiKeyAuth
// @in                         header
// @name                       Authorization

securityDefinitions -> securitydefinitions

does works for me!!!!

by-cx commented 6 months ago

Moving securitydefinitions annotations above main() function helped me.