Skip to content

fix: handle optional request bodies in strict server mode#2222

Merged
mromaszewicz merged 1 commit intooapi-codegen:mainfrom
mromaszewicz:fix/issue-2116
Feb 16, 2026
Merged

fix: handle optional request bodies in strict server mode#2222
mromaszewicz merged 1 commit intooapi-codegen:mainfrom
mromaszewicz:fix/issue-2116

Conversation

@mromaszewicz
Copy link
Member

Fixes #2116

Strict server handlers unconditionally decoded the request body, causing an EOF error when no body was sent for operations with optional request bodies (requestBody.required defaults to false in OpenAPI 3.0).

For optional JSON bodies, the generated handlers now treat io.EOF from the decoder as "no body provided" and leave request.Body as nil rather than returning an error. For optional text bodies, an empty read is similarly treated as absent. Required bodies retain the original unconditional error handling.

All five strict server templates are updated: stdhttp (used by chi and gorilla), echo, gin, fiber, and iris.

Two new test operations (RequiredJSONBody, RequiredTextBody) with required: true are added to the strict server test schema to verify both code paths are generated correctly.

Fixes oapi-codegen#2116

Strict server handlers unconditionally decoded the request body,
causing an EOF error when no body was sent for operations with
optional request bodies (requestBody.required defaults to false in
OpenAPI 3.0).

For optional JSON bodies, the generated handlers now treat io.EOF
from the decoder as "no body provided" and leave request.Body as nil
rather than returning an error. For optional text bodies, an empty
read is similarly treated as absent. Required bodies retain the
original unconditional error handling.

All five strict server templates are updated: stdhttp (used by chi
and gorilla), echo, gin, fiber, and iris.

Two new test operations (RequiredJSONBody, RequiredTextBody) with
required: true are added to the strict server test schema to verify
both code paths are generated correctly.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@mromaszewicz mromaszewicz requested a review from a team as a code owner February 14, 2026 05:32
@jamietanna jamietanna added this to the v2.6.0 milestone Feb 15, 2026
@jamietanna jamietanna added the bug Something isn't working label Feb 16, 2026
@mromaszewicz mromaszewicz merged commit 99615d0 into oapi-codegen:main Feb 16, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Strict Server mode does not respect optional requestBody

2 participants