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

Support performing content-type negotiation on requests

Open jamietanna opened this issue 3 years ago • 0 comments

If an API handles multiple types of request body format, or returns multiple response body formats, it is likely to be performing server-driven content-type negotation.

For example:

https://github.com/deepmap/oapi-codegen/blob/fec990c8f823a7f5db876a133c087338afc0a9ce/internal/test/client/client.yaml#L70-L82

The proposed change is to:

  • return an HTTP 406 if the result of the negotiation between the Accept header and the configured set of media types fails
  • return an HTTP 415 if the result of the negotiation between the Content-Type header and the configured set of media types fails
  • (optionally, introducing a breaking change) we provide the negotiated media type of the request (content-type), and negotiated media type of the response (accept)

I've written https://gitlab.com/jamietanna/content-negotiation-go for this, and feel it'd work quite nicely in this case.

This does add extra complexity, and can introduce breaking functionality for consumers, so I'd definitely see this as something that can be optionally added.

jamietanna avatar Aug 30 '22 14:08 jamietanna