Repository navigation
Description
Strict Server mode does not respect optional requestBody
Description
When using strict-server generation mode, the generated code does not properly handle optional request bodies (required: false). The generated strict handler always attempts to decode the JSON body, causing requests without a body to fail with an EOF error, even though the OpenAPI specification explicitly marks the request body as optional.
Environment
- oapi-codegen version: v2.5.0
- Go version: 1.24.5
Minimal Reproduction
A complete minimal reproduction is available at: https://github.com/ryan-bever/optional-request-body
OpenAPI Specification
The spec defines an optional request body:
paths:
/things:
post:
summary: Create Thing
operationId: createThing
requestBody:
required: false # <-- Request body is optional
content:
application/json:
schema:
$ref: "#/components/schemas/ThingRequest"
responses:
"201":
description: Thing createdGeneration Config
package: main
output: server.gen.go
generate:
std-http-server: true
strict-server: true
embedded-spec: true
models: trueGenerated Code Issue
The generated strict handler always attempts to decode the body:
func (sh *strictHandler) CreateThing(w http.ResponseWriter, r *http.Request) {
var request CreateThingRequestObject
var body CreateThingJSONRequestBody
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
// This error handler is invoked even for empty bodies
sh.options.RequestErrorHandlerFunc(w, r, fmt.Errorf("can't decode JSON body: %w", err))
return
}
request.Body = &body
// ... rest of handler
}Steps to Reproduce
- Generate code with strict-server enabled for a spec with
required: falseon requestBody - Start the server
- Make a POST request without a body:
curl -X POST http://localhost:8080/things
Expected Behavior
The request should succeed. The handler should receive request.Body = nil when no body is provided, allowing the implementation to handle the optional body appropriately.
Actual Behavior
The request fails with:
can't decode JSON body: EOF
Workaround
Sending an empty JSON object works:
curl -X POST http://localhost:8080/things -d "{}"However, this defeats the purpose of having an optional request body and forces clients to always send at least an empty object.
Additional Context
The CreateThingRequestObject already correctly defines Body as a pointer (*CreateThingJSONRequestBody), which is appropriate for optional fields. The issue is solely in the decoding logic of the generated strict handler.
Activity
injeniero commentedon Dec 15, 2025
Same problem here :-(
injeniero commentedon Dec 16, 2025
I fixed for JSON by using a custom template for my server.
You have to use the config to specify it or the cmd line arg
--templates <dir>. The dir should look like:Make sure you rename the attached file to remove the txt extension.
@ryan-bever
strict-http.tmpl.txt
mromaszewicz commentedon Feb 14, 2026
Man, the specification for how to determine the presence of a body is complicated!
https://datatracker.ietf.org/doc/html/rfc7230#section-3.3
fix: handle optional request bodies in strict server mode
fix: handle optional request bodies in strict server mode (#2222)