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

Format default values for "present" fields regardless of value #7487

Merged
merged 2 commits into from Jun 11, 2020

Conversation

jskeet
Copy link
Contributor

@jskeet jskeet commented May 11, 2020

Fixes #7486.

The semantics are relatively complex to explain, even though the
code isn't particularly complex. We need to be certain that these
are the semantics we want.

// If we know about presence for the field, use that to determine whether
// or not to write the value. We *don't* use this for (non-oneof) message fields,
// so that we can control whether null is written or not via FormatDefaultValues.
if (field.HasPresence && !(field.FieldType == FieldType.Message && field.ContainingOneof == null))
Copy link
Member

Choose a reason for hiding this comment

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

Why can't this just be:

  if (field.HasPresence) {

Why exclude messages and oneofs from this? Any field with presence should be skipped if it is not present.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's for message fields that aren't in a oneof - so that if you've specified "format default values" you still get null. Otherwise this is a breaking change.

@jskeet
Copy link
Contributor Author

jskeet commented May 19, 2020

@haberman: I've rewritten this now, in a way that I think is what we discussed. PTAL. We really need to get this specified in a cross-language way in some sort of flow chart...

@jskeet
Copy link
Contributor Author

jskeet commented Jun 5, 2020

@haberman: Ping?

/// </summary>
private bool ShouldFormatFieldValue(IMessage message, FieldDescriptor field, object value)
{
bool formatDefaultValue = settings.FormatDefaultValues &&
Copy link
Member

Choose a reason for hiding this comment

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

Can this be simply:

bool formatDefaultValue = settings.FormatDefaultValues && !field.HasPresence

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No. If FormatDefaultValues is set to true, then I'd expect a singular int32 field which is either in a proto2 message or explicitly optional in a proto3 message to be formatted regardless of presence... although I can see arguments in favour of making field presence trump FormatDefaultValues.

I think we should probably decide that point, and then decide the implementation separately.

@jskeet
Copy link
Contributor Author

jskeet commented Jun 10, 2020

@haberman: I've added an extra commit along the lines you suggest. PTAL, and if you're happy I'll rebase to squash the two commits together (and reword).

Note that currently if you set FormatDefaultValues it does format repeated fields as an empty array - if that's not what you'd expect, we should fix that at the same time.

Fixes protocolbuffers#7486.

Note that this changes the behavior for message fields where
"WithFormatDefaultValues(true)" has been specified. This is
effectively fixing a bug, but will need to be noted in the release
notes.

Basically, FormatDefaultValues only affects fields that don't
support presence - most commonly, singular primitive non-optional
fields in proto3.
@jskeet
Copy link
Contributor Author

jskeet commented Jun 10, 2020

Thanks - have rebased down to one commit (and clarified the FormatDefaultValues documentation). Will merge on green.

@jskeet jskeet merged commit 1dae8fd into protocolbuffers:master Jun 11, 2020
@jskeet jskeet deleted the csharp-conformance branch June 11, 2020 05:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

C# conformance test failure for proto2 JSON formatting
4 participants