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

http strict request bodies should be optional by default

Open emilien-puget opened this issue 1 year ago • 2 comments

request bodies should be optional by default see https://spec.openapis.org/oas/v3.1.0#fixed-fields-10 or https://swagger.io/docs/specification/describing-request-body/

but as we can see here https://github.com/deepmap/oapi-codegen/blob/621b44626ac9a1aa6c9fdda97324a58569227281/pkg/codegen/templates/strict/strict-http.tmpl#L50, we check that a content type is set only when multiple request bodies are defined, so if our one and only request body is not declared as required, this generated code will return an error , i made this custom template to handle that issue https://gist.github.com/emilien-puget/6d0e0af27c72f4cf331991cccfeb96f4#file-strict-http-tmpl-L50

i also made a custom template https://gist.github.com/emilien-puget/6d0e0af27c72f4cf331991cccfeb96f4#file-strict-interface-tmpl-L15 to handle required body https://github.com/deepmap/oapi-codegen/blob/621b44626ac9a1aa6c9fdda97324a58569227281/pkg/codegen/templates/strict/strict-interface.tmpl#L15 as seen here, when the body is set as required we could drop the pointer on the body.

would you be interested having a PR that change the behavior of the generated code to handle such cases ? we could have an operation-output option like "optional-request-bodies" to switch to this new behavior.

emilien-puget avatar Apr 30 '24 14:04 emilien-puget

Interesting, thank you! Yes we'd appreciate a PR for that, and agreed that having it behind optional-request-bodies would be best :raised_hands:

jamietanna avatar May 02 '24 08:05 jamietanna

started to do stuff and things regarding this issue there

emilien-puget avatar May 11 '24 14:05 emilien-puget

@jamietanna have you had the chance to take a look at my draft?

emilien-puget avatar Jun 04 '24 10:06 emilien-puget