From 70a585471e4d5f12233d68afc5b00b0be62376b9 Mon Sep 17 00:00:00 2001 From: Tomasz Prus Date: Fri, 14 Feb 2020 01:09:29 +0100 Subject: [PATCH 1/5] [python] add method to set/get default configuration --- .../main/resources/python/api_client.mustache | 2 +- .../resources/python/configuration.mustache | 28 +++++++++++++++++++ .../python-asyncio/petstore_api/api_client.py | 2 +- .../petstore_api/configuration.py | 28 +++++++++++++++++++ .../petstore_api/configuration.py | 28 +++++++++++++++++++ .../python-tornado/petstore_api/api_client.py | 2 +- .../petstore_api/configuration.py | 28 +++++++++++++++++++ .../python/petstore_api/api_client.py | 2 +- .../python/petstore_api/configuration.py | 28 +++++++++++++++++++ .../python/tests/test_configuration.py | 17 +++++++++-- .../petstore_api/configuration.py | 28 +++++++++++++++++++ .../python/petstore_api/api_client.py | 2 +- .../python/petstore_api/configuration.py | 28 +++++++++++++++++++ 13 files changed, 215 insertions(+), 8 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 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. From 2a4c72628af6e3ebb9bcbb88d49e485fea0326dd Mon Sep 17 00:00:00 2001 From: Tomasz Prus Date: Sat, 22 Feb 2020 00:59:26 +0100 Subject: [PATCH 2/5] [python] change method name and fix handling api_key --- .../main/resources/python/api_client.mustache | 2 +- .../resources/python/configuration.mustache | 17 +++++++++++++---- .../python-asyncio/petstore_api/api_client.py | 2 +- .../petstore_api/configuration.py | 17 +++++++++++++---- .../petstore_api/configuration.py | 17 +++++++++++++---- .../python-tornado/petstore_api/api_client.py | 2 +- .../petstore_api/configuration.py | 17 +++++++++++++---- .../petstore/python/petstore_api/api_client.py | 2 +- .../python/petstore_api/configuration.py | 17 +++++++++++++---- .../petstore/python/tests/test_configuration.py | 5 ++++- 10 files changed, 73 insertions(+), 25 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 644ba9e560f6..9b359c12ff31 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.get_instance() + configuration = Configuration.new_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 7acc98201c01..1a0d70889cb3 100644 --- a/modules/openapi-generator/src/main/resources/python/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/python/configuration.mustache @@ -244,19 +244,28 @@ class Configuration(object): # Disable client side validation self.client_side_validation = True + @classmethod + def copy(cls, source): + if source is None: + return None + ret = copy.copy(source) + ret.api_key = copy.copy(source.api_key) + ret.api_key_prefix = copy.copy(source.api_key_prefix) + return ret + @classmethod def set_default(cls, default): """Set default instance of configuration. It stores default configuration, which can be - returned by get_instance method. + returned by new_instance method. :param default: object of Configuration """ - cls._default = copy.copy(default) + cls._default = Configuration.copy(default) @classmethod - def get_instance(cls): + def new_instance(cls): """Return new instance of configuration. This method returns newly created, based on default constructor, @@ -266,7 +275,7 @@ class Configuration(object): :return: The configuration object. """ if cls._default is not None: - return copy.copy(cls._default) + return Configuration.copy(cls._default) return Configuration() @property 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 40427f35dbd4..15a82b8509b9 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.get_instance() + configuration = Configuration.new_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 d81ea525f3d1..f5b64f52914a 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/configuration.py +++ b/samples/client/petstore/python-asyncio/petstore_api/configuration.py @@ -168,19 +168,28 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", # Disable client side validation self.client_side_validation = True + @classmethod + def copy(cls, source): + if source is None: + return None + ret = copy.copy(source) + ret.api_key = copy.copy(source.api_key) + ret.api_key_prefix = copy.copy(source.api_key_prefix) + return ret + @classmethod def set_default(cls, default): """Set default instance of configuration. It stores default configuration, which can be - returned by get_instance method. + returned by new_instance method. :param default: object of Configuration """ - cls._default = copy.copy(default) + cls._default = Configuration.copy(default) @classmethod - def get_instance(cls): + def new_instance(cls): """Return new instance of configuration. This method returns newly created, based on default constructor, @@ -190,7 +199,7 @@ def get_instance(cls): :return: The configuration object. """ if cls._default is not None: - return copy.copy(cls._default) + return Configuration.copy(cls._default) return Configuration() @property diff --git a/samples/client/petstore/python-experimental/petstore_api/configuration.py b/samples/client/petstore/python-experimental/petstore_api/configuration.py index d5abdf131b2d..1f03b39e4fa9 100644 --- a/samples/client/petstore/python-experimental/petstore_api/configuration.py +++ b/samples/client/petstore/python-experimental/petstore_api/configuration.py @@ -172,19 +172,28 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", # Disable client side validation self.client_side_validation = True + @classmethod + def copy(cls, source): + if source is None: + return None + ret = copy.copy(source) + ret.api_key = copy.copy(source.api_key) + ret.api_key_prefix = copy.copy(source.api_key_prefix) + return ret + @classmethod def set_default(cls, default): """Set default instance of configuration. It stores default configuration, which can be - returned by get_instance method. + returned by new_instance method. :param default: object of Configuration """ - cls._default = copy.copy(default) + cls._default = Configuration.copy(default) @classmethod - def get_instance(cls): + def new_instance(cls): """Return new instance of configuration. This method returns newly created, based on default constructor, @@ -194,7 +203,7 @@ def get_instance(cls): :return: The configuration object. """ if cls._default is not None: - return copy.copy(cls._default) + return Configuration.copy(cls._default) return Configuration() @property 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 e07b586be7c2..ab3631b353ea 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.get_instance() + configuration = Configuration.new_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 d5abdf131b2d..1f03b39e4fa9 100644 --- a/samples/client/petstore/python-tornado/petstore_api/configuration.py +++ b/samples/client/petstore/python-tornado/petstore_api/configuration.py @@ -172,19 +172,28 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", # Disable client side validation self.client_side_validation = True + @classmethod + def copy(cls, source): + if source is None: + return None + ret = copy.copy(source) + ret.api_key = copy.copy(source.api_key) + ret.api_key_prefix = copy.copy(source.api_key_prefix) + return ret + @classmethod def set_default(cls, default): """Set default instance of configuration. It stores default configuration, which can be - returned by get_instance method. + returned by new_instance method. :param default: object of Configuration """ - cls._default = copy.copy(default) + cls._default = Configuration.copy(default) @classmethod - def get_instance(cls): + def new_instance(cls): """Return new instance of configuration. This method returns newly created, based on default constructor, @@ -194,7 +203,7 @@ def get_instance(cls): :return: The configuration object. """ if cls._default is not None: - return copy.copy(cls._default) + return Configuration.copy(cls._default) return Configuration() @property diff --git a/samples/client/petstore/python/petstore_api/api_client.py b/samples/client/petstore/python/petstore_api/api_client.py index 912b0f40ab70..205c1db93e7a 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.get_instance() + configuration = Configuration.new_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 d5abdf131b2d..1f03b39e4fa9 100644 --- a/samples/client/petstore/python/petstore_api/configuration.py +++ b/samples/client/petstore/python/petstore_api/configuration.py @@ -172,19 +172,28 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", # Disable client side validation self.client_side_validation = True + @classmethod + def copy(cls, source): + if source is None: + return None + ret = copy.copy(source) + ret.api_key = copy.copy(source.api_key) + ret.api_key_prefix = copy.copy(source.api_key_prefix) + return ret + @classmethod def set_default(cls, default): """Set default instance of configuration. It stores default configuration, which can be - returned by get_instance method. + returned by new_instance method. :param default: object of Configuration """ - cls._default = copy.copy(default) + cls._default = Configuration.copy(default) @classmethod - def get_instance(cls): + def new_instance(cls): """Return new instance of configuration. This method returns newly created, based on default constructor, @@ -194,7 +203,7 @@ def get_instance(cls): :return: The configuration object. """ if cls._default is not None: - return copy.copy(cls._default) + return Configuration.copy(cls._default) return Configuration() @property diff --git a/samples/client/petstore/python/tests/test_configuration.py b/samples/client/petstore/python/tests/test_configuration.py index d22148e56bbc..eec9566e22d6 100644 --- a/samples/client/petstore/python/tests/test_configuration.py +++ b/samples/client/petstore/python/tests/test_configuration.py @@ -39,9 +39,12 @@ def testDefaultConfiguration(self): petstore_api.Configuration.set_default(c1) # get default configuration - c2 = petstore_api.Configuration.get_instance() + c2 = petstore_api.Configuration.new_instance() self.assertEqual(c2.host, "example.com") + self.assertNotEqual(id(c1.api_key), id(c2.api_key)) + self.assertNotEqual(id(c1.api_key_prefix), id(c2.api_key_prefix)) + if __name__ == '__main__': unittest.main() From be04fb9a0fd1d1f5bb61ebf9272fb2cb6f0162e9 Mon Sep 17 00:00:00 2001 From: Tomasz Prus Date: Sun, 23 Feb 2020 00:20:42 +0100 Subject: [PATCH 3/5] python: using modified deepcopy to set/get default configuration --- .../main/resources/python/api_client.mustache | 2 +- .../resources/python/configuration.mustache | 29 +++++++++++-------- .../python-asyncio/petstore_api/api_client.py | 2 +- .../petstore_api/configuration.py | 29 +++++++++++-------- .../petstore_api/configuration.py | 29 +++++++++++-------- .../python-tornado/petstore_api/api_client.py | 2 +- .../petstore_api/configuration.py | 29 +++++++++++-------- .../python/petstore_api/api_client.py | 2 +- .../python/petstore_api/configuration.py | 29 +++++++++++-------- .../python/tests/test_configuration.py | 4 ++- 10 files changed, 92 insertions(+), 65 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 9b359c12ff31..52b2ec2f3d9a 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.new_instance() + configuration = Configuration.get_default_copy() 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 1a0d70889cb3..f0bb8072a080 100644 --- a/modules/openapi-generator/src/main/resources/python/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/python/configuration.mustache @@ -244,28 +244,33 @@ class Configuration(object): # Disable client side validation self.client_side_validation = True - @classmethod - def copy(cls, source): - if source is None: - return None - ret = copy.copy(source) - ret.api_key = copy.copy(source.api_key) - ret.api_key_prefix = copy.copy(source.api_key_prefix) - return ret + def __deepcopy__(self, memo): + cls = self.__class__ + result = cls.__new__(cls) + memo[id(self)] = result + for k, v in self.__dict__.items(): + if k not in ('logger', 'logger_file_handler'): + setattr(result, k, copy.deepcopy(v, memo)) + # shallow copy of loggers + result.logger = copy.copy(self.logger) + # use setters to configure loggers + result.logger_file = self.logger_file + result.debug = self.debug + return result @classmethod def set_default(cls, default): """Set default instance of configuration. It stores default configuration, which can be - returned by new_instance method. + returned by get_default_copy method. :param default: object of Configuration """ - cls._default = Configuration.copy(default) + cls._default = copy.deepcopy(default) @classmethod - def new_instance(cls): + def get_default_copy(cls): """Return new instance of configuration. This method returns newly created, based on default constructor, @@ -275,7 +280,7 @@ class Configuration(object): :return: The configuration object. """ if cls._default is not None: - return Configuration.copy(cls._default) + return copy.deepcopy(cls._default) return Configuration() @property 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 15a82b8509b9..4f33e8108061 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.new_instance() + configuration = Configuration.get_default_copy() 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 f5b64f52914a..da1f454b867d 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/configuration.py +++ b/samples/client/petstore/python-asyncio/petstore_api/configuration.py @@ -168,28 +168,33 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", # Disable client side validation self.client_side_validation = True - @classmethod - def copy(cls, source): - if source is None: - return None - ret = copy.copy(source) - ret.api_key = copy.copy(source.api_key) - ret.api_key_prefix = copy.copy(source.api_key_prefix) - return ret + def __deepcopy__(self, memo): + cls = self.__class__ + result = cls.__new__(cls) + memo[id(self)] = result + for k, v in self.__dict__.items(): + if k not in ('logger', 'logger_file_handler'): + setattr(result, k, copy.deepcopy(v, memo)) + # shallow copy of loggers + result.logger = copy.copy(self.logger) + # use setters to configure loggers + result.logger_file = self.logger_file + result.debug = self.debug + return result @classmethod def set_default(cls, default): """Set default instance of configuration. It stores default configuration, which can be - returned by new_instance method. + returned by get_default_copy method. :param default: object of Configuration """ - cls._default = Configuration.copy(default) + cls._default = copy.deepcopy(default) @classmethod - def new_instance(cls): + def get_default_copy(cls): """Return new instance of configuration. This method returns newly created, based on default constructor, @@ -199,7 +204,7 @@ def new_instance(cls): :return: The configuration object. """ if cls._default is not None: - return Configuration.copy(cls._default) + return copy.deepcopy(cls._default) return Configuration() @property diff --git a/samples/client/petstore/python-experimental/petstore_api/configuration.py b/samples/client/petstore/python-experimental/petstore_api/configuration.py index 1f03b39e4fa9..d17465dc2298 100644 --- a/samples/client/petstore/python-experimental/petstore_api/configuration.py +++ b/samples/client/petstore/python-experimental/petstore_api/configuration.py @@ -172,28 +172,33 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", # Disable client side validation self.client_side_validation = True - @classmethod - def copy(cls, source): - if source is None: - return None - ret = copy.copy(source) - ret.api_key = copy.copy(source.api_key) - ret.api_key_prefix = copy.copy(source.api_key_prefix) - return ret + def __deepcopy__(self, memo): + cls = self.__class__ + result = cls.__new__(cls) + memo[id(self)] = result + for k, v in self.__dict__.items(): + if k not in ('logger', 'logger_file_handler'): + setattr(result, k, copy.deepcopy(v, memo)) + # shallow copy of loggers + result.logger = copy.copy(self.logger) + # use setters to configure loggers + result.logger_file = self.logger_file + result.debug = self.debug + return result @classmethod def set_default(cls, default): """Set default instance of configuration. It stores default configuration, which can be - returned by new_instance method. + returned by get_default_copy method. :param default: object of Configuration """ - cls._default = Configuration.copy(default) + cls._default = copy.deepcopy(default) @classmethod - def new_instance(cls): + def get_default_copy(cls): """Return new instance of configuration. This method returns newly created, based on default constructor, @@ -203,7 +208,7 @@ def new_instance(cls): :return: The configuration object. """ if cls._default is not None: - return Configuration.copy(cls._default) + return copy.deepcopy(cls._default) return Configuration() @property 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 ab3631b353ea..6b9c738128fd 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.new_instance() + configuration = Configuration.get_default_copy() 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 1f03b39e4fa9..d17465dc2298 100644 --- a/samples/client/petstore/python-tornado/petstore_api/configuration.py +++ b/samples/client/petstore/python-tornado/petstore_api/configuration.py @@ -172,28 +172,33 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", # Disable client side validation self.client_side_validation = True - @classmethod - def copy(cls, source): - if source is None: - return None - ret = copy.copy(source) - ret.api_key = copy.copy(source.api_key) - ret.api_key_prefix = copy.copy(source.api_key_prefix) - return ret + def __deepcopy__(self, memo): + cls = self.__class__ + result = cls.__new__(cls) + memo[id(self)] = result + for k, v in self.__dict__.items(): + if k not in ('logger', 'logger_file_handler'): + setattr(result, k, copy.deepcopy(v, memo)) + # shallow copy of loggers + result.logger = copy.copy(self.logger) + # use setters to configure loggers + result.logger_file = self.logger_file + result.debug = self.debug + return result @classmethod def set_default(cls, default): """Set default instance of configuration. It stores default configuration, which can be - returned by new_instance method. + returned by get_default_copy method. :param default: object of Configuration """ - cls._default = Configuration.copy(default) + cls._default = copy.deepcopy(default) @classmethod - def new_instance(cls): + def get_default_copy(cls): """Return new instance of configuration. This method returns newly created, based on default constructor, @@ -203,7 +208,7 @@ def new_instance(cls): :return: The configuration object. """ if cls._default is not None: - return Configuration.copy(cls._default) + return copy.deepcopy(cls._default) return Configuration() @property diff --git a/samples/client/petstore/python/petstore_api/api_client.py b/samples/client/petstore/python/petstore_api/api_client.py index 205c1db93e7a..c0564c598f13 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.new_instance() + configuration = Configuration.get_default_copy() 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 1f03b39e4fa9..d17465dc2298 100644 --- a/samples/client/petstore/python/petstore_api/configuration.py +++ b/samples/client/petstore/python/petstore_api/configuration.py @@ -172,28 +172,33 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", # Disable client side validation self.client_side_validation = True - @classmethod - def copy(cls, source): - if source is None: - return None - ret = copy.copy(source) - ret.api_key = copy.copy(source.api_key) - ret.api_key_prefix = copy.copy(source.api_key_prefix) - return ret + def __deepcopy__(self, memo): + cls = self.__class__ + result = cls.__new__(cls) + memo[id(self)] = result + for k, v in self.__dict__.items(): + if k not in ('logger', 'logger_file_handler'): + setattr(result, k, copy.deepcopy(v, memo)) + # shallow copy of loggers + result.logger = copy.copy(self.logger) + # use setters to configure loggers + result.logger_file = self.logger_file + result.debug = self.debug + return result @classmethod def set_default(cls, default): """Set default instance of configuration. It stores default configuration, which can be - returned by new_instance method. + returned by get_default_copy method. :param default: object of Configuration """ - cls._default = Configuration.copy(default) + cls._default = copy.deepcopy(default) @classmethod - def new_instance(cls): + def get_default_copy(cls): """Return new instance of configuration. This method returns newly created, based on default constructor, @@ -203,7 +208,7 @@ def new_instance(cls): :return: The configuration object. """ if cls._default is not None: - return Configuration.copy(cls._default) + return copy.deepcopy(cls._default) return Configuration() @property diff --git a/samples/client/petstore/python/tests/test_configuration.py b/samples/client/petstore/python/tests/test_configuration.py index eec9566e22d6..d86666e4205a 100644 --- a/samples/client/petstore/python/tests/test_configuration.py +++ b/samples/client/petstore/python/tests/test_configuration.py @@ -36,11 +36,13 @@ def testDefaultConfiguration(self): # prepare default configuration c1 = petstore_api.Configuration(host="example.com") + c1.debug = True petstore_api.Configuration.set_default(c1) # get default configuration - c2 = petstore_api.Configuration.new_instance() + c2 = petstore_api.Configuration.get_default_copy() self.assertEqual(c2.host, "example.com") + self.assertTrue(c2.debug) self.assertNotEqual(id(c1.api_key), id(c2.api_key)) self.assertNotEqual(id(c1.api_key_prefix), id(c2.api_key_prefix)) From f99b59a16d90004e13315a9429aae8964a0d8d6b Mon Sep 17 00:00:00 2001 From: Tomasz Prus Date: Sun, 23 Feb 2020 10:29:38 +0100 Subject: [PATCH 4/5] python: update samples --- .../python/petstore_api/api_client.py | 2 +- .../python/petstore_api/configuration.py | 22 +++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) 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 912b0f40ab70..c0564c598f13 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.get_instance() + configuration = Configuration.get_default_copy() 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 e662855a3a62..379a951eb811 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/configuration.py +++ b/samples/openapi3/client/petstore/python/petstore_api/configuration.py @@ -172,19 +172,33 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", # Disable client side validation self.client_side_validation = True + def __deepcopy__(self, memo): + cls = self.__class__ + result = cls.__new__(cls) + memo[id(self)] = result + for k, v in self.__dict__.items(): + if k not in ('logger', 'logger_file_handler'): + setattr(result, k, copy.deepcopy(v, memo)) + # shallow copy of loggers + result.logger = copy.copy(self.logger) + # use setters to configure loggers + result.logger_file = self.logger_file + result.debug = self.debug + return result + @classmethod def set_default(cls, default): """Set default instance of configuration. It stores default configuration, which can be - returned by get_instance method. + returned by get_default_copy method. :param default: object of Configuration """ - cls._default = copy.copy(default) + cls._default = copy.deepcopy(default) @classmethod - def get_instance(cls): + def get_default_copy(cls): """Return new instance of configuration. This method returns newly created, based on default constructor, @@ -194,7 +208,7 @@ def get_instance(cls): :return: The configuration object. """ if cls._default is not None: - return copy.copy(cls._default) + return copy.deepcopy(cls._default) return Configuration() @property From f6bd708964bb243e3e9a9df87f70be96ebd75a34 Mon Sep 17 00:00:00 2001 From: Tomasz Prus Date: Sun, 23 Feb 2020 19:40:52 +0100 Subject: [PATCH 5/5] python: update samples --- .../petstore_api/configuration.py | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) 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 9735b4abc98c..c5d865e786e3 100644 --- a/samples/openapi3/client/petstore/python-experimental/petstore_api/configuration.py +++ b/samples/openapi3/client/petstore/python-experimental/petstore_api/configuration.py @@ -219,19 +219,33 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", # Disable client side validation self.client_side_validation = True + def __deepcopy__(self, memo): + cls = self.__class__ + result = cls.__new__(cls) + memo[id(self)] = result + for k, v in self.__dict__.items(): + if k not in ('logger', 'logger_file_handler'): + setattr(result, k, copy.deepcopy(v, memo)) + # shallow copy of loggers + result.logger = copy.copy(self.logger) + # use setters to configure loggers + result.logger_file = self.logger_file + result.debug = self.debug + return result + @classmethod def set_default(cls, default): """Set default instance of configuration. It stores default configuration, which can be - returned by get_instance method. + returned by get_default_copy method. :param default: object of Configuration """ - cls._default = copy.copy(default) + cls._default = copy.deepcopy(default) @classmethod - def get_instance(cls): + def get_default_copy(cls): """Return new instance of configuration. This method returns newly created, based on default constructor, @@ -241,7 +255,7 @@ def get_instance(cls): :return: The configuration object. """ if cls._default is not None: - return copy.copy(cls._default) + return copy.deepcopy(cls._default) return Configuration() @property