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

THRIFT-5693 - fix bug in serialization of enum default values #2783

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

andrewcunnin
Copy link

@andrewcunnin andrewcunnin commented Apr 13, 2023

This change fixes a bug where the default values set in enum fields were unable to be serialized. Previously default enum values were being stored as enum objects upon initialization, where Thrift generally expects I32 values. With the changes in this pull request, serialization and deserialization are fixed and the python 3.4 style enums continue to work as expected.

JIRA ticket here: https://issues.apache.org/jira/browse/THRIFT-5693

  • Did you create an Apache Jira ticket? (Request account here, not required for trivial changes)
  • If a ticket exists: Does your pull request title follow the pattern "THRIFT-NNNN: describe my issue"?
  • Did you squash your changes to a single commit? (not required, but preferred)
  • Did you do your best to avoid breaking changes? If one was needed, did you label the Jira ticket with "Breaking-Change"?
  • If your change does not involve any code, include [skip ci] anywhere in the commit message to free up build resources.

test/ThriftTest.thrift Outdated Show resolved Hide resolved
test/ThriftTest.thrift Outdated Show resolved Hide resolved
@fishy
Copy link
Member

fishy commented Apr 13, 2023

we actually don't have python set up in github actions yet so this is not really tested (we only tested that it did not break other languages that's setup in github actions)

can you show the diff between old and new compiler generated python code?

@fishy fishy added the python label Apr 13, 2023
@Jens-G Jens-G changed the title [THRIFT-5693] - fix bug in serialization of enum default values THRIFT-5693 - fix bug in serialization of enum default values Apr 13, 2023
@andrewcunnin
Copy link
Author

Gotcha, here's a diff of the ThriftTest.thrift generated Python:
https://gist.github.com/andrewcunnin/802f4d8fa779c181e2272b5219b52a07

std::ostringstream out;
out << tfield->get_value()->get_integer();
return out.str();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this diff from ttypes.py:

@@ -2931,7 +2931,7 @@ class OptionalEnum(object):
     )
 
 
-    def __init__(self, e=    Numberz.FIVE,):
+    def __init__(self, e=5,):
         self.e = e
 
     def read(self, iprot):

and also this diff:

@@ -7198,7 +7198,7 @@ OptionalBinary.thrift_spec = (
 all_structs.append(OptionalEnum)
 OptionalEnum.thrift_spec = (
     None,  # 0
-    (1, TType.I32, 'e', None,     Numberz.FIVE, ),  # 1
+    (1, TType.I32, 'e', None, 5, ),  # 1
 )
 fix_spec(all_structs)
 del all_structs

which I think is caused by this part of the change. from the diff I don't think this change is necessary? (it's probably still correct, but the old code generated seems to be better?)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good catch. That default value change was because I thought Thrift was generally expecting int values even representing Python enums, but after re-testing I've found that that was only an issue for serialization and deserialization. Removed that change and here's the updated diff: https://gist.github.com/andrewcunnin/ba688afe22f943c7b9f96e67adb3bc53

Copy link
Member

@fishy fishy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code looks good to me from the diff generated, but I'm not familiar with the python code enough so I'd still prefer to have some tests to verify it, and I failed to run python3 tests locally similar to how we didn't get the CI setup work in #2787 (comment).

obj = self._deserialize(OptionalEnum, self._serialize(self.optional_enum))
self.assertEquals(obj, self.optional_enum)


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

excessive blank lines. I think the general python style is 2 blank lines between classes/top level stuff and one blank line between functions inside the same class. here you are making it 3 blank lines between classes.

Comment on lines +463 to +464


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, you are making it 4 blank lines between the class above and suite.

@andrewcunnin
Copy link
Author

For the testing, is running the cross test and running make -k check in test/py sufficient? Or were you referring to a different set of tests that I should be running separately?

@fishy
Copy link
Member

fishy commented Apr 18, 2023

For the testing, is running the cross test and running make -k check in test/py sufficient? Or were you referring to a different set of tests that I should be running separately?

we have tests under both test/py and lib/py/test, and then we also have cross tests between different language libraries.

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