oapi-codegen
oapi-codegen copied to clipboard
Should the requestBody be treated like params?
Hi,
I was just getting started with oapi-codegen
, and thanks a lot for the awesome tool!
One question I have, is that - unless I'm doing something wrong - the request body isn't automatically unmarshalled, so you have to do something like this: https://github.com/deepmap/oapi-codegen/blob/master/examples/petstore-expanded/echo/api/petstore.go#L85-L86
func (p *PetStore) AddPet(ctx echo.Context) error {
// We expect a NewPet object in the request body.
var newPet NewPet
err := ctx.Bind(&newPet)
Whereas, for the other types of parameters, you get a handy struct with things pre-filled:
https://github.com/deepmap/oapi-codegen/blob/master/examples/petstore-expanded/echo/api/petstore.go#L53
func (p *PetStore) FindPets(ctx echo.Context, params FindPetsParams) error {
Is that by design? Not a massive issue, but wouldn't it make more sense to treat the bodies the same way?
Thanks!
this issue helped me figure out how to get the defined body from the generated interface. I also assumed that the body would show up as a parameter in the definition of the function in the interface. The readme claims
If you have only a JSON request body, you will get: AddPet(ctx context.Context, body NewPet)
so I think we might be missing something. The body types show up as generated types just fine, though they're not different than normal objects I have defined in the components section.
looks like this is a similar issue: https://github.com/deepmap/oapi-codegen/issues/350. Bodies can be multiple types so should require a specific parser implementation, like json unmarshalling in the example you posted. Not sure what the readme is talking about then.
that part of the readme was written in this commit, which certainly implies that it's possible to generate a function definition with json parsed into a parameter. https://github.com/deepmap/oapi-codegen/commit/0c0f5d45d6279f5695bdf02bed713214fc45c33b. This line specifically will generate a body parameter in the function definition if the spec requires a body: https://github.com/deepmap/oapi-codegen/commit/0c0f5d45d6279f5695bdf02bed713214fc45c33b#diff-c8306209ecceed0e799909503c21e674164c27a31308bf57f75c68cd159cc33eR12.
That code in the latest version will generate that body parameter if the spec has a body, which it determines by checking for spec.RequestBody https://github.com/deepmap/oapi-codegen/blob/df873a05fb11cff56c6f46bf17e37486060e2db1/pkg/codegen/operations.go#L240, which im doing here:
paths: /v1/auth/login: post: summary: login requestBody: required: true content: application/json: schema: $ref: '#/components/schemas/LoginBody'
here's a closed issue that references the body parameter generation: https://github.com/deepmap/oapi-codegen/issues/43
That code in the latest version will generate that body parameter if the spec has a body, which it determines by checking for spec.RequestBody
It's for client only, not for server :(
See this PR