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

to_dict returns wrong enum fields when numbering is not consecutive #93

Closed
michael-sayapin opened this issue Jun 14, 2020 · 2 comments · Fixed by #102
Closed

to_dict returns wrong enum fields when numbering is not consecutive #93

michael-sayapin opened this issue Jun 14, 2020 · 2 comments · Fixed by #102
Labels
bug Something isn't working good first issue Good for newcomers has test Has a (xfail) test that verifies the bugfix or feature small Low effort issue that can easily be picked up
Milestone

Comments

@michael-sayapin
Copy link

Protobuf spec file like this:

syntax = "proto3";
package message;
message TeraMessage {
	int64 timestamp = 1;
	enum MessageType {
		MESSAGE_TYPE_UNKNOWN = 0;
		MESSAGE_TYPE_ACTION_MESSAGE = 1;
		// @exclude MESSAGE_TYPE_COMMAND_MESSAGE = 2; // DEPRECATED
		MESSAGE_TYPE_CONFIG_MESSAGE = 3;
		MESSAGE_TYPE_HEARTBEAT_MESSAGE = 4;
	}
	MessageType message_type = 5;
	bytes message = 6;
}

Generates the following Python bindings:

from dataclasses import dataclass
import betterproto

class TeraMessageMessageType(betterproto.Enum):
    MESSAGE_TYPE_UNKNOWN = 0
    MESSAGE_TYPE_ACTION_MESSAGE = 1
    # NB: notice that 2 is missing
    MESSAGE_TYPE_CONFIG_MESSAGE = 3
    MESSAGE_TYPE_HEARTBEAT_MESSAGE = 4

@dataclass
class TeraMessage(betterproto.Message):
    timestamp: int = betterproto.int64_field(1)
    message_type: "TeraMessageMessageType" = betterproto.enum_field(5)
    message: bytes = betterproto.bytes_field(6)

To reproduce the bug:

>>> from my.path.message import TeraMessage, TeraMessageMessageType
>>> message = TeraMessage(message_type=TeraMessageMessageType.MESSAGE_TYPE_CONFIG_MESSAGE)
>>> message.to_dict()
{'messageType': 'MESSAGE_TYPE_HEARTBEAT_MESSAGE'}
>>> message.to_json()
'{"messageType": "MESSAGE_TYPE_HEARTBEAT_MESSAGE"}'
>>> TeraMessage().parse(bytes(message)).message_type == TeraMessageMessageType.MESSAGE_TYPE_CONFIG_MESSAGE
True
@boukeversteegh boukeversteegh added bug Something isn't working good first issue Good for newcomers small Low effort issue that can easily be picked up labels Jun 14, 2020
@boukeversteegh boukeversteegh added this to the Better Fields milestone Jun 14, 2020
@boukeversteegh
Copy link
Collaborator

Awesome bugreport! Thanks!

Would you be willing to add this test-case to our standard tests?

You basically only need to:

  • add a folder with that proto file in it (and call the message Test)
  • plus a json with the same name containing the faulty value
  • add the name of the folder to betterproto/tests/inputs/config.py in xfail

Details are described here: Standard Tests Development Guide

@michael-sayapin
Copy link
Author

Sure @boukeversteegh ! Created a PR #94

@boukeversteegh boukeversteegh added the has test Has a (xfail) test that verifies the bugfix or feature label Jun 15, 2020
@boukeversteegh boukeversteegh linked a pull request Jun 15, 2020 that will close this issue
boukeversteegh added a commit to boukeversteegh/python-betterproto that referenced this issue Jul 4, 2020
…d to_dict when the enum values are not consecutive.

Also ensure enums work well with integer values and invalid values are rejected.
boukeversteegh added a commit to boukeversteegh/python-betterproto that referenced this issue Jul 11, 2020
boukeversteegh added a commit that referenced this issue Jul 12, 2020
…ive (#102)

Fixes #93 to_dict returns wrong enum fields when numbering is not consecutive
Gobot1234 pushed a commit to Gobot1234/python-betterproto that referenced this issue Aug 24, 2020
…ive (danielgtaylor#102)

Fixes danielgtaylor#93 to_dict returns wrong enum fields when numbering is not consecutive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers has test Has a (xfail) test that verifies the bugfix or feature small Low effort issue that can easily be picked up
Projects
None yet
2 participants