-
Notifications
You must be signed in to change notification settings - Fork 43
JSON serialization of default value for fields with explicit presence #238
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
Conversation
| assertEquals(JsonPrimitive(TestAllTypesProto3.NestedEnum.fromValue(0).name!!), parsedJson["optionalNestedEnum"]) | ||
|
|
||
| assertEquals(JsonNull, parsedJson["optionalNestedMessage"]) | ||
| assertFalse("optionalNestedMessage" in parsedJson) |
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 looked deeper into this, please see explanation in PR description about this.
|
|
||
| if (value == null && fd.oneofMember) continue | ||
| if (!fd.oneofMember && !jsonConfig.outputDefaultValues && fd.type.isDefaultValue(value)) continue | ||
| val oneOfOrExplicitPresence = (fd.oneofMember || fd.type.hasPresence) |
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.
If I remember right, I think fd.type.hasPresence will always be true for oneof fields. So you might be able to simplify this and only test for fd.type.hasPresence and drop the explicit test for fd.oneofMember.
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 applied the change, thank you. The tests that we have still pass.
I tried quickly to trace where hasPresence is being set for oneOf fields but I couldn't find it easily :) I would appreciate a pointer so I can better understand what I'm doing :)
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.
hasPresence in the generated code is set here:
| type == File.Field.Type.ENUM -> "Enum(enumCompanion = $kotlinQualifiedTypeName.Companion" + (if (hasPresence) ", hasPresence = true" else "") + ")" |
File.Field.Numbered.hasPresence, which is set here during the parsing of the proto files: | hasPresence = (fieldDesc.label != FieldDescriptorProto.Label.REPEATED) && |
…#238) Fixes #235 * Change the behavior for JSON serialization for fields with explicit presence. * If unset -> don't emit on the wire. * If set -> emit on the wire, even if the value is default (regardless of the `jsonConfig.outputDefaultValues`) This is consistent with the following: https://github.com/protocolbuffers/protobuf/blob/main/docs/field_presence.md#semantic-differences This PR also changes behavior of `outputDefaultValues` for unset nested message fields. Before we were outputting `null` and now we don't output any value. This is consistent with c++, python and java, as explained here: noom/protobuf#5 Run all unit tests in `runtime`
Motivation
Fixes #235
Changes
jsonConfig.outputDefaultValues)This is consistent with the following:
https://github.com/protocolbuffers/protobuf/blob/main/docs/field_presence.md#semantic-differences
This PR also changes behavior of
outputDefaultValuesfor unset nested message fields. Before we were outputtingnulland now we don't output any value. This is consistent with c++, python and java, as explained here: noom/protobuf#5Tested
Run all unit tests in
runtime