This is the command I'm using to generate the gin server:
oapi-codegen -generate gin,types,spec -package api api/openapi/api.yaml > internal/api/openapi/api.go
When the gin server code is generated, it provides a wrapper for the service interface. This wrapper does some request validation logic; however, when it encounters an error, it dumps it to the response payload...
// FindPetById operation middleware
func (siw *ServerInterfaceWrapper) FindPetById(c *gin.Context) {
...
err = runtime.BindStyledParameter("simple", false, "id", c.Param("id"), &id)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Invalid format for parameter id: %s", err)})
return
}
...
}
What is expected is...
// FindPetById operation middleware
func (siw *ServerInterfaceWrapper) FindPetById(c *gin.Context) {
...
err = runtime.BindStyledParameter("simple", false, "id", c.Param("id"), &id)
if err != nil {
c.Error(err)
return
}
...
}
...this way a middleware can handle the error. Is there a way to either:
- Generate the wrapper so it behaves as ☝️ ?
- Omit the wrapper and implement the validation myself?
I've also submitted an issue with the maintainers: https://github.com/deepmap/oapi-codegen/issues/584
Pull request here: https://github.com/deepmap/oapi-codegen/pull/587