OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Python] Cannot access multiple response headers with the same name

Open
#19,154 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
    • Doesn't really make sense as this applies to any python client
  • Have you validated the input using an OpenAPI validator (example)?
    • Again not applicable
  • 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

I am unable to access response headers that share the same name.

openapi-generator version

7.7.0

OpenAPI declaration file content or url

Irrelevant as the issue lies in the python client itself.

Steps to reproduce
  1. Generate a python client for any API that returns multiple headers with the same name
  2. Call an endpoint that should return full response information ExampleApi(...).xxx_with_http_info(...)
  3. Observe that ApiResponse.headers only contains the first header as dict keys are unique
Suggest a fix

I was able to work around this by overriding the templates locally and adding the following 2 lines.

diff --git a/modules/openapi-generator/src/main/resources/python/api_client.mustache b/modules/openapi-generator/src/main/resources/python/api_client.mustache
index c7d4a6d700f..dc9c9057cf9 100644
--- a/modules/openapi-generator/src/main/resources/python/api_client.mustache
+++ b/modules/openapi-generator/src/main/resources/python/api_client.mustache
@@ -335,6 +335,7 @@ class ApiClient:
             status_code = response_data.status,
             data = return_data,
             headers = response_data.getheaders(),
+            full_headers = tuple(response_data.getheaders().items()),
             raw_data = response_data.data
         )
 
diff --git a/modules/openapi-generator/src/main/resources/python/api_response.mustache b/modules/openapi-generator/src/main/resources/python/api_response.mustache
index 9bc7c11f6b9..136a745cb69 100644
--- a/modules/openapi-generator/src/main/resources/python/api_response.mustache
+++ b/modules/openapi-generator/src/main/resources/python/api_response.mustache
@@ -13,6 +13,7 @@ class ApiResponse(BaseModel, Generic[T]):
 
     status_code: StrictInt = Field(description="HTTP status code")
     headers: Optional[Mapping[str, str]] = Field(None, description="HTTP headers")
+    full_headers: tuple[tuple[str, str], ...] = Field(None, description="Full HTTP headers")
     data: T = Field(description="Deserialized data given the data type")
     raw_data: StrictBytes = Field(description="Raw data (HTTP response body)")

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/openapi-generator/src/main/resources/python/api_client.mustache and api_response.mustache, then review how ExampleApi(...).xxx_with_http_info(...) exposes response headers. Generate a Python client for an API returning duplicate response-header names and verify that the resulting ApiResponse preserves all headers, not only unique dictionary keys.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.