Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions betterproto2/src/betterproto2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,11 +606,11 @@ def _value_to_dict(
return value, not bool(value)


def _value_from_dict(value: Any, meta: FieldMetadata, field_type: type) -> Any:
def _value_from_dict(value: Any, meta: FieldMetadata, field_type: type, ignore_unknown_fields: bool) -> Any:
if meta.proto_type == TYPE_MESSAGE:
msg_cls = meta.unwrap() if meta.unwrap else field_type

msg = msg_cls.from_dict(value)
msg = msg_cls.from_dict(value, ignore_unknown_fields=ignore_unknown_fields)

if meta.unwrap:
return msg.to_wrapped()
Expand Down Expand Up @@ -1152,9 +1152,9 @@ def _from_dict_init(cls, mapping: Mapping[str, Any] | Any, *, ignore_unknown_fie

if meta.proto_type == TYPE_MESSAGE:
if meta.repeated:
value = [_value_from_dict(item, meta, field_cls) for item in value]
value = [_value_from_dict(item, meta, field_cls, ignore_unknown_fields) for item in value]
else:
value = _value_from_dict(value, meta, field_cls)
value = _value_from_dict(value, meta, field_cls, ignore_unknown_fields)

elif meta.proto_type == TYPE_MAP:
assert meta.map_meta
Expand All @@ -1163,15 +1163,17 @@ def _from_dict_init(cls, mapping: Mapping[str, Any] | Any, *, ignore_unknown_fie
value_cls = cls._betterproto.cls_by_field[f"{field_name}.value"]

value = {
_value_from_dict(k, meta.map_meta[0], type(None)): _value_from_dict(v, meta.map_meta[1], value_cls)
_value_from_dict(k, meta.map_meta[0], type(None), ignore_unknown_fields): _value_from_dict(
v, meta.map_meta[1], value_cls, ignore_unknown_fields
)
for k, v in value.items()
}

elif meta.repeated:
value = [_value_from_dict(item, meta, field_cls) for item in value]
value = [_value_from_dict(item, meta, field_cls, ignore_unknown_fields) for item in value]

else:
value = _value_from_dict(value, meta, field_cls)
value = _value_from_dict(value, meta, field_cls, ignore_unknown_fields)

init_kwargs[field_name] = value
return init_kwargs
Expand Down
20 changes: 20 additions & 0 deletions betterproto2/tests/test_nested.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def test_nested_from_dict():
"""
Make sure that from_dict() arguments are passed recursively
"""
from tests.outputs.nested.nested import Test

data = {
"nested": {"count": 1},
"sibling": {"foo": 2},
}
Test.from_dict(data)

data["bar"] = 3
Test.from_dict(data, ignore_unknown_fields=True)

data["nested"]["bar"] = 3
Test.from_dict(data, ignore_unknown_fields=True)

data["sibling"]["bar"] = 4
Test.from_dict(data, ignore_unknown_fields=True)