Skip to content

Commit 3137b3d

Browse files
authored
[python-pydantic-v1] Fix unnamed dicts with additional properties (#18112)
* [python-pydantic-v1] pick #16779 * [python] update sample
1 parent fbe81f0 commit 3137b3d

8 files changed

Lines changed: 34 additions & 23 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,17 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
174174
{{/isArray}}
175175
{{#isMap}}
176176
{{#items.isArray}}
177+
{{^items.items.isPrimitiveType}}
177178
# override the default output from pydantic by calling `to_dict()` of each value in {{{name}}} (dict of array)
178179
_field_dict_of_array = {}
179180
if self.{{{name}}}:
180181
for _key in self.{{{name}}}:
181-
if self.{{{name}}}[_key]:
182+
if self.{{{name}}}[_key] is not None:
182183
_field_dict_of_array[_key] = [
183184
_item.to_dict() for _item in self.{{{name}}}[_key]
184185
]
185186
_dict['{{{baseName}}}'] = _field_dict_of_array
187+
{{/items.items.isPrimitiveType}}
186188
{{/items.isArray}}
187189
{{^items.isArray}}
188190
{{^items.isPrimitiveType}}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def to_dict(self):
5757
_field_dict_of_array = {}
5858
if self.shop_id_to_org_online_lip_map:
5959
for _key in self.shop_id_to_org_online_lip_map:
60-
if self.shop_id_to_org_online_lip_map[_key]:
60+
if self.shop_id_to_org_online_lip_map[_key] is not None:
6161
_field_dict_of_array[_key] = [
6262
_item.to_dict() for _item in self.shop_id_to_org_online_lip_map[_key]
6363
]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def to_dict(self):
5757
_field_dict_of_array = {}
5858
if self.dict_property:
5959
for _key in self.dict_property:
60-
if self.dict_property[_key]:
60+
if self.dict_property[_key] is not None:
6161
_field_dict_of_array[_key] = [
6262
_item.to_dict() for _item in self.dict_property[_key]
6363
]

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,6 @@ def to_dict(self):
5252
exclude={
5353
},
5454
exclude_none=True)
55-
# override the default output from pydantic by calling `to_dict()` of each value in dict_property (dict of array)
56-
_field_dict_of_array = {}
57-
if self.dict_property:
58-
for _key in self.dict_property:
59-
if self.dict_property[_key]:
60-
_field_dict_of_array[_key] = [
61-
_item.to_dict() for _item in self.dict_property[_key]
62-
]
63-
_dict['dictProperty'] = _field_dict_of_array
6455
return _dict
6556

6657
@classmethod

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def to_dict(self):
5959
_field_dict_of_array = {}
6060
if self.shop_id_to_org_online_lip_map:
6161
for _key in self.shop_id_to_org_online_lip_map:
62-
if self.shop_id_to_org_online_lip_map[_key]:
62+
if self.shop_id_to_org_online_lip_map[_key] is not None:
6363
_field_dict_of_array[_key] = [
6464
_item.to_dict() for _item in self.shop_id_to_org_online_lip_map[_key]
6565
]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def to_dict(self):
5959
_field_dict_of_array = {}
6060
if self.dict_property:
6161
for _key in self.dict_property:
62-
if self.dict_property[_key]:
62+
if self.dict_property[_key] is not None:
6363
_field_dict_of_array[_key] = [
6464
_item.to_dict() for _item in self.dict_property[_key]
6565
]

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,6 @@ def to_dict(self):
5454
"additional_properties"
5555
},
5656
exclude_none=True)
57-
# override the default output from pydantic by calling `to_dict()` of each value in dict_property (dict of array)
58-
_field_dict_of_array = {}
59-
if self.dict_property:
60-
for _key in self.dict_property:
61-
if self.dict_property[_key]:
62-
_field_dict_of_array[_key] = [
63-
_item.to_dict() for _item in self.dict_property[_key]
64-
]
65-
_dict['dictProperty'] = _field_dict_of_array
6657
# puts key-value pairs in additional_properties in the top level
6758
if self.additional_properties is not None:
6859
for _key, _value in self.additional_properties.items():

samples/openapi3/client/petstore/python-pydantic-v1/tests/test_model.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,3 +534,30 @@ def test_additional_properties(self):
534534
a3.additional_properties = { "xyz": 45.6 }
535535
self.assertEqual(a3.to_dict(), {"xyz": 45.6})
536536
self.assertEqual(a3.to_json(), "{\"xyz\": 45.6}")
537+
538+
class TestUnnamedDictWithAdditionalStringListProperties:
539+
def test_empty_dict(self):
540+
a = petstore_api.UnnamedDictWithAdditionalStringListProperties(dict_property={})
541+
assert a.to_dict() == {"dictProperty": {}}
542+
543+
def test_empty_list(self):
544+
a = petstore_api.UnnamedDictWithAdditionalStringListProperties(dict_property={"b": []})
545+
assert a.to_dict() == {"dictProperty": {"b": []}}
546+
547+
def test_single_string_item(self):
548+
a = petstore_api.UnnamedDictWithAdditionalStringListProperties(dict_property={"b": ["c"]})
549+
assert a.to_dict() == {"dictProperty": {"b": ["c"]}}
550+
551+
class TestUnnamedDictWithAdditionalModelListProperties:
552+
def test_empty_dict(self):
553+
a = petstore_api.UnnamedDictWithAdditionalModelListProperties(dict_property={})
554+
assert a.to_dict() == {"dictProperty": {}}
555+
556+
def test_empty_list(self):
557+
a = petstore_api.UnnamedDictWithAdditionalModelListProperties(dict_property={"b": []})
558+
assert a.to_dict() == {"dictProperty": {"b": []}}
559+
560+
def test_single_string_item(self):
561+
value = {"b": [petstore_api.CreatureInfo(name="creature_name")]}
562+
a = petstore_api.UnnamedDictWithAdditionalModelListProperties(dict_property=value)
563+
assert a.to_dict() == {"dictProperty": {"b": [{"name": "creature_name"}]}}

0 commit comments

Comments
 (0)