Echo Groups在使用oapi-codegen生成的OpenAPI代码中无法正常工作。

huangapple go评论153阅读模式
英文:

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 {&quot;message&quot;:&quot;no matching operation was found&quot;} for any request in that group:

	swagger, err := Openapi.GetSwagger()
	if err != nil {
		fmt.Fprintf(os.Stderr, &quot;Error loading swagger spec\n: %s&quot;, err)
		os.Exit(1)
	}

	// Use oapi validation middleware to check all requests against the
	// OpenAPI schema.
	g := e.Group(&quot;/api&quot;, middleware.OapiRequestValidator(swagger))
	Openapi.RegisterHandlers(g, &amp;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/&lt;some undefined api&gt; 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.

huangapple
  • 本文由 发表于 2021年11月24日 04:35:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/70087465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定