Merge pull request #10 from qix/master
Fix serialization of dataclass constructor parameters
This commit is contained in:
commit
d8785b4622
@ -430,6 +430,9 @@ class Message(ABC):
|
|||||||
_group_map: Dict[str, dict]
|
_group_map: Dict[str, dict]
|
||||||
|
|
||||||
def __post_init__(self) -> None:
|
def __post_init__(self) -> None:
|
||||||
|
# Keep track of whether every field was default
|
||||||
|
all_sentinel = True
|
||||||
|
|
||||||
# Set a default value for each field in the class after `__init__` has
|
# Set a default value for each field in the class after `__init__` has
|
||||||
# already been run.
|
# already been run.
|
||||||
group_map: Dict[str, dict] = {"fields": {}, "groups": {}}
|
group_map: Dict[str, dict] = {"fields": {}, "groups": {}}
|
||||||
@ -446,6 +449,7 @@ class Message(ABC):
|
|||||||
|
|
||||||
if getattr(self, field.name) != PLACEHOLDER:
|
if getattr(self, field.name) != PLACEHOLDER:
|
||||||
# Skip anything not set to the sentinel value
|
# Skip anything not set to the sentinel value
|
||||||
|
all_sentinel = False
|
||||||
|
|
||||||
if meta.group:
|
if meta.group:
|
||||||
# This was set, so make it the selected value of the one-of.
|
# This was set, so make it the selected value of the one-of.
|
||||||
@ -456,7 +460,7 @@ class Message(ABC):
|
|||||||
setattr(self, field.name, self._get_field_default(field, meta))
|
setattr(self, field.name, self._get_field_default(field, meta))
|
||||||
|
|
||||||
# Now that all the defaults are set, reset it!
|
# Now that all the defaults are set, reset it!
|
||||||
self.__dict__["_serialized_on_wire"] = False
|
self.__dict__["_serialized_on_wire"] = not all_sentinel
|
||||||
self.__dict__["_unknown_fields"] = b""
|
self.__dict__["_unknown_fields"] = b""
|
||||||
self.__dict__["_group_map"] = group_map
|
self.__dict__["_group_map"] = group_map
|
||||||
|
|
||||||
|
@ -33,6 +33,21 @@ def test_has_field():
|
|||||||
assert betterproto.serialized_on_wire(foo.bar) == False
|
assert betterproto.serialized_on_wire(foo.bar) == False
|
||||||
|
|
||||||
|
|
||||||
|
def test_class_init():
|
||||||
|
@dataclass
|
||||||
|
class Bar(betterproto.Message):
|
||||||
|
name: str = betterproto.string_field(1)
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Foo(betterproto.Message):
|
||||||
|
name: str = betterproto.string_field(1)
|
||||||
|
child: Bar = betterproto.message_field(2)
|
||||||
|
|
||||||
|
foo = Foo(name="foo", child=Bar(name="bar"))
|
||||||
|
|
||||||
|
assert foo.to_dict() == {"name": "foo", "child": {"name": "bar"}}
|
||||||
|
|
||||||
|
|
||||||
def test_enum_as_int_json():
|
def test_enum_as_int_json():
|
||||||
class TestEnum(betterproto.Enum):
|
class TestEnum(betterproto.Enum):
|
||||||
ZERO = 0
|
ZERO = 0
|
||||||
|
Loading…
x
Reference in New Issue
Block a user