From 4ff8c3aaefcf707a481e27c08803c4f940242cb6 Mon Sep 17 00:00:00 2001 From: Kunitsyn Oleg <114428801+genestack-okunitsyn@users.noreply.github.com> Date: Tue, 8 Oct 2024 03:34:36 +0200 Subject: [PATCH] [Python][Client] Allow all content-types with `text/` prefix (#19802) * ODM-12108: allow all content-types with text/ prefix * ODM-12108: Update deserialization tests --------- Co-authored-by: Oleg Kunitsyn --- .../src/main/resources/python/api_client.mustache | 2 +- .../src/main/resources/python/rest.mustache | 2 +- .../openapi_client/api_client.py | 2 +- .../openapi_client/rest.py | 2 +- .../echo_api/python/openapi_client/api_client.py | 2 +- .../client/echo_api/python/openapi_client/rest.py | 2 +- .../python-aiohttp/petstore_api/api_client.py | 2 +- .../petstore/python/petstore_api/api_client.py | 2 +- .../client/petstore/python/petstore_api/rest.py | 2 +- .../petstore/python/tests/test_deserialization.py | 12 +++++++++--- 10 files changed, 18 insertions(+), 12 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 dc0fc0f895a8..3580c8ff9c68 100644 --- a/modules/openapi-generator/src/main/resources/python/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/python/api_client.mustache @@ -417,7 +417,7 @@ class ApiClient: data = "" else: data = json.loads(response_text) - elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE): + elif re.match(r'^text\/[a-z.+-]+\s*(;|$)', content_type, re.IGNORECASE): data = response_text else: raise ApiException( diff --git a/modules/openapi-generator/src/main/resources/python/rest.mustache b/modules/openapi-generator/src/main/resources/python/rest.mustache index 07aa7ee3feff..fc71a59cf404 100644 --- a/modules/openapi-generator/src/main/resources/python/rest.mustache +++ b/modules/openapi-generator/src/main/resources/python/rest.mustache @@ -215,7 +215,7 @@ class RESTClientObject: headers=headers, preload_content=False ) - elif headers['Content-Type'] == 'text/plain' and isinstance(body, bool): + elif headers['Content-Type'].startswith('text/') and isinstance(body, bool): request_body = "true" if body else "false" r = self.pool_manager.request( method, 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 d6403e55cdc1..60d053860e36 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 @@ -410,7 +410,7 @@ def deserialize(self, response_text: str, response_type: str, content_type: Opti data = "" else: data = json.loads(response_text) - elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE): + elif re.match(r'^text\/[a-z.+-]+\s*(;|$)', content_type, re.IGNORECASE): data = response_text else: raise ApiException( diff --git a/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/rest.py b/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/rest.py index 02b8176438b1..91fb8eb029dc 100644 --- a/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/rest.py +++ b/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/rest.py @@ -226,7 +226,7 @@ def request( headers=headers, preload_content=False ) - elif headers['Content-Type'] == 'text/plain' and isinstance(body, bool): + elif headers['Content-Type'].startswith('text/') and isinstance(body, bool): request_body = "true" if body else "false" r = self.pool_manager.request( method, 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 d6403e55cdc1..60d053860e36 100644 --- a/samples/client/echo_api/python/openapi_client/api_client.py +++ b/samples/client/echo_api/python/openapi_client/api_client.py @@ -410,7 +410,7 @@ def deserialize(self, response_text: str, response_type: str, content_type: Opti data = "" else: data = json.loads(response_text) - elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE): + elif re.match(r'^text\/[a-z.+-]+\s*(;|$)', content_type, re.IGNORECASE): data = response_text else: raise ApiException( diff --git a/samples/client/echo_api/python/openapi_client/rest.py b/samples/client/echo_api/python/openapi_client/rest.py index fbb002c0f6bd..b97741ddfce4 100644 --- a/samples/client/echo_api/python/openapi_client/rest.py +++ b/samples/client/echo_api/python/openapi_client/rest.py @@ -226,7 +226,7 @@ def request( headers=headers, preload_content=False ) - elif headers['Content-Type'] == 'text/plain' and isinstance(body, bool): + elif headers['Content-Type'].startswith('text/') and isinstance(body, bool): request_body = "true" if body else "false" r = self.pool_manager.request( method, 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 36890304c706..7fef2f7c0db7 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 @@ -412,7 +412,7 @@ def deserialize(self, response_text: str, response_type: str, content_type: Opti data = "" else: data = json.loads(response_text) - elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE): + elif re.match(r'^text\/[a-z.+-]+\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 a5e7d86b0a08..ae27d95bd4f3 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/api_client.py +++ b/samples/openapi3/client/petstore/python/petstore_api/api_client.py @@ -409,7 +409,7 @@ def deserialize(self, response_text: str, response_type: str, content_type: Opti data = "" else: data = json.loads(response_text) - elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE): + elif re.match(r'^text\/[a-z.+-]+\s*(;|$)', content_type, re.IGNORECASE): data = response_text else: raise ApiException( diff --git a/samples/openapi3/client/petstore/python/petstore_api/rest.py b/samples/openapi3/client/petstore/python/petstore_api/rest.py index 022f9a82e3fb..3c7cc883f69b 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/rest.py +++ b/samples/openapi3/client/petstore/python/petstore_api/rest.py @@ -225,7 +225,7 @@ def request( headers=headers, preload_content=False ) - elif headers['Content-Type'] == 'text/plain' and isinstance(body, bool): + elif headers['Content-Type'].startswith('text/') and isinstance(body, bool): request_body = "true" if body else "false" r = self.pool_manager.request( method, diff --git a/samples/openapi3/client/petstore/python/tests/test_deserialization.py b/samples/openapi3/client/petstore/python/tests/test_deserialization.py index 0ebfa1bb30a0..8db2929be354 100644 --- a/samples/openapi3/client/petstore/python/tests/test_deserialization.py +++ b/samples/openapi3/client/petstore/python/tests/test_deserialization.py @@ -322,12 +322,18 @@ def test_deserialize_content_type(self): deserialized = self.deserialize(response, "str", 'text/plain') self.assertTrue(isinstance(deserialized, str)) - + + deserialized = self.deserialize(response, "str", 'text/csv') + 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') + with self.assertRaises(petstore_api.ApiException) as cm: - deserialized = self.deserialize(response, "str", 'text/html') + deserialized = self.deserialize(response, "str", 'text/n0t-exist!ng') with self.assertRaises(petstore_api.ApiException) as cm: deserialized = self.deserialize(response, "Dict[str, str]", 'application/jsonnnnn')