Repository navigation
Description
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:
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: stringGeneration Details
Generate a Java client with okhttp-gson:
openapi-generator-cli generate \
-i json-header-content.yaml \
-g java \
-o /tmp/java-json-header \
--additional-properties=library=okhttp-gsonThe generated API currently contains:
localVarHeaderParams.put(
"X-Json-Arg",
localVarApiClient.parameterToString(xJsonArg)
);For:
new HeaderArg().path("/x")ApiClient.parameterToString(...) returns the model's debug toString() representation, not:
{"path":"/x"}Steps to reproduce
-
Generate the Java
okhttp-gsonclient from the minimal spec above. -
Inspect the generated API implementation.
-
Observe that the JSON-content header parameter is serialized with:
localVarApiClient.parameterToString(xJsonArg)
-
Evaluate:
new ApiClient().parameterToString( new HeaderArg().path("/x") )
-
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:
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
contentTypeis 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.
Activity