Skip to content

Commit

Permalink
[python-pydantic-v1] Fix unnamed dicts with additional properties (Op…
Browse files Browse the repository at this point in the history
…enAPITools#18112)

* [python-pydantic-v1] pick OpenAPITools#16779

* [python] update sample
  • Loading branch information
fa0311 authored and zapodot committed Mar 21, 2024
1 parent 754c43b commit 188d4ba
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,17 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
{{/isArray}}
{{#isMap}}
{{#items.isArray}}
{{^items.items.isPrimitiveType}}
# override the default output from pydantic by calling `to_dict()` of each value in {{{name}}} (dict of array)
_field_dict_of_array = {}
if self.{{{name}}}:
for _key in self.{{{name}}}:
if self.{{{name}}}[_key]:
if self.{{{name}}}[_key] is not None:
_field_dict_of_array[_key] = [
_item.to_dict() for _item in self.{{{name}}}[_key]
]
_dict['{{{baseName}}}'] = _field_dict_of_array
{{/items.items.isPrimitiveType}}
{{/items.isArray}}
{{^items.isArray}}
{{^items.isPrimitiveType}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def to_dict(self):
_field_dict_of_array = {}
if self.shop_id_to_org_online_lip_map:
for _key in self.shop_id_to_org_online_lip_map:
if self.shop_id_to_org_online_lip_map[_key]:
if self.shop_id_to_org_online_lip_map[_key] is not None:
_field_dict_of_array[_key] = [
_item.to_dict() for _item in self.shop_id_to_org_online_lip_map[_key]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def to_dict(self):
_field_dict_of_array = {}
if self.dict_property:
for _key in self.dict_property:
if self.dict_property[_key]:
if self.dict_property[_key] is not None:
_field_dict_of_array[_key] = [
_item.to_dict() for _item in self.dict_property[_key]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,6 @@ def to_dict(self):
exclude={
},
exclude_none=True)
# override the default output from pydantic by calling `to_dict()` of each value in dict_property (dict of array)
_field_dict_of_array = {}
if self.dict_property:
for _key in self.dict_property:
if self.dict_property[_key]:
_field_dict_of_array[_key] = [
_item.to_dict() for _item in self.dict_property[_key]
]
_dict['dictProperty'] = _field_dict_of_array
return _dict

@classmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def to_dict(self):
_field_dict_of_array = {}
if self.shop_id_to_org_online_lip_map:
for _key in self.shop_id_to_org_online_lip_map:
if self.shop_id_to_org_online_lip_map[_key]:
if self.shop_id_to_org_online_lip_map[_key] is not None:
_field_dict_of_array[_key] = [
_item.to_dict() for _item in self.shop_id_to_org_online_lip_map[_key]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def to_dict(self):
_field_dict_of_array = {}
if self.dict_property:
for _key in self.dict_property:
if self.dict_property[_key]:
if self.dict_property[_key] is not None:
_field_dict_of_array[_key] = [
_item.to_dict() for _item in self.dict_property[_key]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,6 @@ def to_dict(self):
"additional_properties"
},
exclude_none=True)
# override the default output from pydantic by calling `to_dict()` of each value in dict_property (dict of array)
_field_dict_of_array = {}
if self.dict_property:
for _key in self.dict_property:
if self.dict_property[_key]:
_field_dict_of_array[_key] = [
_item.to_dict() for _item in self.dict_property[_key]
]
_dict['dictProperty'] = _field_dict_of_array
# puts key-value pairs in additional_properties in the top level
if self.additional_properties is not None:
for _key, _value in self.additional_properties.items():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,30 @@ def test_additional_properties(self):
a3.additional_properties = { "xyz": 45.6 }
self.assertEqual(a3.to_dict(), {"xyz": 45.6})
self.assertEqual(a3.to_json(), "{\"xyz\": 45.6}")

class TestUnnamedDictWithAdditionalStringListProperties:
def test_empty_dict(self):
a = petstore_api.UnnamedDictWithAdditionalStringListProperties(dict_property={})
assert a.to_dict() == {"dictProperty": {}}

def test_empty_list(self):
a = petstore_api.UnnamedDictWithAdditionalStringListProperties(dict_property={"b": []})
assert a.to_dict() == {"dictProperty": {"b": []}}

def test_single_string_item(self):
a = petstore_api.UnnamedDictWithAdditionalStringListProperties(dict_property={"b": ["c"]})
assert a.to_dict() == {"dictProperty": {"b": ["c"]}}

class TestUnnamedDictWithAdditionalModelListProperties:
def test_empty_dict(self):
a = petstore_api.UnnamedDictWithAdditionalModelListProperties(dict_property={})
assert a.to_dict() == {"dictProperty": {}}

def test_empty_list(self):
a = petstore_api.UnnamedDictWithAdditionalModelListProperties(dict_property={"b": []})
assert a.to_dict() == {"dictProperty": {"b": []}}

def test_single_string_item(self):
value = {"b": [petstore_api.CreatureInfo(name="creature_name")]}
a = petstore_api.UnnamedDictWithAdditionalModelListProperties(dict_property=value)
assert a.to_dict() == {"dictProperty": {"b": [{"name": "creature_name"}]}}

0 comments on commit 188d4ba

Please sign in to comment.