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

feat: make it so you can optionally have DiscardUnknown not apply to enums #46

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

Conversation

djgilcrease
Copy link

70db1e1#diff-4bfd19b0e2b567c0f35f87a774a43846bc5b549661d952fb1177867cd762e6ba made it so DiscardUnknown also applied to enum values following the discussion golang/protobuf#1208 to comply with the C++ behavior.

The comment by golang/protobuf#1208 (comment) directly applies to my situation in that for historical reasons we had AllowUnknownFields set to true in the old jsonpb code. This did not affect ENUMS and allowed our service to properly throw a 400 error for invalid ENUM values (yes I know we do not follow best practice of having the zero value be invalid). In the migration to this code we still need DiscardUnknown to be true, but also retain the behavior of validating enum fields.

To achieve this I added ErrorOnUnknownEnumValue to UnmarshalOptions that will still let DiscardUnknown apply to field names, but NOT ENUM values. This change will have no affect if DiscardUnknown is set to false and no affect on any other field type except enums. I also made the default value of ErrorOnUnknownEnumValue retain current behavior if DiscardUnknown is set to true.

@djgilcrease
Copy link
Author

will work on getting this over where it belongs for actual code review.

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

Successfully merging this pull request may close these issues.

1 participant