Skip to content

Strict Server mode does not respect optional requestBody #2116

Closed

Description

@ryan-bever

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 created

Generation Config

package: main
output: server.gen.go
generate:
  std-http-server: true
  strict-server: true
  embedded-spec: true
  models: true

Generated 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

  1. Generate code with strict-server enabled for a spec with required: false on requestBody
  2. Start the server
  3. 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

  1. injeniero commented on Dec 15, 2025

    @injeniero

    Same problem here :-(

  2. injeniero commented on Dec 16, 2025

    @injeniero

    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:

    /templates-dir/
    + strict/
      - strict-http.tmpl
    

    Make sure you rename the attached file to remove the txt extension.

    @ryan-bever

    strict-http.tmpl.txt

  3. mromaszewicz commented on Feb 14, 2026

    @mromaszewicz
    Member

    Man, the specification for how to determine the presence of a body is complicated!

    https://datatracker.ietf.org/doc/html/rfc7230#section-3.3

  4. added a commit that references this issue on Feb 14, 2026
    f95dfeb
  5. added a commit that references this issue on Feb 16, 2026
    99615d0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions