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

add a parser option to validate oneofs #236

Merged
merged 2 commits into from
May 22, 2021

Conversation

sttawm
Copy link
Contributor

@sttawm sttawm commented May 7, 2021

The exception looks like:

scalapb.json4s.JsonFormatException: Overlapping keys in oneof: primitive, wrapper

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 the Reads

The exception looks like:
```
scalapb.json4s.JsonFormatException: Overlapping keys in oneof: primitive, wrapper
```
@sttawm sttawm force-pushed the 1117-validate-oneofs branch from 08551ca to 58c1301 Compare May 7, 2021 18:42
Copy link
Contributor

@thesamet thesamet left a 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,
Copy link
Contributor

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.

Copy link
Contributor Author

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)
Copy link
Contributor

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?

Copy link
Contributor Author

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
@thesamet thesamet merged commit 44f68ac into scalapb:master May 22, 2021
@thesamet
Copy link
Contributor

Thank you!

@sttawm
Copy link
Contributor Author

sttawm commented Jun 10, 2021

@thesamet what are your thoughts around releasing this? I guess it has to be a major release since it could break existing clients.

@thesamet
Copy link
Contributor

Released 0.11.1. Add a note here regarding the breakage: https://github.com/scalapb/scalapb-json4s/releases/tag/v0.11.1

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.

2 participants