forked from loafle/openapi-generator-original
[python-pydantic-v1] Fix unnamed dicts with additional properties (#18112)
* [python-pydantic-v1] pick #16779 * [python] update sample
This commit is contained in:
@@ -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}}
|
||||
|
||||
@@ -57,7 +57,7 @@ class MapOfArrayOfModel(BaseModel):
|
||||
_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]
|
||||
]
|
||||
|
||||
@@ -57,7 +57,7 @@ class UnnamedDictWithAdditionalModelListProperties(BaseModel):
|
||||
_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]
|
||||
]
|
||||
|
||||
@@ -52,15 +52,6 @@ class UnnamedDictWithAdditionalStringListProperties(BaseModel):
|
||||
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
|
||||
|
||||
@@ -59,7 +59,7 @@ class MapOfArrayOfModel(BaseModel):
|
||||
_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]
|
||||
]
|
||||
|
||||
@@ -59,7 +59,7 @@ class UnnamedDictWithAdditionalModelListProperties(BaseModel):
|
||||
_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]
|
||||
]
|
||||
|
||||
@@ -54,15 +54,6 @@ class UnnamedDictWithAdditionalStringListProperties(BaseModel):
|
||||
"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():
|
||||
|
||||
@@ -534,3 +534,30 @@ class ModelTests(unittest.TestCase):
|
||||
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"}]}}
|
||||
Reference in New Issue
Block a user