swaggo / gin-swagger

gin middleware to automatically generate RESTful API documentation with Swagger 2.0.
MIT License
3.66k stars 266 forks source link

Swagger throwing "indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: 2.0" issue #266

Open mohanprasathsj opened 1 year ago

mohanprasathsj commented 1 year ago

I a using the following code, when I did swag init and tried to view the swagger-ui, I am getting the following exception "Please indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: "2.0" and those that match openapi: 3.0.n (for example, openapi: 3.0.0)." image

/ @title     Car Track Service 2.0 API
// @version         2.0
// @description     Car Track Service in Go using Gin framework.

// @contact.name  *****
// @contact.email  *******

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

// @BasePath  /api
func main() {
fmt.Println("Starting...")
date := strconv.Itoa(time.Now().Year()) + fmt.Sprintf("%02d", time.Now().Month()) + fmt.Sprintf("%02d", time.Now().Day()) + strconv.Itoa(time.Now().Hour()) + fmt.Sprintf("%02d", time.Now().Minute())
docs.SwaggerInfo.Version = "Ver: 1.0, date: " + date
docs.SwaggerInfo.Host = cfg.Api_Url
ginServ := server.New(cfg, loggr)
api := ginServ.Group("/api")
controllers.AddCarTrackController(api)
ginServ.GET("/swagger/*any", FixRequestUri)
srv := &http.Server{
    Addr:    ":" + strconv.Itoa(cfg.Port),
   Handler: ginServ,
}
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
   loggr.Fatal("Cannot Start the HTTP Listener", zap.Error(err))
}

I tried to do update all versions but I could not resolve the issue. Has anyone got such issue and how can I fix this issue? Generated file seems to be ok.

image

worapolburaphan commented 1 year ago

I also encountered this problem. However, I don't know how it happened. It didn't occur right from the start because it was working fine initially. As I kept writing code continuously without dealing with Swagger, it just happened on its own.

worapolburaphan commented 1 year ago

@mohanprasathsj I have solved it. Just debug your code to find the error that caused the issue with gin. It could possibly be a middleware problem.

Etavioxy commented 1 month ago

inspect the network response by F12