Skip to content

fix(python): serialize JSON header parameter content - #25068

Merged
wing328 merged 2 commits into
OpenAPITools:masterfrom
AndreyVMarkelov:fix/python-json-header
Oct 4, 2026
Merged

wing328 merged 2 commits into
OpenAPITools:masterfrom
AndreyVMarkelov:fix/python-json-header

Conversation

@AndreyVMarkelov

@AndreyVMarkelov AndreyVMarkelov commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fix Python client serialization of header parameters defined with
content: application/json.

Previously, the generated client assigned the parameter directly to the
header:

_header_params['X-Json-Arg'] = x_json_arg

ApiClient.sanitize_for_serialization() converts model instances to
dictionaries, but those values were not JSON-encoded before being used
as HTTP header values.

This change:

  • detects header parameters whose content type is JSON;
  • marks them during Python parameter post-processing;
  • serializes those header values with json.dumps() after
    sanitize_for_serialization();
  • preserves the existing behavior for regular schema-based headers;
  • adds a regression test covering both JSON-content and regular
    headers.

Generated code for a JSON-content header now uses:

_header_params['X-Json-Arg'] = json.dumps(
    self.api_client.sanitize_for_serialization(x_json_arg)
)

while a regular header remains:

_header_params['X-Plain-Arg'] = x_plain_arg

Tests

Added testJsonContentHeaderUsesJsonSerialization to
PythonClientCodegenTest.

The regression test verifies that:

  • content: application/json header parameters are JSON serialized;
  • ordinary header parameters are not JSON serialized.

Fixes #25067


Summary by cubic

Fixes a bug in the generated Python client where header parameters defined with content: application/json were sent without JSON serialization. These headers are now encoded with json.dumps() after sanitize_for_serialization(), while regular header parameters keep their previous behavior. The json import is generated only when an API uses a JSON-content header, and regression tests cover both cases.

Written for commit 2d53ce8. 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 across 4 files

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

Re-trigger cubic

Comment thread modules/openapi-generator/src/main/resources/python/api.mustache
@AndreyVMarkelov

Copy link
Copy Markdown
Contributor Author

@fa0311 @krjakbrjak would you mind taking a look when you have a chance? This is a small Python 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!

@wing328

wing328 commented Oct 4, 2026 •

Copy link
Copy Markdown
Member

lgtm. let's give it a try. thanks for the contribution.

@wing328
wing328 merged commit a5e4173 into OpenAPITools:master Oct 4, 2026
15 checks passed
@wing328 wing328 mentioned this pull request Oct 4, 2026
3 tasks
@wing328

wing328 commented Oct 4, 2026

Copy link
Copy Markdown
Member

FYI. Merged #25112 to add tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][Python] Header parameter with content: application/json is not JSON serialized

2 participants