英文:
Echo Groups not working with OpenAPI generated code using oapi-codegen
问题
我正在使用oapi-codegen生成我的服务器代码,并使用Echo Labstack作为服务器。
当我将Group
实例传递给Openapi.RegisterHandlers
而不是Echo
实例时,无论在该组中的任何请求,我都会始终收到一个400错误,错误消息为{"message":"no matching operation was found"}
:
swagger, err := Openapi.GetSwagger()
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err)
os.Exit(1)
}
// 使用oapi验证中间件来检查所有请求是否符合OpenAPI模式。
g := e.Group("/api", middleware.OapiRequestValidator(swagger))
Openapi.RegisterHandlers(g, &MyApi{})
如果发送请求/api/foo
,其中foo
是生成的服务器代码中定义的API端点,我会收到一个400错误。如果我发送一个未定义的API端点,例如/api/<some undefined api>
,我也会收到400错误。如果我发送一个/baz
的请求,我会得到预期的404错误,因为那不是一个定义的路由。如果我在Group()
中不传递前缀,每个请求都会收到400错误。如果我使用RegisterHandlersWithBaseURL()
也会得到相同的行为。
英文:
I am using oapi-codegen to generate my server code and Echo Labstack as the server.
When I pass a Group
instance to Openapi.RegisterHandlers
instead of an Echo
instance, I always get a 400 error with {"message":"no matching operation was found"}
for any request in that group:
swagger, err := Openapi.GetSwagger()
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err)
os.Exit(1)
}
// Use oapi validation middleware to check all requests against the
// OpenAPI schema.
g := e.Group("/api", middleware.OapiRequestValidator(swagger))
Openapi.RegisterHandlers(g, &MyApi{})
If send request /api/foo
, where foo
is an API endpoint defined in the generated server code, I get a 400 error. If I do /api/<some undefined api>
I also get 400. If I do send a request for /baz
, I get 404 as expected, since that isn't a defined route. If I don't pass a prefix to Group()
, I get a 400 error for every request. I get the same behavior if I use RegisterHandlersWithBaseURL()
答案1
得分: 1
似乎存在一个错误,如果你在Group()
函数或RegisterHandlersWithBaseURL()
函数中指定了基本路径,OapiRequestValidator
中间件在检查请求路径与路由时会忽略基本路径。它使用在OpenAPI规范中定义的路由,而不包括基本路径。为了解决这个问题,我重写了inline.tmpl
模板,并修改了GetSwagger()
函数,在底部添加了以下内容:
func GetSwagger(pathPrefix string) (swagger *openapi3.T, err error) {
...
var updatedPaths openapi3.Paths = make(openapi3.Paths)
for key, value := range(swagger.Paths) {
updatedPaths[pathPrefix + key] = value
}
swagger.Paths = updatedPaths
}
Path
映射中的键是路由。我只是在每个键后面添加了基本路径。
英文:
There seems to be a bug where if you specify the a base path, either to the Group()
function or to RegisterHandlersWithBaseURL()
, theOapiRequestValidator
middle ignores the base path when checking the request path against the routes. It uses the routes defined in the OpenAPI spec without the base path. To work around this, I overwrote the inline.tmpl
template and hacked the GetSwagger()
function to include this at the bottom:
func GetSwagger(pathPrefix string) (swagger *openapi3.T, err error) {
...
var updatedPaths openapi3.Paths = make(openapi3.Paths)
for key, value := range(swagger.Paths) {
updatedPaths[pathPrefix + key] = value
}
swagger.Paths = updatedPaths
}
The key in the Path
map is the route. I just append the base path to every key.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论