From f608457949a9075bb6460905c8573ff069eca414 Mon Sep 17 00:00:00 2001 From: Tassilo Karge <67320593+karge-itestra@users.noreply.github.com> Date: Wed, 21 Aug 2024 11:34:11 +0200 Subject: [PATCH 1/2] correct sanitize_for_serialization in python generator, fixes #18106 The method did not consider the objects created for oneOf schemata. If one of the cases was a list, to_dict would return it instead of something that has an items() method. --- .../src/main/resources/python/api_client.mustache | 4 ++++ 1 file changed, 4 insertions(+) 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 0fe89af0b1c8..fb96a9f03e36 100644 --- a/modules/openapi-generator/src/main/resources/python/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/python/api_client.mustache @@ -390,6 +390,10 @@ class ApiClient: else: 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 { key: self.sanitize_for_serialization(val) for key, val in obj_dict.items() From 7d59c0eb01960caf05d10d1472808497548f56d0 Mon Sep 17 00:00:00 2001 From: karge-itestra Date: Wed, 21 Aug 2024 17:30:39 +0200 Subject: [PATCH 2/2] generate new samples --- .../openapi_client/api_client.py | 4 ++++ samples/client/echo_api/python/openapi_client/api_client.py | 4 ++++ .../client/petstore/python-aiohttp/petstore_api/api_client.py | 4 ++++ .../client/petstore/python/petstore_api/api_client.py | 4 ++++ 4 files changed, 16 insertions(+) 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 ed46d8c31a22..368214952778 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 @@ -383,6 +383,10 @@ def sanitize_for_serialization(self, obj): else: 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 { key: self.sanitize_for_serialization(val) for key, val in obj_dict.items() 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 ed46d8c31a22..368214952778 100644 --- a/samples/client/echo_api/python/openapi_client/api_client.py +++ b/samples/client/echo_api/python/openapi_client/api_client.py @@ -383,6 +383,10 @@ def sanitize_for_serialization(self, obj): else: 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 { key: self.sanitize_for_serialization(val) for key, val in obj_dict.items() 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 ab412d976569..077953e623bc 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 @@ -385,6 +385,10 @@ def sanitize_for_serialization(self, obj): else: 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 { key: self.sanitize_for_serialization(val) for key, val in obj_dict.items() 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 0aad03162b9a..d75ceb2b9a60 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/api_client.py +++ b/samples/openapi3/client/petstore/python/petstore_api/api_client.py @@ -382,6 +382,10 @@ def sanitize_for_serialization(self, obj): else: 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 { key: self.sanitize_for_serialization(val) for key, val in obj_dict.items()