From 66908020e0a112aef2504bc6b6b751bc1c0a3740 Mon Sep 17 00:00:00 2001 From: Tim <39098775+webertim@users.noreply.github.com> Date: Sat, 16 Nov 2024 14:48:11 +0100 Subject: [PATCH] [python-pydantic-v1] Fix bug in serialization for SecretStr by applying changes from #18023 (#20102) * Apply changes of #18023 * Regenerate samples --------- Co-authored-by: tweber2 --- .../resources/python-pydantic-v1/api_client.mustache | 9 ++++++++- .../python-pydantic-v1/openapi_client/api_client.py | 9 ++++++++- .../petstore_api/api_client.py | 9 ++++++++- .../python-pydantic-v1/petstore_api/api_client.py | 9 ++++++++- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python-pydantic-v1/api_client.mustache b/modules/openapi-generator/src/main/resources/python-pydantic-v1/api_client.mustache index 000b6658b131..b06ddfd3a28f 100644 --- a/modules/openapi-generator/src/main/resources/python-pydantic-v1/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/python-pydantic-v1/api_client.mustache @@ -15,6 +15,7 @@ import re import tempfile from urllib.parse import quote +from pydantic import SecretStr {{#tornado}} import tornado.gen {{/tornado}} @@ -285,6 +286,7 @@ class ApiClient: """Builds a JSON POST object. If obj is None, return None. + If obj is SecretStr, return obj.get_secret_value() If obj is str, int, long, float, bool, return directly. If obj is datetime.datetime, datetime.date convert to string in iso8601 format. @@ -297,6 +299,8 @@ class ApiClient: """ if obj is None: return None + elif isinstance(obj, SecretStr): + return obj.get_secret_value() elif isinstance(obj, self.PRIMITIVE_TYPES): return obj elif isinstance(obj, list): @@ -316,7 +320,10 @@ class ApiClient: # and attributes which value is not None. # Convert attribute name to json key in # model definition for request. - obj_dict = obj.to_dict() + if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')): + obj_dict = obj.to_dict() + else: + obj_dict = obj.__dict__ return {key: self.sanitize_for_serialization(val) for key, val in obj_dict.items()} diff --git a/samples/client/echo_api/python-pydantic-v1/openapi_client/api_client.py b/samples/client/echo_api/python-pydantic-v1/openapi_client/api_client.py index 6d0d11c5b538..c71b3c27b311 100644 --- a/samples/client/echo_api/python-pydantic-v1/openapi_client/api_client.py +++ b/samples/client/echo_api/python-pydantic-v1/openapi_client/api_client.py @@ -24,6 +24,7 @@ import tempfile from urllib.parse import quote +from pydantic import SecretStr from openapi_client.configuration import Configuration from openapi_client.api_response import ApiResponse @@ -261,6 +262,7 @@ def sanitize_for_serialization(self, obj): """Builds a JSON POST object. If obj is None, return None. + If obj is SecretStr, return obj.get_secret_value() If obj is str, int, long, float, bool, return directly. If obj is datetime.datetime, datetime.date convert to string in iso8601 format. @@ -273,6 +275,8 @@ def sanitize_for_serialization(self, obj): """ if obj is None: return None + elif isinstance(obj, SecretStr): + return obj.get_secret_value() elif isinstance(obj, self.PRIMITIVE_TYPES): return obj elif isinstance(obj, list): @@ -292,7 +296,10 @@ def sanitize_for_serialization(self, obj): # and attributes which value is not None. # Convert attribute name to json key in # model definition for request. - obj_dict = obj.to_dict() + if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')): + obj_dict = obj.to_dict() + else: + obj_dict = obj.__dict__ return {key: self.sanitize_for_serialization(val) for key, val in obj_dict.items()} diff --git a/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/api_client.py b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/api_client.py index 52390c675df6..08d8d790c4f6 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/api_client.py +++ b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/api_client.py @@ -22,6 +22,7 @@ import tempfile from urllib.parse import quote +from pydantic import SecretStr from petstore_api.configuration import Configuration from petstore_api.api_response import ApiResponse @@ -241,6 +242,7 @@ def sanitize_for_serialization(self, obj): """Builds a JSON POST object. If obj is None, return None. + If obj is SecretStr, return obj.get_secret_value() If obj is str, int, long, float, bool, return directly. If obj is datetime.datetime, datetime.date convert to string in iso8601 format. @@ -253,6 +255,8 @@ def sanitize_for_serialization(self, obj): """ if obj is None: return None + elif isinstance(obj, SecretStr): + return obj.get_secret_value() elif isinstance(obj, self.PRIMITIVE_TYPES): return obj elif isinstance(obj, list): @@ -272,7 +276,10 @@ def sanitize_for_serialization(self, obj): # and attributes which value is not None. # Convert attribute name to json key in # model definition for request. - obj_dict = obj.to_dict() + if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')): + obj_dict = obj.to_dict() + else: + obj_dict = obj.__dict__ return {key: self.sanitize_for_serialization(val) for key, val in obj_dict.items()} diff --git a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/api_client.py b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/api_client.py index fae62ad75768..61c153fd31cd 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/api_client.py +++ b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/api_client.py @@ -23,6 +23,7 @@ import tempfile from urllib.parse import quote +from pydantic import SecretStr from petstore_api.configuration import Configuration from petstore_api.api_response import ApiResponse @@ -260,6 +261,7 @@ def sanitize_for_serialization(self, obj): """Builds a JSON POST object. If obj is None, return None. + If obj is SecretStr, return obj.get_secret_value() If obj is str, int, long, float, bool, return directly. If obj is datetime.datetime, datetime.date convert to string in iso8601 format. @@ -272,6 +274,8 @@ def sanitize_for_serialization(self, obj): """ if obj is None: return None + elif isinstance(obj, SecretStr): + return obj.get_secret_value() elif isinstance(obj, self.PRIMITIVE_TYPES): return obj elif isinstance(obj, list): @@ -291,7 +295,10 @@ def sanitize_for_serialization(self, obj): # and attributes which value is not None. # Convert attribute name to json key in # model definition for request. - obj_dict = obj.to_dict() + if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')): + obj_dict = obj.to_dict() + else: + obj_dict = obj.__dict__ return {key: self.sanitize_for_serialization(val) for key, val in obj_dict.items()}