OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.15k stars 6.41k forks source link

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

Open tom300z opened 1 month ago

tom300z commented 1 month ago

Bug Report Checklist

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)")
wing328 commented 1 month ago

@tom300z what about running tuple(api_response.headers.items()) using the existing headers field? would that meet your requirement?

tom300z commented 1 month ago

Yes, that should work too