Echo Groups not working with OpenAPI generated code using oapi-codegen

0

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()

go go-echo openapi
2021-11-23 20:35:13
1

0

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.

2021-11-30 04:00:22

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................