Skip to content

fix: respect requestBody.required in strict server#1936

Closed
dbarrosop wants to merge 1 commit into
oapi-codegen:mainfrom
dbarrosop:gin-body-required-false
Closed

fix: respect requestBody.required in strict server#1936
dbarrosop wants to merge 1 commit into
oapi-codegen:mainfrom
dbarrosop:gin-body-required-false

Conversation

@dbarrosop

Copy link
Copy Markdown

I noticed that requestBody.required wasn't being respected and requests without a body were failing with an [EOF] error even though required was set to false.

This PR is a tentative fix to this issue. It certainly works as my API now behaves according to the spec and it fails without a body only if required: true

I was wondering if we should also do:

var body *{{$opid}}{{.NameTag}}RequestBody

That would mean that if the body isn't present we would correctly send a nil object down to the server implementation but I am concerned that would lead to lots of panics in existing services for users that hadn't been paying attention to the fact that required: false is the default behavior when not set.

Thoughts?

@dbarrosop dbarrosop requested a review from a team as a code owner March 23, 2025 18:40
@dbarrosop

Copy link
Copy Markdown
Author

any update on this PR? thanks!

@dbarrosop dbarrosop force-pushed the gin-body-required-false branch from 1ad6f36 to 6225e75 Compare August 1, 2025 08:12
@kusari-inspector

Copy link
Copy Markdown

Kusari Analysis Results

Analysis for commit: 6225e75, performed at: 2025-08-01T08:12:45Z

@kusari-inspector rerun - Trigger a re-analysis of this PR

@kusari-inspector feedback [your message] - Send feedback to our AI and team


Recommendation

✅ PROCEED with this Pull Request

Summary

No Flagged Issues Detected

All values appear to be within acceptable risk parameters.

No pinned version dependency changes, code issues or exposed secrets detected!

Found this helpful? Give it a 👍 or 👎 reaction!

@gologames

Copy link
Copy Markdown

We also need this fix to support required: false request bodies being generated. Up

@mromaszewicz

Copy link
Copy Markdown
Member

Thank you for submitting this PR, @dbarrosop — but we need to close it because the same fix (treating io.EOF as "no body" for optional JSON request bodies in strict server) was extended to all five strict-server templates and merged via #2222 (commit 99615d04). pkg/codegen/templates/strict/strict-gin.tmpl:83-94 now contains the guarded io.EOF check.

Thanks for the contribution.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants