OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.69k stars 6.55k forks source link

[REQ] [go-gin-server] Add featureCORS option to enable CORS middleware #11186

Open kumo-rn5s opened 2 years ago

kumo-rn5s commented 2 years ago

Is your feature request related to a problem? Please describe.

Go-Gin-Server supporting file routers.go start a router without any cors configurations.

router := gin.Default()
for _, route := range routes {
  switch route.Method {
  case http.MethodGet:
      router.GET(route.Pattern, route.HandlerFunc)
  case http.MethodPost:
      router.POST(route.Pattern, route.HandlerFunc)
  case http.MethodPut:
      router.PUT(route.Pattern, route.HandlerFunc)
  case http.MethodPatch:
      router.PATCH(route.Pattern, route.HandlerFunc)
  case http.MethodDelete:
      router.DELETE(route.Pattern, route.HandlerFunc)
  }
}

Describe the solution you'd like

Like Go-Server featureCORS option, cors configurations are added to router before route paths are holding, similar processing is required by Go-Gin-Server.

https://github.com/gin-contrib/cors

 router := gin.Default()
  router.Use(cors.New(cors.Config{
    AllowOrigins:     []string{"https://foo.com"},
    AllowMethods:     []string{"PUT", "PATCH"},
    AllowHeaders:     []string{"Origin"},
    ExposeHeaders:    []string{"Content-Length"},
    AllowCredentials: true,
    AllowOriginFunc: func(origin string) bool {
      return origin == "https://github.com"
    },
    MaxAge: 12 * time.Hour,
  }))
  router.Run()
wing328 commented 2 years ago

@FirosStuart thanks for the suggestion. Can you please file a PR with the suggested enhancement?

kumo-rn5s commented 2 years ago

Ok, I'll try that. I don't have experience contributing to this repository though, this might take some time.