mirror of
https://github.com/openSUSE/osc.git
synced 2025-01-03 21:36:15 +01:00
Fix exporting an empty model field in BaseModel.dict()
This commit is contained in:
parent
3baeb4b64e
commit
b4a1b29f57
@ -339,8 +339,10 @@ class BaseModel(metaclass=ModelMeta):
|
||||
for name, field in self.__fields__.items():
|
||||
if field.exclude:
|
||||
continue
|
||||
if field.is_model:
|
||||
result[name] = getattr(self, name).dict()
|
||||
value = getattr(self, name)
|
||||
if value is not None and field.is_model:
|
||||
result[name] = value.dict()
|
||||
else:
|
||||
result[name] = getattr(self, name)
|
||||
result[name] = value
|
||||
|
||||
return result
|
||||
|
@ -186,14 +186,17 @@ class Test(unittest.TestCase):
|
||||
self.assertEqual(field.is_optional, True)
|
||||
self.assertEqual(field.origin_type, TestSubmodel)
|
||||
self.assertEqual(m.field, None)
|
||||
m.dict()
|
||||
|
||||
m = TestModel(field=TestSubmodel())
|
||||
self.assertIsInstance(m.field, TestSubmodel)
|
||||
self.assertEqual(m.field.text, "default")
|
||||
m.dict()
|
||||
|
||||
m = TestModel(field={"text": "text"})
|
||||
self.assertNotEqual(m.field, None)
|
||||
self.assertEqual(m.field.text, "text")
|
||||
m.dict()
|
||||
|
||||
def test_enum(self):
|
||||
class Numbers(Enum):
|
||||
|
Loading…
Reference in New Issue
Block a user