Bug Report Checklist
Description
The Python client generator does not JSON-serialize header parameters
defined using content: application/json.
For a header parameter backed by an object schema, the generated client
assigns the model directly to the header:
_header_params['X-Json-Arg'] = x_json_arg
ApiClient.sanitize_for_serialization() later converts the model to a
Python dict, but that value is not JSON-encoded before it is used as
an HTTP header value.
As a result, a parameter declared with content: application/json does
not produce the JSON string required by the OpenAPI declaration.
Regular schema-based header parameters should keep their existing
behavior.
openapi-generator version
7.26.0-SNAPSHOT
Reproduced against the latest master.
OpenAPI declaration file content or url
Minimal repro:
openapi: 3.1.0
info:
title: JSON header content test
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'
- name: X-Plain-Arg
in: header
required: false
schema:
type: string
responses:
'204':
description: No content
components:
schemas:
HeaderArg:
type: object
properties:
path:
type: string
recursive:
type: boolean
Generation Details
Generator: python
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
-i /tmp/json-header-content.yaml \
-g python \
-o /tmp/python-json-header
Steps to reproduce
- Build the current
master.
- Save the OpenAPI document above as
/tmp/json-header-content.yaml.
- Generate a Python client using the command above.
- Inspect
openapi_client/api/default_api.py.
Actual generated code:
if x_json_arg is not None:
_header_params['X-Json-Arg'] = x_json_arg
Expected behavior:
if x_json_arg is not None:
_header_params['X-Json-Arg'] = json.dumps(
self.api_client.sanitize_for_serialization(x_json_arg)
)
The regular header should remain unchanged:
_header_params['X-Plain-Arg'] = x_plain_arg
Related issues/PRs
Related Java generator issue/PR: #25056
This is the equivalent problem in the Python client generator.
Suggest a fix
CodegenParameter.contentType is populated for parameters defined using
content, so the Python generator can detect JSON header parameters
during parameter post-processing.
A possible fix is to: - mark header parameters whose contentType is
JSON; - JSON-encode only those values in python/api.mustache; -
preserve the existing behavior for ordinary header parameters.
Bug Report Checklist
master
to confirm the issue still exists?
request
(example)
Description
The Python client generator does not JSON-serialize header parameters
defined using
content: application/json.For a header parameter backed by an object schema, the generated client
assigns the model directly to the header:
ApiClient.sanitize_for_serialization()later converts the model to aPython
dict, but that value is not JSON-encoded before it is used asan HTTP header value.
As a result, a parameter declared with
content: application/jsondoesnot produce the JSON string required by the OpenAPI declaration.
Regular schema-based header parameters should keep their existing
behavior.
openapi-generator version
7.26.0-SNAPSHOTReproduced against the latest
master.OpenAPI declaration file content or url
Minimal repro:
Generation Details
Generator:
pythonSteps to reproduce
master./tmp/json-header-content.yaml.openapi_client/api/default_api.py.Actual generated code:
Expected behavior:
The regular header should remain unchanged:
Related issues/PRs
Related Java generator issue/PR: #25056
This is the equivalent problem in the Python client generator.
Suggest a fix
CodegenParameter.contentTypeis populated for parameters defined usingcontent, so the Python generator can detect JSON header parametersduring parameter post-processing.
A possible fix is to: - mark header parameters whose
contentTypeisJSON; - JSON-encode only those values in
python/api.mustache; -preserve the existing behavior for ordinary header parameters.