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 0fe89af0b1c..dc0fc0f895a 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( 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 ed46d8c31a2..d6403e55cdc 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 @@ 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( 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 ed46d8c31a2..d6403e55cdc 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 @@ 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( 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 ab412d97656..36890304c70 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 @@ 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( 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 0aad03162b9..a5e7d86b0a0 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 @@ 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( diff --git a/samples/openapi3/client/petstore/python/tests/test_deserialization.py b/samples/openapi3/client/petstore/python/tests/test_deserialization.py index 11b2955015a..0ebfa1bb30a 100644 --- a/samples/openapi3/client/petstore/python/tests/test_deserialization.py +++ b/samples/openapi3/client/petstore/python/tests/test_deserialization.py @@ -301,3 +301,35 @@ class DeserializationTests(unittest.TestCase): 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, "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') + +