Skip to content

Commit

Permalink
Fix csharp fieldmasktree merge null checking (protocolbuffers#12737)
Browse files Browse the repository at this point in the history
Fixes protocolbuffers#12685 for CSharp, but the Java equivalent probably has the same bug.

Closes protocolbuffers#12737

COPYBARA_INTEGRATE_REVIEW=protocolbuffers#12737 from marner2:fix_csharp_fieldmasktree_merge_null_checking 78c7812
PiperOrigin-RevId: 579314424
  • Loading branch information
marner2 authored and copybara-github committed Nov 3, 2023
1 parent 70cc55a commit c1e0853
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions csharp/src/Google.Protobuf.Test/FieldMaskTreeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,12 @@ public void Merge(bool useDynamicMessage)
Merge(new FieldMaskTree().AddFieldPath("payload.single_int32"),
sourceWithPayloadInt32Unset, destination, options, useDynamicMessage);
Assert.IsNotNull(destination.Payload);

// Clear unset primitive fields even if source payload is cleared
destination = source.Clone();
Merge(new FieldMaskTree().AddFieldPath("payload.single_int32"),
clearedSource, destination, options, useDynamicMessage);
Assert.AreEqual(0, destination.Payload.SingleInt32);
}

[Test]
Expand Down
7 changes: 7 additions & 0 deletions csharp/src/Google.Protobuf/FieldMaskTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,13 @@ private void Merge(
field.Accessor.SetValue(destination, destinationField);
}

if (sourceField == null)
{
// If the message field is not present in the source but is in the destination, create an empty one
// so we can properly handle child entries
sourceField = field.MessageType.Parser.CreateTemplate();
}

var childPath = path.Length == 0 ? entry.Key : path + "." + entry.Key;
Merge(entry.Value, childPath, (IMessage)sourceField, (IMessage)destinationField, options);
continue;
Expand Down

0 comments on commit c1e0853

Please sign in to comment.