Fix csharp fieldmasktree merge null checking (#12737)

Fixes #12685 for CSharp, but the Java equivalent probably has the same bug.

Closes #12737

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/12737 from marner2:fix_csharp_fieldmasktree_merge_null_checking 78c7812ac3
PiperOrigin-RevId: 579314424
pull/14636/head
marner2 1 year ago committed by Copybara-Service
parent 70cc55aeb2
commit c1e0853f70
  1. 6
      csharp/src/Google.Protobuf.Test/FieldMaskTreeTest.cs
  2. 7
      csharp/src/Google.Protobuf/FieldMaskTree.cs

@ -406,6 +406,12 @@ namespace Google.Protobuf
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]

@ -270,6 +270,13 @@ namespace Google.Protobuf
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;

Loading…
Cancel
Save