Skip to content

Commit f5a3eee

Browse files
committed
betterproto: handle struct in to_dict as well
Signed-off-by: William Woodruff <[email protected]>
1 parent cd39656 commit f5a3eee

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/betterproto/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,15 @@ def to_dict(
14031403
Dict[:class:`str`, Any]
14041404
The JSON serializable dict representation of this object.
14051405
"""
1406+
# Mirror of from_dict: Struct's `fields` member is transparently
1407+
# dispatched through instead.
1408+
if isinstance(self, Struct):
1409+
output = {**self.fields}
1410+
for k in self.fields:
1411+
if hasattr(self.fields[k], "to_dict"):
1412+
output[k] = self.fields[k].to_dict(casing, include_default_values)
1413+
return output
1414+
14061415
output: Dict[str, Any] = {}
14071416
field_types = self._type_hints()
14081417
defaults = self._betterproto.default_gen

0 commit comments

Comments
 (0)