diff --git a/modules/openapi-generator/src/main/resources/python/model_utils.mustache b/modules/openapi-generator/src/main/resources/python/model_utils.mustache index 0e8ad8be5db7..22fd37695516 100644 --- a/modules/openapi-generator/src/main/resources/python/model_utils.mustache +++ b/modules/openapi-generator/src/main/resources/python/model_utils.mustache @@ -8,6 +8,7 @@ import os import pprint import re import tempfile +import uuid from dateutil.parser import parse @@ -1082,7 +1083,13 @@ def deserialize_file(response_data, configuration, content_disposition=None): if content_disposition: filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?', - content_disposition).group(1) + content_disposition, + flags=re.I) + if filename is not None: + filename = filename.group(1) + else: + filename = "default_" + str(uuid.uuid4()) + path = os.path.join(os.path.dirname(path), filename) with open(path, "wb") as f: diff --git a/samples/client/petstore/python/petstore_api/model_utils.py b/samples/client/petstore/python/petstore_api/model_utils.py index 6414688b772b..4c3fa41c463c 100644 --- a/samples/client/petstore/python/petstore_api/model_utils.py +++ b/samples/client/petstore/python/petstore_api/model_utils.py @@ -16,6 +16,7 @@ import os import pprint import re import tempfile +import uuid from dateutil.parser import parse @@ -1399,7 +1400,13 @@ def deserialize_file(response_data, configuration, content_disposition=None): if content_disposition: filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?', - content_disposition).group(1) + content_disposition, + flags=re.I) + if filename is not None: + filename = filename.group(1) + else: + filename = "default_" + str(uuid.uuid4()) + path = os.path.join(os.path.dirname(path), filename) with open(path, "wb") as f: diff --git a/samples/client/petstore/python_disallowAdditionalPropertiesIfNotPresent/petstore_api/model_utils.py b/samples/client/petstore/python_disallowAdditionalPropertiesIfNotPresent/petstore_api/model_utils.py index 6414688b772b..4c3fa41c463c 100644 --- a/samples/client/petstore/python_disallowAdditionalPropertiesIfNotPresent/petstore_api/model_utils.py +++ b/samples/client/petstore/python_disallowAdditionalPropertiesIfNotPresent/petstore_api/model_utils.py @@ -16,6 +16,7 @@ import os import pprint import re import tempfile +import uuid from dateutil.parser import parse @@ -1399,7 +1400,13 @@ def deserialize_file(response_data, configuration, content_disposition=None): if content_disposition: filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?', - content_disposition).group(1) + content_disposition, + flags=re.I) + if filename is not None: + filename = filename.group(1) + else: + filename = "default_" + str(uuid.uuid4()) + path = os.path.join(os.path.dirname(path), filename) with open(path, "wb") as f: diff --git a/samples/openapi3/client/extensions/x-auth-id-alias/python/x_auth_id_alias/model_utils.py b/samples/openapi3/client/extensions/x-auth-id-alias/python/x_auth_id_alias/model_utils.py index 38a28ca20897..72322faf7895 100644 --- a/samples/openapi3/client/extensions/x-auth-id-alias/python/x_auth_id_alias/model_utils.py +++ b/samples/openapi3/client/extensions/x-auth-id-alias/python/x_auth_id_alias/model_utils.py @@ -16,6 +16,7 @@ import os import pprint import re import tempfile +import uuid from dateutil.parser import parse @@ -1399,7 +1400,13 @@ def deserialize_file(response_data, configuration, content_disposition=None): if content_disposition: filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?', - content_disposition).group(1) + content_disposition, + flags=re.I) + if filename is not None: + filename = filename.group(1) + else: + filename = "default_" + str(uuid.uuid4()) + path = os.path.join(os.path.dirname(path), filename) with open(path, "wb") as f: diff --git a/samples/openapi3/client/features/dynamic-servers/python/dynamic_servers/model_utils.py b/samples/openapi3/client/features/dynamic-servers/python/dynamic_servers/model_utils.py index d76884daf889..7b77e1be8643 100644 --- a/samples/openapi3/client/features/dynamic-servers/python/dynamic_servers/model_utils.py +++ b/samples/openapi3/client/features/dynamic-servers/python/dynamic_servers/model_utils.py @@ -16,6 +16,7 @@ import os import pprint import re import tempfile +import uuid from dateutil.parser import parse @@ -1399,7 +1400,13 @@ def deserialize_file(response_data, configuration, content_disposition=None): if content_disposition: filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?', - content_disposition).group(1) + content_disposition, + flags=re.I) + if filename is not None: + filename = filename.group(1) + else: + filename = "default_" + str(uuid.uuid4()) + path = os.path.join(os.path.dirname(path), filename) with open(path, "wb") as f: diff --git a/samples/openapi3/client/petstore/python/petstore_api/model_utils.py b/samples/openapi3/client/petstore/python/petstore_api/model_utils.py index 6414688b772b..4c3fa41c463c 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/model_utils.py +++ b/samples/openapi3/client/petstore/python/petstore_api/model_utils.py @@ -16,6 +16,7 @@ import os import pprint import re import tempfile +import uuid from dateutil.parser import parse @@ -1399,7 +1400,13 @@ def deserialize_file(response_data, configuration, content_disposition=None): if content_disposition: filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?', - content_disposition).group(1) + content_disposition, + flags=re.I) + if filename is not None: + filename = filename.group(1) + else: + filename = "default_" + str(uuid.uuid4()) + path = os.path.join(os.path.dirname(path), filename) with open(path, "wb") as f: diff --git a/samples/openapi3/client/petstore/python/tests_manual/test_fake_api.py b/samples/openapi3/client/petstore/python/tests_manual/test_fake_api.py index 33812b1b3f58..f9ac740e4ef7 100644 --- a/samples/openapi3/client/petstore/python/tests_manual/test_fake_api.py +++ b/samples/openapi3/client/petstore/python/tests_manual/test_fake_api.py @@ -495,42 +495,46 @@ class TestFakeApi(unittest.TestCase): # sample from http://www.jtricks.com/download-text file_name = 'content.txt' - headers = {'Content-Disposition': 'attachment; filename={}'.format(file_name), 'Content-Type': 'text/plain'} - def get_headers(): - return headers - def get_header(name, default=None): - return headers.get(name, default) + headers_dict = { + 'with_filename': {'Content-Disposition': 'attachment; filename={}'.format(file_name), 'Content-Type': 'text/plain'}, + 'no_filename': {'Content-Disposition': 'attachment;', 'Content-Type': 'text/plain'} + } + def get_headers(*args): + return args file_data = ( "You are reading text file that was supposed to be downloaded\r\n" "to your hard disk. If your browser offered to save you the file," "\r\nthen it handled the Content-Disposition header correctly." ) - http_response = HTTPResponse( - status=200, - reason='OK', - data=file_data, - getheaders=get_headers, - getheader=get_header - ) - # deserialize response to a file - mock_response = RESTResponse(http_response) - with patch.object(RESTClientObject, 'request') as mock_method: - mock_method.return_value = mock_response - try: - file_object = self.api.download_attachment(file_name='download-text') - self.assert_request_called_with( - mock_method, - 'http://www.jtricks.com/download-text', - http_method='GET', - accept='text/plain', - content_type=None, - ) - self.assertTrue(isinstance(file_object, file_type)) - self.assertFalse(file_object.closed) - self.assertEqual(file_object.read(), file_data.encode('utf-8')) - finally: - file_object.close() - os.unlink(file_object.name) + for key, headers in headers_dict.items(): + def get_header(name, default=None): + return headers_dict[key].get(name, default) + http_response = HTTPResponse( + status=200, + reason='OK', + data=file_data, + getheaders=get_headers(headers), + getheader=get_header + ) + # deserialize response to a file + mock_response = RESTResponse(http_response) + with patch.object(RESTClientObject, 'request') as mock_method: + mock_method.return_value = mock_response + try: + file_object = self.api.download_attachment(file_name='download-text') + self.assert_request_called_with( + mock_method, + 'http://www.jtricks.com/download-text', + http_method='GET', + accept='text/plain', + content_type=None, + ) + self.assertTrue(isinstance(file_object, file_type)) + self.assertFalse(file_object.closed) + self.assertEqual(file_object.read(), file_data.encode('utf-8')) + finally: + file_object.close() + os.unlink(file_object.name) def test_upload_download_file(self): test_file_dir = os.path.realpath(