[BUG][Python] Header parameter with content: application/json is not JSON serialized
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 (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:
_header_params['X-Json-Arg'] = x_json_argApiClient.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: booleanGeneration 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-headerSteps 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_argExpected 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_argRelated 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.
Source: OpenAPITools/openapi-generator