Skip to content

Commit 6690802

Browse files
webertimtimvector
andauthored
[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 <tim.weber@vector.com>
1 parent 7087104 commit 6690802

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

modules/openapi-generator/src/main/resources/python-pydantic-v1/api_client.mustache

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import re
1515
import tempfile
1616

1717
from urllib.parse import quote
18+
from pydantic import SecretStr
1819
{{#tornado}}
1920
import tornado.gen
2021
{{/tornado}}
@@ -285,6 +286,7 @@ class ApiClient:
285286
"""Builds a JSON POST object.
286287

287288
If obj is None, return None.
289+
If obj is SecretStr, return obj.get_secret_value()
288290
If obj is str, int, long, float, bool, return directly.
289291
If obj is datetime.datetime, datetime.date
290292
convert to string in iso8601 format.
@@ -297,6 +299,8 @@ class ApiClient:
297299
"""
298300
if obj is None:
299301
return None
302+
elif isinstance(obj, SecretStr):
303+
return obj.get_secret_value()
300304
elif isinstance(obj, self.PRIMITIVE_TYPES):
301305
return obj
302306
elif isinstance(obj, list):
@@ -316,7 +320,10 @@ class ApiClient:
316320
# and attributes which value is not None.
317321
# Convert attribute name to json key in
318322
# model definition for request.
319-
obj_dict = obj.to_dict()
323+
if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')):
324+
obj_dict = obj.to_dict()
325+
else:
326+
obj_dict = obj.__dict__
320327

321328
return {key: self.sanitize_for_serialization(val)
322329
for key, val in obj_dict.items()}

samples/client/echo_api/python-pydantic-v1/openapi_client/api_client.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import tempfile
2525

2626
from urllib.parse import quote
27+
from pydantic import SecretStr
2728

2829
from openapi_client.configuration import Configuration
2930
from openapi_client.api_response import ApiResponse
@@ -261,6 +262,7 @@ def sanitize_for_serialization(self, obj):
261262
"""Builds a JSON POST object.
262263
263264
If obj is None, return None.
265+
If obj is SecretStr, return obj.get_secret_value()
264266
If obj is str, int, long, float, bool, return directly.
265267
If obj is datetime.datetime, datetime.date
266268
convert to string in iso8601 format.
@@ -273,6 +275,8 @@ def sanitize_for_serialization(self, obj):
273275
"""
274276
if obj is None:
275277
return None
278+
elif isinstance(obj, SecretStr):
279+
return obj.get_secret_value()
276280
elif isinstance(obj, self.PRIMITIVE_TYPES):
277281
return obj
278282
elif isinstance(obj, list):
@@ -292,7 +296,10 @@ def sanitize_for_serialization(self, obj):
292296
# and attributes which value is not None.
293297
# Convert attribute name to json key in
294298
# model definition for request.
295-
obj_dict = obj.to_dict()
299+
if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')):
300+
obj_dict = obj.to_dict()
301+
else:
302+
obj_dict = obj.__dict__
296303

297304
return {key: self.sanitize_for_serialization(val)
298305
for key, val in obj_dict.items()}

samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/api_client.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import tempfile
2323

2424
from urllib.parse import quote
25+
from pydantic import SecretStr
2526

2627
from petstore_api.configuration import Configuration
2728
from petstore_api.api_response import ApiResponse
@@ -241,6 +242,7 @@ def sanitize_for_serialization(self, obj):
241242
"""Builds a JSON POST object.
242243
243244
If obj is None, return None.
245+
If obj is SecretStr, return obj.get_secret_value()
244246
If obj is str, int, long, float, bool, return directly.
245247
If obj is datetime.datetime, datetime.date
246248
convert to string in iso8601 format.
@@ -253,6 +255,8 @@ def sanitize_for_serialization(self, obj):
253255
"""
254256
if obj is None:
255257
return None
258+
elif isinstance(obj, SecretStr):
259+
return obj.get_secret_value()
256260
elif isinstance(obj, self.PRIMITIVE_TYPES):
257261
return obj
258262
elif isinstance(obj, list):
@@ -272,7 +276,10 @@ def sanitize_for_serialization(self, obj):
272276
# and attributes which value is not None.
273277
# Convert attribute name to json key in
274278
# model definition for request.
275-
obj_dict = obj.to_dict()
279+
if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')):
280+
obj_dict = obj.to_dict()
281+
else:
282+
obj_dict = obj.__dict__
276283

277284
return {key: self.sanitize_for_serialization(val)
278285
for key, val in obj_dict.items()}

samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/api_client.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import tempfile
2424

2525
from urllib.parse import quote
26+
from pydantic import SecretStr
2627

2728
from petstore_api.configuration import Configuration
2829
from petstore_api.api_response import ApiResponse
@@ -260,6 +261,7 @@ def sanitize_for_serialization(self, obj):
260261
"""Builds a JSON POST object.
261262
262263
If obj is None, return None.
264+
If obj is SecretStr, return obj.get_secret_value()
263265
If obj is str, int, long, float, bool, return directly.
264266
If obj is datetime.datetime, datetime.date
265267
convert to string in iso8601 format.
@@ -272,6 +274,8 @@ def sanitize_for_serialization(self, obj):
272274
"""
273275
if obj is None:
274276
return None
277+
elif isinstance(obj, SecretStr):
278+
return obj.get_secret_value()
275279
elif isinstance(obj, self.PRIMITIVE_TYPES):
276280
return obj
277281
elif isinstance(obj, list):
@@ -291,7 +295,10 @@ def sanitize_for_serialization(self, obj):
291295
# and attributes which value is not None.
292296
# Convert attribute name to json key in
293297
# model definition for request.
294-
obj_dict = obj.to_dict()
298+
if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')):
299+
obj_dict = obj.to_dict()
300+
else:
301+
obj_dict = obj.__dict__
295302

296303
return {key: self.sanitize_for_serialization(val)
297304
for key, val in obj_dict.items()}

0 commit comments

Comments
 (0)