-
-
Notifications
You must be signed in to change notification settings - Fork 43
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
add a parser option to validate oneofs #236
Conversation
The exception looks like: ``` scalapb.json4s.JsonFormatException: Overlapping keys in oneof: primitive, wrapper ```
08551ca
to
58c1301
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for sending the PR. Some comments follow.
@@ -461,6 +461,7 @@ class Printer private (config: Printer.PrinterConfig) { | |||
object Parser { | |||
private final case class ParserConfig( | |||
isIgnoringUnknownFields: Boolean, | |||
failOnOverlappingOneofKeys: Boolean, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like the default behavior to be consistent with the Java implementation, despite of a possible breakage for users. Let's rename to ignoreOverlappingOneofKeys
defaults to false
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the name in the latest commit. However, I chose isIgnoringOverlappingOneofFields
for consistency. Let me know if in fact you want ignoreOverlappingOneofKeys
|
||
private def validateOneofs[A <: GeneratedMessage](message: PMessage) = { | ||
message.value.keys | ||
.groupBy(_.containingOneof) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since I want this logic turned on by default, I'd like to avoid allocation that creates a second copy proportional to each PMessage. Instead, can we maintain a mutable set in fromJsonToPMessage
of all the seen oneof descriptors, and check in L624 whether a duplicate hit occurred and throw as soon as that happened?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I (think) I made this change as you've described in the latest commit.
* fail on overlapping oneof fields by default * update names * use 'field' instead of 'key' for consistency * use 'isIgnoringOverlappingOneofFields' and 'ignoringOverlappingOneofFields' for consistency
Thank you! |
@thesamet what are your thoughts around releasing this? I guess it has to be a major release since it could break existing clients. |
Released 0.11.1. Add a note here regarding the breakage: https://github.com/scalapb/scalapb-json4s/releases/tag/v0.11.1 |
The exception looks like:
Here's a first pass. I'm definitely open to feedback re: different approaches and style.
The java-parser was already throwing an
InvalidProtocolBufferException
for too many keys (and this is now part of the unit tests).I wasn't thrilled with the placement of
validateOneofs()
, but it seemed better and easier than working with the generated code to modify theReads