Skip to content

fix(java): serialize JSON header parameter content - #25056

Open
AndreyVMarkelov wants to merge 1 commit into
OpenAPITools:masterfrom
AndreyVMarkelov:fix/java-json-header
Open

AndreyVMarkelov wants to merge 1 commit into
OpenAPITools:masterfrom
AndreyVMarkelov:fix/java-json-header

Conversation

@AndreyVMarkelov

@AndreyVMarkelov AndreyVMarkelov commented Sep 30, 2026 •

Copy link
Copy Markdown

Fixes #25055

Problem

For Java okhttp-gson clients, a header parameter defined using:

content:
  application/json:
    schema:
      type: object

was serialized through ApiClient.parameterToString(...).

For model objects, that produces the generated model's debug toString() representation instead of JSON.

Fix

  • Preserve whether a header parameter uses a JSON media type.
  • Serialize JSON-content header parameters through a dedicated JSON serialization path.
  • Escape non-ASCII characters as \uXXXX for ASCII-safe HTTP header values.
  • Preserve existing parameterToString(...) behavior for non-JSON headers.

Tests

Added:

  • DefaultCodegen regression coverage for JSON header parameter metadata
  • Java okhttp-gson generation regression coverage
  • a minimal OpenAPI 3.1 fixture

Verified:

  • generated header code uses parameterToJsonString(...)
  • {"path":"/x"} is produced for ASCII input
  • non-ASCII input such as /тест is escaped as \uXXXX
  • generated Java client compiles successfully

Summary by cubic

Fixes Java okhttp-gson clients sending JSON-content header parameters as the model's debug toString() output instead of JSON, in both static and dynamic operations paths.

  • Flags JSON media type headers on CodegenParameter via headerIsJsonMimeType.
  • Serializes flagged headers through a new parameterToJsonString(...) method, escaping non-ASCII characters as \uXXXX to keep HTTP headers ASCII-safe.
  • Static methods dispatch on the flag; dynamic operations check the parameter content for application/json.
  • Non-JSON headers keep the existing parameterToString(...) behavior.
  • Adds DefaultCodegen, generated-output, and sample ApiClient regression tests, plus an OpenAPI 3.1 fixture, and regenerates the okhttp-gson samples.

Written for commit 1a5fbe0. Summary will update on new commits.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

@AndreyVMarkelov

Copy link
Copy Markdown
Author

@KannaKim @martin-mfg would you mind taking a look when you have a chance? This is a small Java client bug fix with regression coverage and green CI.

This issue currently blocks our work on a new Dropbox SDK generated with OpenAPI Generator, so getting it into the next release would be very helpful. Thanks!

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 15 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/ApiClient.java">

<violation number="1" location="samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/ApiClient.java:819">
P2: This helper is never used by the dynamic-operation header path, so JSON header parameters still serialize through `parameterToString(value)` and send generated model strings. Update `fillParametersFromOperation` to select `parameterToJsonString` for JSON content and retain `parameterToString` for other headers.</violation>
</file>

<file name="samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/ApiClient.java">

<violation number="1" location="samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/ApiClient.java:804">
P3: `parameterToJsonString` always copies the whole `JSON.serialize` result into a second StringBuilder, even when the JSON contains no character >= 0x7f (the common case for header values such as `{"path":"/x"}`). Scan for a non-ASCII char first and return the JSON string unchanged when none is found, avoiding the allocation and full second pass.</violation>
</file>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 19 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

This branch has not been deployed

No deployments
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.

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

1 participant