fix(python): serialize JSON header parameter content

Merged
AN
2mo ago
+122-2

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.