2

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:

  1. Generate the wrapper so it behaves as ☝️ ?
  2. 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

chaseisabelle
  • 162
  • 2
  • 18
  • If it helps anyone, which it did for me, you can actually override the templates with your own: https://github.com/deepmap/oapi-codegen#making-changes-to-code-generation – chaseisabelle Jul 07 '22 at 03:18

0 Answers0