Skip to content

Commit 80ea9e5

Browse files
authored
fix(python): Fix sanitize_for_serialization code generation for python client (#23415)
* fix(python): Fix sanitize_for_serialization code generation for python client * chore: regenerate samples
1 parent d99a8c9 commit 80ea9e5

7 files changed

Lines changed: 98 additions & 126 deletions

File tree

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -379,28 +379,24 @@ class ApiClient:
379379
return obj.isoformat()
380380
elif isinstance(obj, decimal.Decimal):
381381
return str(obj)
382-
383382
elif isinstance(obj, dict):
384-
obj_dict = obj
383+
return {
384+
key: self.sanitize_for_serialization(val)
385+
for key, val in obj.items()
386+
}
387+
388+
# Convert model obj to dict except
389+
# attributes `openapi_types`, `attribute_map`
390+
# and attributes which value is not None.
391+
# Convert attribute name to json key in
392+
# model definition for request.
393+
if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')):
394+
obj_dict = obj.to_dict()
385395
else:
386-
# Convert model obj to dict except
387-
# attributes `openapi_types`, `attribute_map`
388-
# and attributes which value is not None.
389-
# Convert attribute name to json key in
390-
# model definition for request.
391-
if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')):
392-
obj_dict = obj.to_dict()
393-
else:
394-
obj_dict = obj.__dict__
396+
obj_dict = obj.__dict__
395397

396-
if isinstance(obj_dict, list):
397-
# here we handle instances that can either be a list or something else, and only became a real list by calling to_dict()
398-
return self.sanitize_for_serialization(obj_dict)
398+
return self.sanitize_for_serialization(obj_dict)
399399

400-
return {
401-
key: self.sanitize_for_serialization(val)
402-
for key, val in obj_dict.items()
403-
}
404400

405401
def deserialize(self, response_text: str, response_type: str, content_type: Optional[str]):
406402
"""Deserializes response into an object.

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/api_client.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -372,28 +372,24 @@ def sanitize_for_serialization(self, obj):
372372
return obj.isoformat()
373373
elif isinstance(obj, decimal.Decimal):
374374
return str(obj)
375-
376375
elif isinstance(obj, dict):
377-
obj_dict = obj
376+
return {
377+
key: self.sanitize_for_serialization(val)
378+
for key, val in obj.items()
379+
}
380+
381+
# Convert model obj to dict except
382+
# attributes `openapi_types`, `attribute_map`
383+
# and attributes which value is not None.
384+
# Convert attribute name to json key in
385+
# model definition for request.
386+
if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')):
387+
obj_dict = obj.to_dict()
378388
else:
379-
# Convert model obj to dict except
380-
# attributes `openapi_types`, `attribute_map`
381-
# and attributes which value is not None.
382-
# Convert attribute name to json key in
383-
# model definition for request.
384-
if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')):
385-
obj_dict = obj.to_dict()
386-
else:
387-
obj_dict = obj.__dict__
389+
obj_dict = obj.__dict__
388390

389-
if isinstance(obj_dict, list):
390-
# here we handle instances that can either be a list or something else, and only became a real list by calling to_dict()
391-
return self.sanitize_for_serialization(obj_dict)
391+
return self.sanitize_for_serialization(obj_dict)
392392

393-
return {
394-
key: self.sanitize_for_serialization(val)
395-
for key, val in obj_dict.items()
396-
}
397393

398394
def deserialize(self, response_text: str, response_type: str, content_type: Optional[str]):
399395
"""Deserializes response into an object.

samples/client/echo_api/python/openapi_client/api_client.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -372,28 +372,24 @@ def sanitize_for_serialization(self, obj):
372372
return obj.isoformat()
373373
elif isinstance(obj, decimal.Decimal):
374374
return str(obj)
375-
376375
elif isinstance(obj, dict):
377-
obj_dict = obj
376+
return {
377+
key: self.sanitize_for_serialization(val)
378+
for key, val in obj.items()
379+
}
380+
381+
# Convert model obj to dict except
382+
# attributes `openapi_types`, `attribute_map`
383+
# and attributes which value is not None.
384+
# Convert attribute name to json key in
385+
# model definition for request.
386+
if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')):
387+
obj_dict = obj.to_dict()
378388
else:
379-
# Convert model obj to dict except
380-
# attributes `openapi_types`, `attribute_map`
381-
# and attributes which value is not None.
382-
# Convert attribute name to json key in
383-
# model definition for request.
384-
if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')):
385-
obj_dict = obj.to_dict()
386-
else:
387-
obj_dict = obj.__dict__
389+
obj_dict = obj.__dict__
388390

389-
if isinstance(obj_dict, list):
390-
# here we handle instances that can either be a list or something else, and only became a real list by calling to_dict()
391-
return self.sanitize_for_serialization(obj_dict)
391+
return self.sanitize_for_serialization(obj_dict)
392392

393-
return {
394-
key: self.sanitize_for_serialization(val)
395-
for key, val in obj_dict.items()
396-
}
397393

398394
def deserialize(self, response_text: str, response_type: str, content_type: Optional[str]):
399395
"""Deserializes response into an object.

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -374,28 +374,24 @@ def sanitize_for_serialization(self, obj):
374374
return obj.isoformat()
375375
elif isinstance(obj, decimal.Decimal):
376376
return str(obj)
377-
378377
elif isinstance(obj, dict):
379-
obj_dict = obj
378+
return {
379+
key: self.sanitize_for_serialization(val)
380+
for key, val in obj.items()
381+
}
382+
383+
# Convert model obj to dict except
384+
# attributes `openapi_types`, `attribute_map`
385+
# and attributes which value is not None.
386+
# Convert attribute name to json key in
387+
# model definition for request.
388+
if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')):
389+
obj_dict = obj.to_dict()
380390
else:
381-
# Convert model obj to dict except
382-
# attributes `openapi_types`, `attribute_map`
383-
# and attributes which value is not None.
384-
# Convert attribute name to json key in
385-
# model definition for request.
386-
if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')):
387-
obj_dict = obj.to_dict()
388-
else:
389-
obj_dict = obj.__dict__
391+
obj_dict = obj.__dict__
390392

391-
if isinstance(obj_dict, list):
392-
# here we handle instances that can either be a list or something else, and only became a real list by calling to_dict()
393-
return self.sanitize_for_serialization(obj_dict)
393+
return self.sanitize_for_serialization(obj_dict)
394394

395-
return {
396-
key: self.sanitize_for_serialization(val)
397-
for key, val in obj_dict.items()
398-
}
399395

400396
def deserialize(self, response_text: str, response_type: str, content_type: Optional[str]):
401397
"""Deserializes response into an object.

samples/openapi3/client/petstore/python-httpx/petstore_api/api_client.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -374,28 +374,24 @@ def sanitize_for_serialization(self, obj):
374374
return obj.isoformat()
375375
elif isinstance(obj, decimal.Decimal):
376376
return str(obj)
377-
378377
elif isinstance(obj, dict):
379-
obj_dict = obj
378+
return {
379+
key: self.sanitize_for_serialization(val)
380+
for key, val in obj.items()
381+
}
382+
383+
# Convert model obj to dict except
384+
# attributes `openapi_types`, `attribute_map`
385+
# and attributes which value is not None.
386+
# Convert attribute name to json key in
387+
# model definition for request.
388+
if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')):
389+
obj_dict = obj.to_dict()
380390
else:
381-
# Convert model obj to dict except
382-
# attributes `openapi_types`, `attribute_map`
383-
# and attributes which value is not None.
384-
# Convert attribute name to json key in
385-
# model definition for request.
386-
if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')):
387-
obj_dict = obj.to_dict()
388-
else:
389-
obj_dict = obj.__dict__
391+
obj_dict = obj.__dict__
390392

391-
if isinstance(obj_dict, list):
392-
# here we handle instances that can either be a list or something else, and only became a real list by calling to_dict()
393-
return self.sanitize_for_serialization(obj_dict)
393+
return self.sanitize_for_serialization(obj_dict)
394394

395-
return {
396-
key: self.sanitize_for_serialization(val)
397-
for key, val in obj_dict.items()
398-
}
399395

400396
def deserialize(self, response_text: str, response_type: str, content_type: Optional[str]):
401397
"""Deserializes response into an object.

samples/openapi3/client/petstore/python-lazyImports/petstore_api/api_client.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -371,28 +371,24 @@ def sanitize_for_serialization(self, obj):
371371
return obj.isoformat()
372372
elif isinstance(obj, decimal.Decimal):
373373
return str(obj)
374-
375374
elif isinstance(obj, dict):
376-
obj_dict = obj
375+
return {
376+
key: self.sanitize_for_serialization(val)
377+
for key, val in obj.items()
378+
}
379+
380+
# Convert model obj to dict except
381+
# attributes `openapi_types`, `attribute_map`
382+
# and attributes which value is not None.
383+
# Convert attribute name to json key in
384+
# model definition for request.
385+
if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')):
386+
obj_dict = obj.to_dict()
377387
else:
378-
# Convert model obj to dict except
379-
# attributes `openapi_types`, `attribute_map`
380-
# and attributes which value is not None.
381-
# Convert attribute name to json key in
382-
# model definition for request.
383-
if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')):
384-
obj_dict = obj.to_dict()
385-
else:
386-
obj_dict = obj.__dict__
388+
obj_dict = obj.__dict__
387389

388-
if isinstance(obj_dict, list):
389-
# here we handle instances that can either be a list or something else, and only became a real list by calling to_dict()
390-
return self.sanitize_for_serialization(obj_dict)
390+
return self.sanitize_for_serialization(obj_dict)
391391

392-
return {
393-
key: self.sanitize_for_serialization(val)
394-
for key, val in obj_dict.items()
395-
}
396392

397393
def deserialize(self, response_text: str, response_type: str, content_type: Optional[str]):
398394
"""Deserializes response into an object.

samples/openapi3/client/petstore/python/petstore_api/api_client.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -371,28 +371,24 @@ def sanitize_for_serialization(self, obj):
371371
return obj.isoformat()
372372
elif isinstance(obj, decimal.Decimal):
373373
return str(obj)
374-
375374
elif isinstance(obj, dict):
376-
obj_dict = obj
375+
return {
376+
key: self.sanitize_for_serialization(val)
377+
for key, val in obj.items()
378+
}
379+
380+
# Convert model obj to dict except
381+
# attributes `openapi_types`, `attribute_map`
382+
# and attributes which value is not None.
383+
# Convert attribute name to json key in
384+
# model definition for request.
385+
if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')):
386+
obj_dict = obj.to_dict()
377387
else:
378-
# Convert model obj to dict except
379-
# attributes `openapi_types`, `attribute_map`
380-
# and attributes which value is not None.
381-
# Convert attribute name to json key in
382-
# model definition for request.
383-
if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')):
384-
obj_dict = obj.to_dict()
385-
else:
386-
obj_dict = obj.__dict__
388+
obj_dict = obj.__dict__
387389

388-
if isinstance(obj_dict, list):
389-
# here we handle instances that can either be a list or something else, and only became a real list by calling to_dict()
390-
return self.sanitize_for_serialization(obj_dict)
390+
return self.sanitize_for_serialization(obj_dict)
391391

392-
return {
393-
key: self.sanitize_for_serialization(val)
394-
for key, val in obj_dict.items()
395-
}
396392

397393
def deserialize(self, response_text: str, response_type: str, content_type: Optional[str]):
398394
"""Deserializes response into an object.

0 commit comments

Comments
 (0)