Skip to content

Commit

Permalink
[python-pydantic-v1] Fix bug in serialization for SecretStr by applyi…
Browse files Browse the repository at this point in the history
…ng changes from #18023 (#20102)

* Apply changes of #18023

* Regenerate samples

---------

Co-authored-by: tweber2 <[email protected]>
  • Loading branch information
webertim and timvector authored Nov 16, 2024
1 parent 7087104 commit 6690802
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import re
import tempfile

from urllib.parse import quote
from pydantic import SecretStr
{{#tornado}}
import tornado.gen
{{/tornado}}
Expand Down Expand Up @@ -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.
Expand All @@ -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):
Expand All @@ -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()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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):
Expand All @@ -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()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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):
Expand All @@ -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()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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):
Expand All @@ -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()}
Expand Down

0 comments on commit 6690802

Please sign in to comment.