From 6c8866871c64acbeaea063e1065779d9700d092e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=B5=E3=81=81?= Date: Wed, 7 Aug 2024 11:09:12 +0000 Subject: [PATCH 1/4] [Python] fix: #19285 --- .../src/main/resources/python/api_client.mustache | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 aad33bcc06cb..a4b00138b60c 100644 --- a/modules/openapi-generator/src/main/resources/python/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/python/api_client.mustache @@ -412,12 +412,12 @@ class ApiClient: data = json.loads(response_text) except ValueError: data = response_text - elif content_type.startswith("application/json"): + elif re.match(r'^application/(json|[\w!#$&.+-^_]+\+json)\s*(;|$)', content_type, re.IGNORECASE): if response_text == "": data = "" else: data = json.loads(response_text) - elif content_type.startswith("text/plain"): + elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE): data = response_text else: raise ApiException( From b06c4dda68f8124480d87d600253a38bd7c84459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=B5=E3=81=81?= Date: Wed, 7 Aug 2024 11:09:36 +0000 Subject: [PATCH 2/4] [python] update sample --- .../openapi_client/api_client.py | 4 ++-- samples/client/echo_api/python/openapi_client/api_client.py | 4 ++-- .../client/petstore/python-aiohttp/petstore_api/api_client.py | 4 ++-- .../client/petstore/python/petstore_api/api_client.py | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/api_client.py b/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/api_client.py index c6fa28de3197..d1c19f8f7ebe 100644 --- a/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/api_client.py +++ b/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/api_client.py @@ -405,12 +405,12 @@ def deserialize(self, response_text: str, response_type: str, content_type: Opti data = json.loads(response_text) except ValueError: data = response_text - elif content_type.startswith("application/json"): + elif re.match(r'^application/(json|[\w!#$&.+-^_]+\+json)\s*(;|$)', content_type, re.IGNORECASE): if response_text == "": data = "" else: data = json.loads(response_text) - elif content_type.startswith("text/plain"): + elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE): data = response_text else: raise ApiException( diff --git a/samples/client/echo_api/python/openapi_client/api_client.py b/samples/client/echo_api/python/openapi_client/api_client.py index c6fa28de3197..d1c19f8f7ebe 100644 --- a/samples/client/echo_api/python/openapi_client/api_client.py +++ b/samples/client/echo_api/python/openapi_client/api_client.py @@ -405,12 +405,12 @@ def deserialize(self, response_text: str, response_type: str, content_type: Opti data = json.loads(response_text) except ValueError: data = response_text - elif content_type.startswith("application/json"): + elif re.match(r'^application/(json|[\w!#$&.+-^_]+\+json)\s*(;|$)', content_type, re.IGNORECASE): if response_text == "": data = "" else: data = json.loads(response_text) - elif content_type.startswith("text/plain"): + elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE): data = response_text else: raise ApiException( diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api_client.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api_client.py index db57dd1827b6..40a6113a71f8 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api_client.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api_client.py @@ -407,12 +407,12 @@ def deserialize(self, response_text: str, response_type: str, content_type: Opti data = json.loads(response_text) except ValueError: data = response_text - elif content_type.startswith("application/json"): + elif re.match(r'^application/(json|[\w!#$&.+-^_]+\+json)\s*(;|$)', content_type, re.IGNORECASE): if response_text == "": data = "" else: data = json.loads(response_text) - elif content_type.startswith("text/plain"): + elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE): data = response_text else: raise ApiException( diff --git a/samples/openapi3/client/petstore/python/petstore_api/api_client.py b/samples/openapi3/client/petstore/python/petstore_api/api_client.py index cb831d0de813..3dcda1b2baff 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/api_client.py +++ b/samples/openapi3/client/petstore/python/petstore_api/api_client.py @@ -404,12 +404,12 @@ def deserialize(self, response_text: str, response_type: str, content_type: Opti data = json.loads(response_text) except ValueError: data = response_text - elif content_type.startswith("application/json"): + elif re.match(r'^application/(json|[\w!#$&.+-^_]+\+json)\s*(;|$)', content_type, re.IGNORECASE): if response_text == "": data = "" else: data = json.loads(response_text) - elif content_type.startswith("text/plain"): + elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE): data = response_text else: raise ApiException( From 3f91577e5869ca6e37e35961f546e348b61b2f31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=B5=E3=81=81?= Date: Wed, 7 Aug 2024 11:10:01 +0000 Subject: [PATCH 3/4] [python] add test --- .../python/tests/test_deserialization.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/samples/openapi3/client/petstore/python/tests/test_deserialization.py b/samples/openapi3/client/petstore/python/tests/test_deserialization.py index 11b2955015ac..706c26dff4a5 100644 --- a/samples/openapi3/client/petstore/python/tests/test_deserialization.py +++ b/samples/openapi3/client/petstore/python/tests/test_deserialization.py @@ -301,3 +301,39 @@ def test_deserialize_animal(self): self.assertEqual(deserialized.class_name, "Cat") self.assertEqual(deserialized.declawed, True) self.assertEqual(deserialized.to_json(), '{"className": "Cat", "color": "red", "declawed": true}') + + def test_deserialize_content_type(self): + + response = json.dumps({"a": "a"}) + + deserialized = self.deserialize(response, "Dict[str, str]", 'application/json') + self.assertTrue(isinstance(deserialized, dict)) + + + deserialized = self.deserialize(response, "Dict[str, str]", 'application/vnd.api+json') + self.assertTrue(isinstance(deserialized, dict)) + + + deserialized = self.deserialize(response, "Dict[str, str]", 'application/json; charset=utf-8') + self.assertTrue(isinstance(deserialized, dict)) + + deserialized = self.deserialize(response, "Dict[str, str]", 'application/vnd.api+json; charset=utf-8') + self.assertTrue(isinstance(deserialized, dict)) + + + deserialized = self.deserialize(response.encode('shift_jis'), "Dict[str, str]", 'application/json; charset=shift_jis') + self.assertTrue(isinstance(deserialized, dict)) + + deserialized = self.deserialize(response, "str", 'text/plain') + self.assertTrue(isinstance(deserialized, str)) + + deserialized = self.deserialize(response, "Dict[str, str]", 'APPLICATION/JSON') + self.assertTrue(isinstance(deserialized, dict)) + + with self.assertRaises(petstore_api.ApiException) as cm: + deserialized = self.deserialize(response, "str", 'text/html') + + with self.assertRaises(petstore_api.ApiException) as cm: + deserialized = self.deserialize(response, "Dict[str, str]", 'application/jsonnnnn') + + From 2c435ae7f5a6d734f14848fbcbace8df56a82c45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=B5=E3=81=81?= Date: Tue, 20 Aug 2024 13:36:03 +0000 Subject: [PATCH 4/4] [python] remove test --- .../client/petstore/python/tests/test_deserialization.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/samples/openapi3/client/petstore/python/tests/test_deserialization.py b/samples/openapi3/client/petstore/python/tests/test_deserialization.py index 706c26dff4a5..0ebfa1bb30a0 100644 --- a/samples/openapi3/client/petstore/python/tests/test_deserialization.py +++ b/samples/openapi3/client/petstore/python/tests/test_deserialization.py @@ -320,10 +320,6 @@ def test_deserialize_content_type(self): deserialized = self.deserialize(response, "Dict[str, str]", 'application/vnd.api+json; charset=utf-8') self.assertTrue(isinstance(deserialized, dict)) - - deserialized = self.deserialize(response.encode('shift_jis'), "Dict[str, str]", 'application/json; charset=shift_jis') - self.assertTrue(isinstance(deserialized, dict)) - deserialized = self.deserialize(response, "str", 'text/plain') self.assertTrue(isinstance(deserialized, str))