fix(java): serialize JSON header parameter content
Merged
AN
2mo ago+838+838+012345678901234567890123456789-2-2-0123456789
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
\uXXXXfor ASCII-safe HTTP header values. - Preserve existing
parameterToString(...)behavior for non-JSON headers.
Tests
Added:
DefaultCodegenregression coverage for JSON header parameter metadata- Java
okhttp-gsongeneration 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
CodegenParameterviaheaderIsJsonMimeType. - Serializes flagged headers through a new
parameterToJsonString(...), returning an empty string for null values and escaping non-ASCII characters as\uXXXXto keep headers ASCII-safe. - Static methods dispatch on the flag; dynamic operations check the parameter
contentforapplication/json. - Non-JSON headers keep the existing
parameterToString(...)behavior. - Adds
DefaultCodegen, generated-output, and sampleApiClientregression tests, plus an OpenAPI 3.1 fixture, and regenerates theokhttp-gsonsamples.