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 08de3bab85e9..644ba9e560f6 100644 --- a/modules/openapi-generator/src/main/resources/python/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/python/api_client.mustache @@ -63,7 +63,7 @@ class ApiClient(object): def __init__(self, configuration=None, header_name=None, header_value=None, cookie=None, pool_threads=1): if configuration is None: - configuration = Configuration() + configuration = Configuration.get_instance() self.configuration = configuration self.pool_threads = pool_threads diff --git a/modules/openapi-generator/src/main/resources/python/configuration.mustache b/modules/openapi-generator/src/main/resources/python/configuration.mustache index 0645bc46755a..7acc98201c01 100644 --- a/modules/openapi-generator/src/main/resources/python/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/python/configuration.mustache @@ -4,6 +4,7 @@ from __future__ import absolute_import +import copy import logging {{^asyncio}} import multiprocessing @@ -117,6 +118,8 @@ class Configuration(object): {{/hasAuthMethods}} """ + _default = None + def __init__(self, host="{{{basePath}}}", api_key=None, api_key_prefix=None, username=None, password=None, @@ -241,6 +244,31 @@ class Configuration(object): # Disable client side validation self.client_side_validation = True + @classmethod + def set_default(cls, default): + """Set default instance of configuration. + + It stores default configuration, which can be + returned by get_instance method. + + :param default: object of Configuration + """ + cls._default = copy.copy(default) + + @classmethod + def get_instance(cls): + """Return new instance of configuration. + + This method returns newly created, based on default constructor, + object of Configuration class or returns a copy of default + configuration passed by the set_default method. + + :return: The configuration object. + """ + if cls._default is not None: + return copy.copy(cls._default) + return Configuration() + @property def logger_file(self): """The logger file. diff --git a/samples/client/petstore/python-asyncio/petstore_api/api_client.py b/samples/client/petstore/python-asyncio/petstore_api/api_client.py index 7b8cb7dc239a..40427f35dbd4 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/api_client.py +++ b/samples/client/petstore/python-asyncio/petstore_api/api_client.py @@ -68,7 +68,7 @@ class ApiClient(object): def __init__(self, configuration=None, header_name=None, header_value=None, cookie=None, pool_threads=1): if configuration is None: - configuration = Configuration() + configuration = Configuration.get_instance() self.configuration = configuration self.pool_threads = pool_threads diff --git a/samples/client/petstore/python-asyncio/petstore_api/configuration.py b/samples/client/petstore/python-asyncio/petstore_api/configuration.py index 82fd3361968c..d81ea525f3d1 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/configuration.py +++ b/samples/client/petstore/python-asyncio/petstore_api/configuration.py @@ -12,6 +12,7 @@ from __future__ import absolute_import +import copy import logging import sys import urllib3 @@ -71,6 +72,8 @@ class Configuration(object): ) """ + _default = None + def __init__(self, host="http://petstore.swagger.io:80/v2", api_key=None, api_key_prefix=None, username=None, password=None, @@ -165,6 +168,31 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", # Disable client side validation self.client_side_validation = True + @classmethod + def set_default(cls, default): + """Set default instance of configuration. + + It stores default configuration, which can be + returned by get_instance method. + + :param default: object of Configuration + """ + cls._default = copy.copy(default) + + @classmethod + def get_instance(cls): + """Return new instance of configuration. + + This method returns newly created, based on default constructor, + object of Configuration class or returns a copy of default + configuration passed by the set_default method. + + :return: The configuration object. + """ + if cls._default is not None: + return copy.copy(cls._default) + return Configuration() + @property def logger_file(self): """The logger file. diff --git a/samples/client/petstore/python-experimental/petstore_api/configuration.py b/samples/client/petstore/python-experimental/petstore_api/configuration.py index faadfd56055b..d5abdf131b2d 100644 --- a/samples/client/petstore/python-experimental/petstore_api/configuration.py +++ b/samples/client/petstore/python-experimental/petstore_api/configuration.py @@ -12,6 +12,7 @@ from __future__ import absolute_import +import copy import logging import multiprocessing import sys @@ -72,6 +73,8 @@ class Configuration(object): ) """ + _default = None + def __init__(self, host="http://petstore.swagger.io:80/v2", api_key=None, api_key_prefix=None, username=None, password=None, @@ -169,6 +172,31 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", # Disable client side validation self.client_side_validation = True + @classmethod + def set_default(cls, default): + """Set default instance of configuration. + + It stores default configuration, which can be + returned by get_instance method. + + :param default: object of Configuration + """ + cls._default = copy.copy(default) + + @classmethod + def get_instance(cls): + """Return new instance of configuration. + + This method returns newly created, based on default constructor, + object of Configuration class or returns a copy of default + configuration passed by the set_default method. + + :return: The configuration object. + """ + if cls._default is not None: + return copy.copy(cls._default) + return Configuration() + @property def logger_file(self): """The logger file. diff --git a/samples/client/petstore/python-tornado/petstore_api/api_client.py b/samples/client/petstore/python-tornado/petstore_api/api_client.py index dde7bac90f47..e07b586be7c2 100644 --- a/samples/client/petstore/python-tornado/petstore_api/api_client.py +++ b/samples/client/petstore/python-tornado/petstore_api/api_client.py @@ -69,7 +69,7 @@ class ApiClient(object): def __init__(self, configuration=None, header_name=None, header_value=None, cookie=None, pool_threads=1): if configuration is None: - configuration = Configuration() + configuration = Configuration.get_instance() self.configuration = configuration self.pool_threads = pool_threads diff --git a/samples/client/petstore/python-tornado/petstore_api/configuration.py b/samples/client/petstore/python-tornado/petstore_api/configuration.py index faadfd56055b..d5abdf131b2d 100644 --- a/samples/client/petstore/python-tornado/petstore_api/configuration.py +++ b/samples/client/petstore/python-tornado/petstore_api/configuration.py @@ -12,6 +12,7 @@ from __future__ import absolute_import +import copy import logging import multiprocessing import sys @@ -72,6 +73,8 @@ class Configuration(object): ) """ + _default = None + def __init__(self, host="http://petstore.swagger.io:80/v2", api_key=None, api_key_prefix=None, username=None, password=None, @@ -169,6 +172,31 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", # Disable client side validation self.client_side_validation = True + @classmethod + def set_default(cls, default): + """Set default instance of configuration. + + It stores default configuration, which can be + returned by get_instance method. + + :param default: object of Configuration + """ + cls._default = copy.copy(default) + + @classmethod + def get_instance(cls): + """Return new instance of configuration. + + This method returns newly created, based on default constructor, + object of Configuration class or returns a copy of default + configuration passed by the set_default method. + + :return: The configuration object. + """ + if cls._default is not None: + return copy.copy(cls._default) + return Configuration() + @property def logger_file(self): """The logger file. diff --git a/samples/client/petstore/python/petstore_api/api_client.py b/samples/client/petstore/python/petstore_api/api_client.py index aec97a99e067..912b0f40ab70 100644 --- a/samples/client/petstore/python/petstore_api/api_client.py +++ b/samples/client/petstore/python/petstore_api/api_client.py @@ -68,7 +68,7 @@ class ApiClient(object): def __init__(self, configuration=None, header_name=None, header_value=None, cookie=None, pool_threads=1): if configuration is None: - configuration = Configuration() + configuration = Configuration.get_instance() self.configuration = configuration self.pool_threads = pool_threads diff --git a/samples/client/petstore/python/petstore_api/configuration.py b/samples/client/petstore/python/petstore_api/configuration.py index faadfd56055b..d5abdf131b2d 100644 --- a/samples/client/petstore/python/petstore_api/configuration.py +++ b/samples/client/petstore/python/petstore_api/configuration.py @@ -12,6 +12,7 @@ from __future__ import absolute_import +import copy import logging import multiprocessing import sys @@ -72,6 +73,8 @@ class Configuration(object): ) """ + _default = None + def __init__(self, host="http://petstore.swagger.io:80/v2", api_key=None, api_key_prefix=None, username=None, password=None, @@ -169,6 +172,31 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", # Disable client side validation self.client_side_validation = True + @classmethod + def set_default(cls, default): + """Set default instance of configuration. + + It stores default configuration, which can be + returned by get_instance method. + + :param default: object of Configuration + """ + cls._default = copy.copy(default) + + @classmethod + def get_instance(cls): + """Return new instance of configuration. + + This method returns newly created, based on default constructor, + object of Configuration class or returns a copy of default + configuration passed by the set_default method. + + :return: The configuration object. + """ + if cls._default is not None: + return copy.copy(cls._default) + return Configuration() + @property def logger_file(self): """The logger file. diff --git a/samples/client/petstore/python/tests/test_configuration.py b/samples/client/petstore/python/tests/test_configuration.py index f1074b17e70a..d22148e56bbc 100644 --- a/samples/client/petstore/python/tests/test_configuration.py +++ b/samples/client/petstore/python/tests/test_configuration.py @@ -22,14 +22,25 @@ def setUp(self): pass def tearDown(self): - pass + # reset Configuration + petstore_api.Configuration.set_default(None) def testConfiguration(self): # check that different instances use different dictionaries c1 = petstore_api.Configuration() c2 = petstore_api.Configuration() - assert id(c1.api_key) != id(c2.api_key) - assert id(c1.api_key_prefix) != id(c2.api_key_prefix) + self.assertNotEqual(id(c1.api_key), id(c2.api_key)) + self.assertNotEqual(id(c1.api_key_prefix), id(c2.api_key_prefix)) + + def testDefaultConfiguration(self): + + # prepare default configuration + c1 = petstore_api.Configuration(host="example.com") + petstore_api.Configuration.set_default(c1) + + # get default configuration + c2 = petstore_api.Configuration.get_instance() + self.assertEqual(c2.host, "example.com") if __name__ == '__main__': diff --git a/samples/openapi3/client/petstore/python-experimental/petstore_api/configuration.py b/samples/openapi3/client/petstore/python-experimental/petstore_api/configuration.py index cce89b13acae..9735b4abc98c 100644 --- a/samples/openapi3/client/petstore/python-experimental/petstore_api/configuration.py +++ b/samples/openapi3/client/petstore/python-experimental/petstore_api/configuration.py @@ -12,6 +12,7 @@ from __future__ import absolute_import +import copy import logging import multiprocessing import sys @@ -113,6 +114,8 @@ class Configuration(object): ) """ + _default = None + def __init__(self, host="http://petstore.swagger.io:80/v2", api_key=None, api_key_prefix=None, username=None, password=None, @@ -216,6 +219,31 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", # Disable client side validation self.client_side_validation = True + @classmethod + def set_default(cls, default): + """Set default instance of configuration. + + It stores default configuration, which can be + returned by get_instance method. + + :param default: object of Configuration + """ + cls._default = copy.copy(default) + + @classmethod + def get_instance(cls): + """Return new instance of configuration. + + This method returns newly created, based on default constructor, + object of Configuration class or returns a copy of default + configuration passed by the set_default method. + + :return: The configuration object. + """ + if cls._default is not None: + return copy.copy(cls._default) + return Configuration() + @property def logger_file(self): """The logger file. 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 aec97a99e067..912b0f40ab70 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/api_client.py +++ b/samples/openapi3/client/petstore/python/petstore_api/api_client.py @@ -68,7 +68,7 @@ class ApiClient(object): def __init__(self, configuration=None, header_name=None, header_value=None, cookie=None, pool_threads=1): if configuration is None: - configuration = Configuration() + configuration = Configuration.get_instance() self.configuration = configuration self.pool_threads = pool_threads diff --git a/samples/openapi3/client/petstore/python/petstore_api/configuration.py b/samples/openapi3/client/petstore/python/petstore_api/configuration.py index 131534656e34..e662855a3a62 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/configuration.py +++ b/samples/openapi3/client/petstore/python/petstore_api/configuration.py @@ -12,6 +12,7 @@ from __future__ import absolute_import +import copy import logging import multiprocessing import sys @@ -72,6 +73,8 @@ class Configuration(object): ) """ + _default = None + def __init__(self, host="http://petstore.swagger.io:80/v2", api_key=None, api_key_prefix=None, username=None, password=None, @@ -169,6 +172,31 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", # Disable client side validation self.client_side_validation = True + @classmethod + def set_default(cls, default): + """Set default instance of configuration. + + It stores default configuration, which can be + returned by get_instance method. + + :param default: object of Configuration + """ + cls._default = copy.copy(default) + + @classmethod + def get_instance(cls): + """Return new instance of configuration. + + This method returns newly created, based on default constructor, + object of Configuration class or returns a copy of default + configuration passed by the set_default method. + + :return: The configuration object. + """ + if cls._default is not None: + return copy.copy(cls._default) + return Configuration() + @property def logger_file(self): """The logger file.