[BUG][Java][okhttp-gson] Header parameter with content: application/json is serialized via toString()

Author: AndreyVMarkelovCreated Sep 30, 2026Updated Sep 30, 2026
LabelsIssue: Bug

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request
Description

For Java okhttp-gson clients, a header parameter declared with content: application/json is serialized through ApiClient.parameterToString(...).

For model objects, parameterToString(...) falls back to String.valueOf(param), which produces the generated model's debug toString() representation rather than JSON.

For example, a model containing path: /x produces a multiline value beginning with something like:

class HeaderArg {
    path: /x
}

That value is not the JSON representation required by the parameter's content: application/json declaration.

The expected header value is:

X-Json-Arg: {"path":"/x"}

This is reproducible without making any HTTP requests.

openapi-generator version

Reproduced with:

  • OpenAPI Generator 7.25.0
  • current master / 7.26.0-SNAPSHOT
OpenAPI declaration file content or url

Minimal reproducer:

yaml
openapi: 3.1.0

info:
  title: JSON Header Content
  version: 1.0.0

paths:
  /test:
    post:
      operationId: testJsonHeader
      parameters:
        - name: X-Json-Arg
          in: header
          required: true
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HeaderArg'
      responses:
        '204':
          description: OK

components:
  schemas:
    HeaderArg:
      type: object
      required:
        - path
      properties:
        path:
          type: string
Generation Details

Generate a Java client with okhttp-gson:

bash
openapi-generator-cli generate \
  -i json-header-content.yaml \
  -g java \
  -o /tmp/java-json-header \
  --additional-properties=library=okhttp-gson

The generated API currently contains:

java
localVarHeaderParams.put(
    "X-Json-Arg",
    localVarApiClient.parameterToString(xJsonArg)
);

For:

java
new HeaderArg().path("/x")

ApiClient.parameterToString(...) returns the model's debug toString() representation, not:

json
{"path":"/x"}
Steps to reproduce
  1. Generate the Java okhttp-gson client from the minimal spec above.

  2. Inspect the generated API implementation.

  3. Observe that the JSON-content header parameter is serialized with:

    java
    localVarApiClient.parameterToString(xJsonArg)
  4. Evaluate:

    java
    new ApiClient().parameterToString(
        new HeaderArg().path("/x")
    )
  5. The returned string is the generated model's multiline debug representation rather than JSON.

No server, access token, or network request is required.

Expected behavior

A header parameter declared with:

yaml
content:
  application/json:

should be serialized as JSON before being placed in the HTTP header.

For example:

X-Json-Arg: {"path":"/x"}

For ASCII-safe HTTP header output, non-ASCII characters should also be escaped. For example, a path containing /тест should be represented with JSON Unicode escapes such as:

{"path":"/\u0442\u0435\u0441\u0442"}
Related issues/PRs

I searched existing issues before filing this report. I found issues involving Content-Type handling and general complex parameter serialization, but not this specific case of a header parameter declared with content: application/json being serialized through parameterToString(...).

Suggest a fix

DefaultCodegen already preserves the parameter media type in CodegenParameter.contentType.

A generic fix can:

  • mark header parameters whose contentType is JSON,
  • use JSON serialization for those header values,
  • keep existing parameterToString(...) behavior for non-JSON headers,
  • produce ASCII-safe JSON for header values.

A regression test can use the minimal spec above and verify that the generated Java okhttp-gson code uses the JSON serialization path for X-Json-Arg.

Source: OpenAPITools/openapi-generator