Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http strict request bodies should be optional by default #1584

Open
emilien-puget opened this issue Apr 30, 2024 · 2 comments
Open

http strict request bodies should be optional by default #1584

emilien-puget opened this issue Apr 30, 2024 · 2 comments

Comments

@emilien-puget
Copy link

emilien-puget commented Apr 30, 2024

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

{{if $multipleBodies}}if strings.HasPrefix(r.Header.Get("Content-Type"), "{{.ContentType}}") { {{end}}
, 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

{{if $multipleBodies}}{{.NameTag}}{{end}}Body {{if eq .NameTag "Multipart"}}*multipart.Reader{{else if ne .NameTag ""}}*{{$opid}}{{.NameTag}}RequestBody{{else}}io.Reader{{end}}
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.

@jamietanna
Copy link
Collaborator

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

@emilien-puget
Copy link
Author

started to do stuff and things regarding this issue there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants