oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Gin strict handler uses Bind which causes the request to abort without error

Open jeroendk opened this issue 2 years ago • 0 comments

Gin Bind (MustBind under the hood) aborts the request immediately and sends a 400 error as plain text to the client.

  if err := ctx.Bind(&body); err != nil {
      ctx.Error(err)
      return
  }

This prevents any custom error handler from setting the proper content-type headers.

My suggestion would be to use ShouldBind instead and set the response status code for backward compatibility:

if err := ctx.ShouldBind(&body); err != nil {
    ctx.Status(http.StatusBadRequest)
    ctx.Error(err)
    return
}

jeroendk avatar Oct 28 '22 11:10 jeroendk