diff --git a/modules/openapi-generator/src/main/resources/python-pydantic-v1/api_client.mustache b/modules/openapi-generator/src/main/resources/python-pydantic-v1/api_client.mustache index 000b6658b131..244474c13c6f 100644 --- a/modules/openapi-generator/src/main/resources/python-pydantic-v1/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/python-pydantic-v1/api_client.mustache @@ -220,6 +220,12 @@ class ApiClient: collection_formats) url += "?" + url_query + def extract_charset(content_type): + match = None + if content_type is not None: + match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type) + return match.group(1) if match else "utf-8" + try: # perform request and return response response_data = {{#asyncio}}await {{/asyncio}}{{#tornado}}yield {{/tornado}}self.request( @@ -231,7 +237,12 @@ class ApiClient: _request_timeout=_request_timeout) except ApiException as e: if e.body: - e.body = e.body.decode('utf-8') + try: + charset = extract_charset((e.headers or {}).get('content-type')) + e.body = e.body.decode(charset) + except UnicodeDecodeError: + # Keep original body if charset is not recognized. + pass raise e self.last_response = response_data @@ -247,12 +258,9 @@ class ApiClient: if response_type == "bytearray": response_data.data = response_data.data else: - match = None content_type = response_data.getheader('content-type') - if content_type is not None: - match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type) - encoding = match.group(1) if match else "utf-8" - response_data.data = response_data.data.decode(encoding) + charset = extract_charset(content_type) + response_data.data = response_data.data.decode(charset) # deserialize response data if response_type == "bytearray": diff --git a/samples/client/echo_api/python-pydantic-v1/openapi_client/api_client.py b/samples/client/echo_api/python-pydantic-v1/openapi_client/api_client.py index 6d0d11c5b538..13e5efa0481d 100644 --- a/samples/client/echo_api/python-pydantic-v1/openapi_client/api_client.py +++ b/samples/client/echo_api/python-pydantic-v1/openapi_client/api_client.py @@ -207,6 +207,12 @@ def __call_api( collection_formats) url += "?" + url_query + def extract_charset(content_type): + match = None + if content_type is not None: + match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type) + return match.group(1) if match else "utf-8" + try: # perform request and return response response_data = self.request( @@ -218,7 +224,12 @@ def __call_api( _request_timeout=_request_timeout) except ApiException as e: if e.body: - e.body = e.body.decode('utf-8') + try: + charset = extract_charset((e.headers or {}).get('content-type')) + e.body = e.body.decode(charset) + except UnicodeDecodeError: + # Keep original body if charset is not recognized. + pass raise e self.last_response = response_data @@ -234,12 +245,9 @@ def __call_api( if response_type == "bytearray": response_data.data = response_data.data else: - match = None content_type = response_data.getheader('content-type') - if content_type is not None: - match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type) - encoding = match.group(1) if match else "utf-8" - response_data.data = response_data.data.decode(encoding) + charset = extract_charset(content_type) + response_data.data = response_data.data.decode(charset) # deserialize response data if response_type == "bytearray": diff --git a/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/api_client.py b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/api_client.py index 52390c675df6..c61be95af42b 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/api_client.py +++ b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/api_client.py @@ -187,6 +187,12 @@ async def __call_api( collection_formats) url += "?" + url_query + def extract_charset(content_type): + match = None + if content_type is not None: + match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type) + return match.group(1) if match else "utf-8" + try: # perform request and return response response_data = await self.request( @@ -198,7 +204,12 @@ async def __call_api( _request_timeout=_request_timeout) except ApiException as e: if e.body: - e.body = e.body.decode('utf-8') + try: + charset = extract_charset((e.headers or {}).get('content-type')) + e.body = e.body.decode(charset) + except UnicodeDecodeError: + # Keep original body if charset is not recognized. + pass raise e self.last_response = response_data @@ -214,12 +225,9 @@ async def __call_api( if response_type == "bytearray": response_data.data = response_data.data else: - match = None content_type = response_data.getheader('content-type') - if content_type is not None: - match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type) - encoding = match.group(1) if match else "utf-8" - response_data.data = response_data.data.decode(encoding) + charset = extract_charset(content_type) + response_data.data = response_data.data.decode(charset) # deserialize response data if response_type == "bytearray": diff --git a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/api_client.py b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/api_client.py index fae62ad75768..ede41232a231 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/api_client.py +++ b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/api_client.py @@ -206,6 +206,12 @@ def __call_api( collection_formats) url += "?" + url_query + def extract_charset(content_type): + match = None + if content_type is not None: + match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type) + return match.group(1) if match else "utf-8" + try: # perform request and return response response_data = self.request( @@ -217,7 +223,12 @@ def __call_api( _request_timeout=_request_timeout) except ApiException as e: if e.body: - e.body = e.body.decode('utf-8') + try: + charset = extract_charset((e.headers or {}).get('content-type')) + e.body = e.body.decode(charset) + except UnicodeDecodeError: + # Keep original body if charset is not recognized. + pass raise e self.last_response = response_data @@ -233,12 +244,9 @@ def __call_api( if response_type == "bytearray": response_data.data = response_data.data else: - match = None content_type = response_data.getheader('content-type') - if content_type is not None: - match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type) - encoding = match.group(1) if match else "utf-8" - response_data.data = response_data.data.decode(encoding) + charset = extract_charset(content_type) + response_data.data = response_data.data.decode(charset) # deserialize response data if response_type == "bytearray":