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 4d1d789e4e8..c7d4a6d700f 100644 --- a/modules/openapi-generator/src/main/resources/python/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/python/api_client.mustache @@ -232,7 +232,7 @@ class ApiClient: body = self.sanitize_for_serialization(body) # request url - if _host is None: + if _host is None or self.configuration.ignore_operation_servers: url = self.configuration.host + resource_path else: # use server/host defined in path or operation instead diff --git a/modules/openapi-generator/src/main/resources/python/configuration.mustache b/modules/openapi-generator/src/main/resources/python/configuration.mustache index c50c6fe3fdf..8b9a6f4854e 100644 --- a/modules/openapi-generator/src/main/resources/python/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/python/configuration.mustache @@ -24,6 +24,9 @@ class Configuration: """This class contains various settings of the API client. :param host: Base url. + :param ignore_operation_servers + Boolean to ignore operation servers for the API client. + Config will use `host` as the base url regardless of the operation servers. :param api_key: Dict to store API key(s). Each entry in the dict specifies an API key. The dict key is the name of the security scheme in the OAS specification. @@ -148,6 +151,7 @@ conf = {{{packageName}}}.Configuration( {{/hasHttpSignatureMethods}} server_index=None, server_variables=None, server_operation_index=None, server_operation_variables=None, + ignore_operation_servers=False, ssl_ca_cert=None, retries=None, *, @@ -166,6 +170,9 @@ conf = {{{packageName}}}.Configuration( self.server_operation_variables = server_operation_variables or {} """Default server variables """ + self.ignore_operation_servers = ignore_operation_servers + """Ignore operation servers + """ self.temp_folder_path = None """Temp file folder for downloading files """ 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 2d5d7b55ad7..feef534a2f2 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 @@ -228,7 +228,7 @@ class ApiClient: body = self.sanitize_for_serialization(body) # request url - if _host is None: + if _host is None or self.configuration.ignore_operation_servers: url = self.configuration.host + resource_path else: # use server/host defined in path or operation instead diff --git a/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/configuration.py b/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/configuration.py index 0966e566417..27946de1787 100644 --- a/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/configuration.py +++ b/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/configuration.py @@ -33,6 +33,9 @@ class Configuration: """This class contains various settings of the API client. :param host: Base url. + :param ignore_operation_servers + Boolean to ignore operation servers for the API client. + Config will use `host` as the base url regardless of the operation servers. :param api_key: Dict to store API key(s). Each entry in the dict specifies an API key. The dict key is the name of the security scheme in the OAS specification. @@ -84,6 +87,7 @@ conf = openapi_client.Configuration( access_token=None, server_index=None, server_variables=None, server_operation_index=None, server_operation_variables=None, + ignore_operation_servers=False, ssl_ca_cert=None, retries=None, *, @@ -102,6 +106,9 @@ conf = openapi_client.Configuration( self.server_operation_variables = server_operation_variables or {} """Default server variables """ + self.ignore_operation_servers = ignore_operation_servers + """Ignore operation servers + """ self.temp_folder_path = None """Temp file folder for downloading files """ 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 2d5d7b55ad7..feef534a2f2 100644 --- a/samples/client/echo_api/python/openapi_client/api_client.py +++ b/samples/client/echo_api/python/openapi_client/api_client.py @@ -228,7 +228,7 @@ class ApiClient: body = self.sanitize_for_serialization(body) # request url - if _host is None: + if _host is None or self.configuration.ignore_operation_servers: url = self.configuration.host + resource_path else: # use server/host defined in path or operation instead diff --git a/samples/client/echo_api/python/openapi_client/configuration.py b/samples/client/echo_api/python/openapi_client/configuration.py index 0966e566417..27946de1787 100644 --- a/samples/client/echo_api/python/openapi_client/configuration.py +++ b/samples/client/echo_api/python/openapi_client/configuration.py @@ -33,6 +33,9 @@ class Configuration: """This class contains various settings of the API client. :param host: Base url. + :param ignore_operation_servers + Boolean to ignore operation servers for the API client. + Config will use `host` as the base url regardless of the operation servers. :param api_key: Dict to store API key(s). Each entry in the dict specifies an API key. The dict key is the name of the security scheme in the OAS specification. @@ -84,6 +87,7 @@ conf = openapi_client.Configuration( access_token=None, server_index=None, server_variables=None, server_operation_index=None, server_operation_variables=None, + ignore_operation_servers=False, ssl_ca_cert=None, retries=None, *, @@ -102,6 +106,9 @@ conf = openapi_client.Configuration( self.server_operation_variables = server_operation_variables or {} """Default server variables """ + self.ignore_operation_servers = ignore_operation_servers + """Ignore operation servers + """ self.temp_folder_path = None """Temp file folder for downloading files """ 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 5766c284223..302e5dac53c 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 @@ -230,7 +230,7 @@ class ApiClient: body = self.sanitize_for_serialization(body) # request url - if _host is None: + if _host is None or self.configuration.ignore_operation_servers: url = self.configuration.host + resource_path else: # use server/host defined in path or operation instead diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/configuration.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/configuration.py index 096957ab701..85c2e738c4e 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/configuration.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/configuration.py @@ -31,6 +31,9 @@ class Configuration: """This class contains various settings of the API client. :param host: Base url. + :param ignore_operation_servers + Boolean to ignore operation servers for the API client. + Config will use `host` as the base url regardless of the operation servers. :param api_key: Dict to store API key(s). Each entry in the dict specifies an API key. The dict key is the name of the security scheme in the OAS specification. @@ -143,6 +146,7 @@ conf = petstore_api.Configuration( signing_info=None, server_index=None, server_variables=None, server_operation_index=None, server_operation_variables=None, + ignore_operation_servers=False, ssl_ca_cert=None, retries=None, *, @@ -161,6 +165,9 @@ conf = petstore_api.Configuration( self.server_operation_variables = server_operation_variables or {} """Default server variables """ + self.ignore_operation_servers = ignore_operation_servers + """Ignore operation servers + """ self.temp_folder_path = None """Temp file folder for downloading files """ diff --git a/samples/openapi3/client/petstore/python-aiohttp/tests/test_api_client.py b/samples/openapi3/client/petstore/python-aiohttp/tests/test_api_client.py index d3daa10d51c..fbcd7485a9c 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/tests/test_api_client.py +++ b/samples/openapi3/client/petstore/python-aiohttp/tests/test_api_client.py @@ -8,6 +8,7 @@ import weakref from tests.util import async_test import petstore_api +HOST = 'http://localhost/v2' class TestApiClient(unittest.TestCase): @@ -20,3 +21,31 @@ class TestApiClient(unittest.TestCase): rest_pool_ref = client.rest_client.pool_manager self.assertTrue(rest_pool_ref.closed) + + @async_test + async def test_ignore_operation_servers(self): + config = petstore_api.Configuration(host=HOST) + client = petstore_api.ApiClient(config) + user_api_instance = petstore_api.api.user_api.UserApi(client) + + config_ignore = petstore_api.Configuration(host=HOST, ignore_operation_servers=True) + client_ignore = petstore_api.ApiClient(config_ignore) + user_api_instance_ignore = petstore_api.api.user_api.UserApi(client_ignore) + + params_to_serialize = { + 'user': petstore_api.User(id=1, username='test'), + '_request_auth': None, + '_content_type': 'application/json', + '_headers': None, + '_host_index': 0 + } + + # operation servers should be used + _, url, *_ = user_api_instance._create_user_serialize(**params_to_serialize) + self.assertEqual(client.configuration.host, HOST) + self.assertEqual(url, 'http://petstore.swagger.io/v2/user') + + # operation servers should be ignored + _, url_ignore, *_ = user_api_instance_ignore._create_user_serialize(**params_to_serialize) + self.assertEqual(client.configuration.host, HOST) + self.assertEqual(url_ignore, HOST + '/user') 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 c4dbcb450a7..9ab0756d53b 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/api_client.py +++ b/samples/openapi3/client/petstore/python/petstore_api/api_client.py @@ -227,7 +227,7 @@ class ApiClient: body = self.sanitize_for_serialization(body) # request url - if _host is None: + if _host is None or self.configuration.ignore_operation_servers: url = self.configuration.host + resource_path else: # use server/host defined in path or operation instead diff --git a/samples/openapi3/client/petstore/python/petstore_api/configuration.py b/samples/openapi3/client/petstore/python/petstore_api/configuration.py index 9739ca72f44..a9c837f3f35 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/configuration.py +++ b/samples/openapi3/client/petstore/python/petstore_api/configuration.py @@ -32,6 +32,9 @@ class Configuration: """This class contains various settings of the API client. :param host: Base url. + :param ignore_operation_servers + Boolean to ignore operation servers for the API client. + Config will use `host` as the base url regardless of the operation servers. :param api_key: Dict to store API key(s). Each entry in the dict specifies an API key. The dict key is the name of the security scheme in the OAS specification. @@ -144,6 +147,7 @@ conf = petstore_api.Configuration( signing_info=None, server_index=None, server_variables=None, server_operation_index=None, server_operation_variables=None, + ignore_operation_servers=False, ssl_ca_cert=None, retries=None, *, @@ -162,6 +166,9 @@ conf = petstore_api.Configuration( self.server_operation_variables = server_operation_variables or {} """Default server variables """ + self.ignore_operation_servers = ignore_operation_servers + """Ignore operation servers + """ self.temp_folder_path = None """Temp file folder for downloading files """ diff --git a/samples/openapi3/client/petstore/python/tests/test_api_client.py b/samples/openapi3/client/petstore/python/tests/test_api_client.py index 1d8ffebaca0..e80bc135296 100644 --- a/samples/openapi3/client/petstore/python/tests/test_api_client.py +++ b/samples/openapi3/client/petstore/python/tests/test_api_client.py @@ -56,6 +56,33 @@ class ApiClientTests(unittest.TestCase): self.assertEqual('test_username', client.configuration.username) self.assertEqual('test_password', client.configuration.password) + def test_ignore_operation_servers(self): + config = petstore_api.Configuration(host=HOST) + client = petstore_api.ApiClient(config) + user_api_instance = petstore_api.api.user_api.UserApi(client) + + config_ignore = petstore_api.Configuration(host=HOST, ignore_operation_servers=True) + client_ignore = petstore_api.ApiClient(config_ignore) + user_api_instance_ignore = petstore_api.api.user_api.UserApi(client_ignore) + + params_to_serialize = { + 'user': petstore_api.User(id=1, username='test'), + '_request_auth': None, + '_content_type': 'application/json', + '_headers': None, + '_host_index': 0 + } + + # operation servers should be used + _, url, *_ = user_api_instance._create_user_serialize(**params_to_serialize) + self.assertEqual(client.configuration.host, HOST) + self.assertEqual(url, 'http://petstore.swagger.io/v2/user') + + # operation servers should be ignored + _, url_ignore, *_ = user_api_instance_ignore._create_user_serialize(**params_to_serialize) + self.assertEqual(client.configuration.host, HOST) + self.assertEqual(url_ignore, HOST + '/user') + def test_select_header_accept(self): accepts = ['APPLICATION/JSON', 'APPLICATION/XML'] accept = self.api_client.select_header_accept(accepts) diff --git a/samples/openapi3/client/petstore/python/tests/test_configuration.py b/samples/openapi3/client/petstore/python/tests/test_configuration.py index 3ff15aaa4c6..af9fa6eef15 100644 --- a/samples/openapi3/client/petstore/python/tests/test_configuration.py +++ b/samples/openapi3/client/petstore/python/tests/test_configuration.py @@ -58,6 +58,18 @@ class TestConfiguration(unittest.TestCase): c1 = petstore_api.Configuration(access_token="12345") self.assertEqual(c1.access_token, "12345") + def test_ignore_operation_servers(self): + self.config.ignore_operation_servers = True + self.assertTrue(self.config.ignore_operation_servers) + self.config.ignore_operation_servers = False + self.assertFalse(self.config.ignore_operation_servers) + + c1 = petstore_api.Configuration(ignore_operation_servers=True) + self.assertTrue(c1.ignore_operation_servers) + + c2 = petstore_api.Configuration() + self.assertFalse(c2.ignore_operation_servers) + def test_get_host_settings(self): host_settings = self.config.get_host_settings()