From f2fea3324680a7c1caebaeb1b3aeb5ad8dbd706d Mon Sep 17 00:00:00 2001 From: Nick Brown Date: Mon, 20 Oct 2025 18:11:34 +0100 Subject: [PATCH 1/2] Don't set logging stream handler in library code Closes #8263 --- .../resources/python/configuration.mustache | 20 ++++--------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/python/configuration.mustache b/modules/swagger-codegen/src/main/resources/python/configuration.mustache index 9fa5239e81b..c9f54a80a3b 100644 --- a/modules/swagger-codegen/src/main/resources/python/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/python/configuration.mustache @@ -56,8 +56,6 @@ class Configuration(object): self.logger["urllib3_logger"] = logging.getLogger("urllib3") # Log format self.logger_format = '%(asctime)s %(levelname)s %(message)s' - # Log stream handler - self.logger_stream_handler = None # Log file handler self.logger_file_handler = None # Debug file location @@ -101,9 +99,6 @@ class Configuration(object): def logger_file(self): """The logger file. - If the logger_file is None, then add stream handler and remove file - handler. Otherwise, add file handler and remove stream handler. - :param value: The logger_file path. :type: str """ @@ -113,29 +108,22 @@ class Configuration(object): def logger_file(self, value): """The logger file. - If the logger_file is None, then add stream handler and remove file - handler. Otherwise, add file handler and remove stream handler. + If the logger_file is None, then remove file + handler. Otherwise, add file handler. :param value: The logger_file path. :type: str """ self.__logger_file = value if self.__logger_file: - # If set logging file, - # then add file handler and remove stream handler. + # If set logging file, then add file handler. self.logger_file_handler = logging.FileHandler(self.__logger_file) self.logger_file_handler.setFormatter(self.logger_formatter) for _, logger in six.iteritems(self.logger): logger.addHandler(self.logger_file_handler) - if self.logger_stream_handler: - logger.removeHandler(self.logger_stream_handler) else: - # If not set logging file, - # then add stream handler and remove file handler. - self.logger_stream_handler = logging.StreamHandler() - self.logger_stream_handler.setFormatter(self.logger_formatter) + # If not, remove file handler. for _, logger in six.iteritems(self.logger): - logger.addHandler(self.logger_stream_handler) if self.logger_file_handler: logger.removeHandler(self.logger_file_handler) From 1b530d5ae2ce71600060372191a395df1e1bd0d5 Mon Sep 17 00:00:00 2001 From: Nick Brown Date: Mon, 20 Oct 2025 22:01:46 +0100 Subject: [PATCH 2/2] Update python-petstore and petstore-security-test samplea --- .../python/.swagger-codegen/VERSION | 2 +- .../python/petstore_api/api_client.py | 3 +- .../python/petstore_api/configuration.py | 41 +++++++++---------- .../petstore_api/models/model_return.py | 14 +++++-- .../python/petstore_api/rest.py | 6 +-- .../python/test/__init__.py | 1 + .../petstore/python/.swagger-codegen/VERSION | 2 +- .../python/petstore_api/configuration.py | 20 ++------- .../petstore/python/petstore_api/rest.py | 4 +- 9 files changed, 45 insertions(+), 48 deletions(-) diff --git a/samples/client/petstore-security-test/python/.swagger-codegen/VERSION b/samples/client/petstore-security-test/python/.swagger-codegen/VERSION index cfd1b966621..9213bf6f1c6 100644 --- a/samples/client/petstore-security-test/python/.swagger-codegen/VERSION +++ b/samples/client/petstore-security-test/python/.swagger-codegen/VERSION @@ -1 +1 @@ -2.4.33-SNAPSHOT \ No newline at end of file +2.4.49-SNAPSHOT \ No newline at end of file diff --git a/samples/client/petstore-security-test/python/petstore_api/api_client.py b/samples/client/petstore-security-test/python/petstore_api/api_client.py index 637d9a5119d..9fc3b0f694c 100644 --- a/samples/client/petstore-security-test/python/petstore_api/api_client.py +++ b/samples/client/petstore-security-test/python/petstore_api/api_client.py @@ -75,6 +75,7 @@ def __init__(self, configuration=None, header_name=None, header_value=None, self.cookie = cookie # Set default User-Agent. self.user_agent = 'Swagger-Codegen/1.0.0/python' + self.client_side_validation = configuration.client_side_validation def __del__(self): if self._pool is not None: @@ -533,7 +534,7 @@ def __deserialize_file(self, response): content_disposition).group(1) path = os.path.join(os.path.dirname(path), filename) - with open(path, "wb") as f: + with open(path, "w") as f: f.write(response.data) return path diff --git a/samples/client/petstore-security-test/python/petstore_api/configuration.py b/samples/client/petstore-security-test/python/petstore_api/configuration.py index b9eb1ab6564..3905c71505e 100644 --- a/samples/client/petstore-security-test/python/petstore_api/configuration.py +++ b/samples/client/petstore-security-test/python/petstore_api/configuration.py @@ -49,6 +49,8 @@ def __init__(self): self.api_key = {} # dict to store API prefix (e.g. Bearer) self.api_key_prefix = {} + # function to refresh API key if expired + self.refresh_api_key_hook = None # Username for HTTP basic authentication self.username = "" # Password for HTTP basic authentication @@ -63,8 +65,6 @@ def __init__(self): self.logger["urllib3_logger"] = logging.getLogger("urllib3") # Log format self.logger_format = '%(asctime)s %(levelname)s %(message)s' - # Log stream handler - self.logger_stream_handler = None # Log file handler self.logger_file_handler = None # Debug file location @@ -97,6 +97,9 @@ def __init__(self): # Safe chars for path_param self.safe_chars_for_path_param = '' + # Disable client side validation + self.client_side_validation = True + @classmethod def set_default(cls, default): cls._default = default @@ -105,9 +108,6 @@ def set_default(cls, default): def logger_file(self): """The logger file. - If the logger_file is None, then add stream handler and remove file - handler. Otherwise, add file handler and remove stream handler. - :param value: The logger_file path. :type: str """ @@ -117,29 +117,22 @@ def logger_file(self): def logger_file(self, value): """The logger file. - If the logger_file is None, then add stream handler and remove file - handler. Otherwise, add file handler and remove stream handler. + If the logger_file is None, then remove file + handler. Otherwise, add file handler. :param value: The logger_file path. :type: str """ self.__logger_file = value if self.__logger_file: - # If set logging file, - # then add file handler and remove stream handler. + # If set logging file, then add file handler. self.logger_file_handler = logging.FileHandler(self.__logger_file) self.logger_file_handler.setFormatter(self.logger_formatter) for _, logger in six.iteritems(self.logger): logger.addHandler(self.logger_file_handler) - if self.logger_stream_handler: - logger.removeHandler(self.logger_stream_handler) else: - # If not set logging file, - # then add stream handler and remove file handler. - self.logger_stream_handler = logging.StreamHandler() - self.logger_stream_handler.setFormatter(self.logger_formatter) + # If not, remove file handler. for _, logger in six.iteritems(self.logger): - logger.addHandler(self.logger_stream_handler) if self.logger_file_handler: logger.removeHandler(self.logger_file_handler) @@ -203,11 +196,17 @@ def get_api_key_with_prefix(self, identifier): :param identifier: The identifier of apiKey. :return: The token for api key authentication. """ - if (self.api_key.get(identifier) and - self.api_key_prefix.get(identifier)): - return self.api_key_prefix[identifier] + ' ' + self.api_key[identifier] # noqa: E501 - elif self.api_key.get(identifier): - return self.api_key[identifier] + + if self.refresh_api_key_hook: + self.refresh_api_key_hook(self) + + key = self.api_key.get(identifier) + if key: + prefix = self.api_key_prefix.get(identifier) + if prefix: + return "%s %s" % (prefix, key) + else: + return key def get_basic_auth_token(self): """Gets HTTP basic authentication header (string). diff --git a/samples/client/petstore-security-test/python/petstore_api/models/model_return.py b/samples/client/petstore-security-test/python/petstore_api/models/model_return.py index ff8afa357c7..dbba5520399 100644 --- a/samples/client/petstore-security-test/python/petstore_api/models/model_return.py +++ b/samples/client/petstore-security-test/python/petstore_api/models/model_return.py @@ -16,6 +16,8 @@ import six +from petstore_api.configuration import Configuration + class ModelReturn(object): """NOTE: This class is auto generated by the swagger code generator program. @@ -38,8 +40,11 @@ class ModelReturn(object): '_return': 'return' } - def __init__(self, _return=None): # noqa: E501 + def __init__(self, _return=None, _configuration=None): # noqa: E501 """ModelReturn - a model defined in Swagger""" # noqa: E501 + if _configuration is None: + _configuration = Configuration() + self._configuration = _configuration self.__return = None self.discriminator = None @@ -110,8 +115,11 @@ def __eq__(self, other): if not isinstance(other, ModelReturn): return False - return self.__dict__ == other.__dict__ + return self.to_dict() == other.to_dict() def __ne__(self, other): """Returns true if both objects are not equal""" - return not self == other + if not isinstance(other, ModelReturn): + return True + + return self.to_dict() != other.to_dict() diff --git a/samples/client/petstore-security-test/python/petstore_api/rest.py b/samples/client/petstore-security-test/python/petstore_api/rest.py index 1f21e828708..032f100353f 100644 --- a/samples/client/petstore-security-test/python/petstore_api/rest.py +++ b/samples/client/petstore-security-test/python/petstore_api/rest.py @@ -43,11 +43,11 @@ def __init__(self, resp): def getheaders(self): """Returns a dictionary of the response headers.""" - return self.urllib3_response.getheaders() + return self.urllib3_response.headers def getheader(self, name, default=None): """Returns a given response header.""" - return self.urllib3_response.getheader(name, default) + return self.urllib3_response.headers.get(name, default) class RESTClientObject(object): @@ -156,7 +156,7 @@ def request(self, method, url, query_params=None, headers=None, if query_params: url += '?' + urlencode(query_params) if re.search('json', headers['Content-Type'], re.IGNORECASE): - request_body = None + request_body = '{}' if body is not None: request_body = json.dumps(body) r = self.pool_manager.request( diff --git a/samples/client/petstore-security-test/python/test/__init__.py b/samples/client/petstore-security-test/python/test/__init__.py index e69de29bb2d..576f56f87e7 100644 --- a/samples/client/petstore-security-test/python/test/__init__.py +++ b/samples/client/petstore-security-test/python/test/__init__.py @@ -0,0 +1 @@ +# coding: utf-8 \ No newline at end of file diff --git a/samples/client/petstore/python/.swagger-codegen/VERSION b/samples/client/petstore/python/.swagger-codegen/VERSION index cfd1b966621..9213bf6f1c6 100644 --- a/samples/client/petstore/python/.swagger-codegen/VERSION +++ b/samples/client/petstore/python/.swagger-codegen/VERSION @@ -1 +1 @@ -2.4.33-SNAPSHOT \ No newline at end of file +2.4.49-SNAPSHOT \ No newline at end of file diff --git a/samples/client/petstore/python/petstore_api/configuration.py b/samples/client/petstore/python/petstore_api/configuration.py index dedd3919394..570c1d41cac 100644 --- a/samples/client/petstore/python/petstore_api/configuration.py +++ b/samples/client/petstore/python/petstore_api/configuration.py @@ -65,8 +65,6 @@ def __init__(self): self.logger["urllib3_logger"] = logging.getLogger("urllib3") # Log format self.logger_format = '%(asctime)s %(levelname)s %(message)s' - # Log stream handler - self.logger_stream_handler = None # Log file handler self.logger_file_handler = None # Debug file location @@ -110,9 +108,6 @@ def set_default(cls, default): def logger_file(self): """The logger file. - If the logger_file is None, then add stream handler and remove file - handler. Otherwise, add file handler and remove stream handler. - :param value: The logger_file path. :type: str """ @@ -122,29 +117,22 @@ def logger_file(self): def logger_file(self, value): """The logger file. - If the logger_file is None, then add stream handler and remove file - handler. Otherwise, add file handler and remove stream handler. + If the logger_file is None, then remove file + handler. Otherwise, add file handler. :param value: The logger_file path. :type: str """ self.__logger_file = value if self.__logger_file: - # If set logging file, - # then add file handler and remove stream handler. + # If set logging file, then add file handler. self.logger_file_handler = logging.FileHandler(self.__logger_file) self.logger_file_handler.setFormatter(self.logger_formatter) for _, logger in six.iteritems(self.logger): logger.addHandler(self.logger_file_handler) - if self.logger_stream_handler: - logger.removeHandler(self.logger_stream_handler) else: - # If not set logging file, - # then add stream handler and remove file handler. - self.logger_stream_handler = logging.StreamHandler() - self.logger_stream_handler.setFormatter(self.logger_formatter) + # If not, remove file handler. for _, logger in six.iteritems(self.logger): - logger.addHandler(self.logger_stream_handler) if self.logger_file_handler: logger.removeHandler(self.logger_file_handler) diff --git a/samples/client/petstore/python/petstore_api/rest.py b/samples/client/petstore/python/petstore_api/rest.py index 00b43a2e4ff..4603ef0853c 100644 --- a/samples/client/petstore/python/petstore_api/rest.py +++ b/samples/client/petstore/python/petstore_api/rest.py @@ -43,11 +43,11 @@ def __init__(self, resp): def getheaders(self): """Returns a dictionary of the response headers.""" - return self.urllib3_response.getheaders() + return self.urllib3_response.headers def getheader(self, name, default=None): """Returns a given response header.""" - return self.urllib3_response.getheader(name, default) + return self.urllib3_response.headers.get(name, default) class RESTClientObject(object):