Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoding of oneof fields only works for the last specified field #171

Closed
rgilton opened this issue Aug 5, 2024 · 5 comments · Fixed by #174
Closed

Decoding of oneof fields only works for the last specified field #171

rgilton opened this issue Aug 5, 2024 · 5 comments · Fixed by #174
Labels

Comments

@rgilton
Copy link

rgilton commented Aug 5, 2024

I am finding that only the last specified field can be successfully decoded for a oneof field.

For example, the assertion in this round-trip test fails:

#!/usr/bin/env python3
from pure_protobuf.annotations import Field
from pure_protobuf.message import BaseMessage
from pure_protobuf.one_of import OneOf
from typing_extensions import Annotated
from dataclasses import dataclass
from typing import ClassVar, Optional


@dataclass
class Message(BaseMessage):
    payload: ClassVar[OneOf] = OneOf()
    which_payload = payload.which_one_of_getter()

    foo: Annotated[Optional[int], Field(1, one_of=payload)] = None
    bar: Annotated[Optional[bool], Field(2, one_of=payload)] = None


message = Message()
message.foo = 3

message = Message.loads(message.dumps())
print(message)
assert message.foo == 3

The output from the print statement is:

Message(foo=None, bar=None)

If the example is modified so it sets and asserts against the bar field, then it works fine.

@eigenein eigenein added the bug label Aug 8, 2024
@eigenein
Copy link
Owner

eigenein commented Aug 8, 2024

👋 Thanks for reporting it, I'll have a look!

Alesh pushed a commit to Alesh/pure-protobuf that referenced this issue Aug 16, 2024

Unverified

The committer email address is not verified.
@Alesh
Copy link

Alesh commented Aug 16, 2024

I has made pull request, in my case this fix worked. The project's tests were also successful.

eigenein added a commit that referenced this issue Aug 16, 2024
eigenein added a commit that referenced this issue Aug 16, 2024
Resolves #171 #172
eigenein added a commit that referenced this issue Aug 16, 2024
Resolves #171 #172
@eigenein
Copy link
Owner

eigenein commented Aug 16, 2024

The root cause is not the decoding, but @dataclass-generated __init__() assigning fields one by one. As soon as it calls __setattr__ with the subsequent None, it resets all the other fields in the one-of group

@eigenein
Copy link
Owner

The fix is released in 3.1.2

@rgilton
Copy link
Author

rgilton commented Aug 20, 2024

Thanks! That has solved the problem I was having :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants