Skip to content

http strict request bodies should be optional by default #1584

@emilien-puget

Description

@emilien-puget

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions