From bcda8e4afd93b6acd113c53bad2daa0a6a608eb3 Mon Sep 17 00:00:00 2001 From: William Calliari Date: Wed, 1 Apr 2026 14:27:30 +0200 Subject: [PATCH 1/2] fix(python): Fix sanitize_for_serialization code generation for python client --- .../main/resources/python/api_client.mustache | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 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 fb5e39999614..e414300cd095 100644 --- a/modules/openapi-generator/src/main/resources/python/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/python/api_client.mustache @@ -379,28 +379,24 @@ class ApiClient: return obj.isoformat() elif isinstance(obj, decimal.Decimal): return str(obj) - elif isinstance(obj, dict): - obj_dict = obj + return { + key: self.sanitize_for_serialization(val) + for key, val in obj.items() + } + + # Convert model obj to dict except + # attributes `openapi_types`, `attribute_map` + # and attributes which value is not None. + # Convert attribute name to json key in + # model definition for request. + if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')): + obj_dict = obj.to_dict() else: - # Convert model obj to dict except - # attributes `openapi_types`, `attribute_map` - # and attributes which value is not None. - # Convert attribute name to json key in - # model definition for request. - if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')): - obj_dict = obj.to_dict() - else: - obj_dict = obj.__dict__ + obj_dict = obj.__dict__ - if isinstance(obj_dict, list): - # here we handle instances that can either be a list or something else, and only became a real list by calling to_dict() - return self.sanitize_for_serialization(obj_dict) + return self.sanitize_for_serialization(obj_dict) - return { - key: self.sanitize_for_serialization(val) - for key, val in obj_dict.items() - } def deserialize(self, response_text: str, response_type: str, content_type: Optional[str]): """Deserializes response into an object. From 2da75eca667b403ffb61af1a9e0b4f1beae09c30 Mon Sep 17 00:00:00 2001 From: William Calliari Date: Wed, 1 Apr 2026 15:46:48 +0200 Subject: [PATCH 2/2] chore: regenerate samples --- .../openapi_client/api_client.py | 32 ++++++++----------- .../python/openapi_client/api_client.py | 32 ++++++++----------- .../python-aiohttp/petstore_api/api_client.py | 32 ++++++++----------- .../python-httpx/petstore_api/api_client.py | 32 ++++++++----------- .../petstore_api/api_client.py | 32 ++++++++----------- .../python/petstore_api/api_client.py | 32 ++++++++----------- 6 files changed, 84 insertions(+), 108 deletions(-) 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 c8ed0de8b19d..4b824e431f11 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 @@ -372,28 +372,24 @@ def sanitize_for_serialization(self, obj): return obj.isoformat() elif isinstance(obj, decimal.Decimal): return str(obj) - elif isinstance(obj, dict): - obj_dict = obj + return { + key: self.sanitize_for_serialization(val) + for key, val in obj.items() + } + + # Convert model obj to dict except + # attributes `openapi_types`, `attribute_map` + # and attributes which value is not None. + # Convert attribute name to json key in + # model definition for request. + if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')): + obj_dict = obj.to_dict() else: - # Convert model obj to dict except - # attributes `openapi_types`, `attribute_map` - # and attributes which value is not None. - # Convert attribute name to json key in - # model definition for request. - if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')): - obj_dict = obj.to_dict() - else: - obj_dict = obj.__dict__ + obj_dict = obj.__dict__ - if isinstance(obj_dict, list): - # here we handle instances that can either be a list or something else, and only became a real list by calling to_dict() - return self.sanitize_for_serialization(obj_dict) + return self.sanitize_for_serialization(obj_dict) - return { - key: self.sanitize_for_serialization(val) - for key, val in obj_dict.items() - } def deserialize(self, response_text: str, response_type: str, content_type: Optional[str]): """Deserializes response into an object. 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 c8ed0de8b19d..4b824e431f11 100644 --- a/samples/client/echo_api/python/openapi_client/api_client.py +++ b/samples/client/echo_api/python/openapi_client/api_client.py @@ -372,28 +372,24 @@ def sanitize_for_serialization(self, obj): return obj.isoformat() elif isinstance(obj, decimal.Decimal): return str(obj) - elif isinstance(obj, dict): - obj_dict = obj + return { + key: self.sanitize_for_serialization(val) + for key, val in obj.items() + } + + # Convert model obj to dict except + # attributes `openapi_types`, `attribute_map` + # and attributes which value is not None. + # Convert attribute name to json key in + # model definition for request. + if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')): + obj_dict = obj.to_dict() else: - # Convert model obj to dict except - # attributes `openapi_types`, `attribute_map` - # and attributes which value is not None. - # Convert attribute name to json key in - # model definition for request. - if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')): - obj_dict = obj.to_dict() - else: - obj_dict = obj.__dict__ + obj_dict = obj.__dict__ - if isinstance(obj_dict, list): - # here we handle instances that can either be a list or something else, and only became a real list by calling to_dict() - return self.sanitize_for_serialization(obj_dict) + return self.sanitize_for_serialization(obj_dict) - return { - key: self.sanitize_for_serialization(val) - for key, val in obj_dict.items() - } def deserialize(self, response_text: str, response_type: str, content_type: Optional[str]): """Deserializes response into an object. 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 c6ce4380f5ae..3d0170779a39 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 @@ -374,28 +374,24 @@ def sanitize_for_serialization(self, obj): return obj.isoformat() elif isinstance(obj, decimal.Decimal): return str(obj) - elif isinstance(obj, dict): - obj_dict = obj + return { + key: self.sanitize_for_serialization(val) + for key, val in obj.items() + } + + # Convert model obj to dict except + # attributes `openapi_types`, `attribute_map` + # and attributes which value is not None. + # Convert attribute name to json key in + # model definition for request. + if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')): + obj_dict = obj.to_dict() else: - # Convert model obj to dict except - # attributes `openapi_types`, `attribute_map` - # and attributes which value is not None. - # Convert attribute name to json key in - # model definition for request. - if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')): - obj_dict = obj.to_dict() - else: - obj_dict = obj.__dict__ + obj_dict = obj.__dict__ - if isinstance(obj_dict, list): - # here we handle instances that can either be a list or something else, and only became a real list by calling to_dict() - return self.sanitize_for_serialization(obj_dict) + return self.sanitize_for_serialization(obj_dict) - return { - key: self.sanitize_for_serialization(val) - for key, val in obj_dict.items() - } def deserialize(self, response_text: str, response_type: str, content_type: Optional[str]): """Deserializes response into an object. diff --git a/samples/openapi3/client/petstore/python-httpx/petstore_api/api_client.py b/samples/openapi3/client/petstore/python-httpx/petstore_api/api_client.py index c6ce4380f5ae..3d0170779a39 100644 --- a/samples/openapi3/client/petstore/python-httpx/petstore_api/api_client.py +++ b/samples/openapi3/client/petstore/python-httpx/petstore_api/api_client.py @@ -374,28 +374,24 @@ def sanitize_for_serialization(self, obj): return obj.isoformat() elif isinstance(obj, decimal.Decimal): return str(obj) - elif isinstance(obj, dict): - obj_dict = obj + return { + key: self.sanitize_for_serialization(val) + for key, val in obj.items() + } + + # Convert model obj to dict except + # attributes `openapi_types`, `attribute_map` + # and attributes which value is not None. + # Convert attribute name to json key in + # model definition for request. + if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')): + obj_dict = obj.to_dict() else: - # Convert model obj to dict except - # attributes `openapi_types`, `attribute_map` - # and attributes which value is not None. - # Convert attribute name to json key in - # model definition for request. - if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')): - obj_dict = obj.to_dict() - else: - obj_dict = obj.__dict__ + obj_dict = obj.__dict__ - if isinstance(obj_dict, list): - # here we handle instances that can either be a list or something else, and only became a real list by calling to_dict() - return self.sanitize_for_serialization(obj_dict) + return self.sanitize_for_serialization(obj_dict) - return { - key: self.sanitize_for_serialization(val) - for key, val in obj_dict.items() - } def deserialize(self, response_text: str, response_type: str, content_type: Optional[str]): """Deserializes response into an object. diff --git a/samples/openapi3/client/petstore/python-lazyImports/petstore_api/api_client.py b/samples/openapi3/client/petstore/python-lazyImports/petstore_api/api_client.py index 2eed8c105cee..d388566cbbe2 100644 --- a/samples/openapi3/client/petstore/python-lazyImports/petstore_api/api_client.py +++ b/samples/openapi3/client/petstore/python-lazyImports/petstore_api/api_client.py @@ -371,28 +371,24 @@ def sanitize_for_serialization(self, obj): return obj.isoformat() elif isinstance(obj, decimal.Decimal): return str(obj) - elif isinstance(obj, dict): - obj_dict = obj + return { + key: self.sanitize_for_serialization(val) + for key, val in obj.items() + } + + # Convert model obj to dict except + # attributes `openapi_types`, `attribute_map` + # and attributes which value is not None. + # Convert attribute name to json key in + # model definition for request. + if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')): + obj_dict = obj.to_dict() else: - # Convert model obj to dict except - # attributes `openapi_types`, `attribute_map` - # and attributes which value is not None. - # Convert attribute name to json key in - # model definition for request. - if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')): - obj_dict = obj.to_dict() - else: - obj_dict = obj.__dict__ + obj_dict = obj.__dict__ - if isinstance(obj_dict, list): - # here we handle instances that can either be a list or something else, and only became a real list by calling to_dict() - return self.sanitize_for_serialization(obj_dict) + return self.sanitize_for_serialization(obj_dict) - return { - key: self.sanitize_for_serialization(val) - for key, val in obj_dict.items() - } def deserialize(self, response_text: str, response_type: str, content_type: Optional[str]): """Deserializes response into an object. 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 2eed8c105cee..d388566cbbe2 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/api_client.py +++ b/samples/openapi3/client/petstore/python/petstore_api/api_client.py @@ -371,28 +371,24 @@ def sanitize_for_serialization(self, obj): return obj.isoformat() elif isinstance(obj, decimal.Decimal): return str(obj) - elif isinstance(obj, dict): - obj_dict = obj + return { + key: self.sanitize_for_serialization(val) + for key, val in obj.items() + } + + # Convert model obj to dict except + # attributes `openapi_types`, `attribute_map` + # and attributes which value is not None. + # Convert attribute name to json key in + # model definition for request. + if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')): + obj_dict = obj.to_dict() else: - # Convert model obj to dict except - # attributes `openapi_types`, `attribute_map` - # and attributes which value is not None. - # Convert attribute name to json key in - # model definition for request. - if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')): - obj_dict = obj.to_dict() - else: - obj_dict = obj.__dict__ + obj_dict = obj.__dict__ - if isinstance(obj_dict, list): - # here we handle instances that can either be a list or something else, and only became a real list by calling to_dict() - return self.sanitize_for_serialization(obj_dict) + return self.sanitize_for_serialization(obj_dict) - return { - key: self.sanitize_for_serialization(val) - for key, val in obj_dict.items() - } def deserialize(self, response_text: str, response_type: str, content_type: Optional[str]): """Deserializes response into an object.