Skip to content

Commit

Permalink
Move check for wrapper types into the message branch of the field mas…
Browse files Browse the repository at this point in the history
…k merge logic
  • Loading branch information
vassilvk committed Apr 12, 2022
1 parent c8a7868 commit cef9f67
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
16 changes: 14 additions & 2 deletions csharp/src/Google.Protobuf.Test/FieldMaskTreeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,9 @@ public void Merge(bool useDynamicMessage)
}

[Test]
public void MergeWrapperFields()
[TestCase(true)]
[TestCase(false)]
public void MergeWrapperFields(bool replaceFields)
{
// Instantiate a destination with wrapper-based field types.
var destination = new TestWellKnownTypes()
Expand All @@ -452,7 +454,17 @@ public void MergeWrapperFields()
};

Merge(new FieldMaskTree().AddFieldPath("string_field").AddFieldPath("int64_field"),
source, destination, new FieldMask.MergeOptions(), false);
source,
destination,
new FieldMask.MergeOptions()
{
// Prove that setting the replace options has no bearing on the outcome of a merge operation
// for well-known wrapper fields as they are semantically equivalent to primitive value fields.
// For those fields "merge" is the same as "replace".
ReplaceMessageFields = replaceFields,
ReplacePrimitiveFields = replaceFields
},
false);

// Make sure only the targeted fields were updated.
Assert.AreEqual("Hi", destination.StringField);
Expand Down
5 changes: 3 additions & 2 deletions csharp/src/Google.Protobuf/FieldMaskTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,10 @@ public void Merge(IMessage source, IMessage destination, FieldMask.MergeOptions
else
{
var sourceField = field.Accessor.GetValue(source);
if (field.FieldType == FieldType.Message && !field.MessageType.IsWrapperType)
if (field.FieldType == FieldType.Message)
{
if (options.ReplaceMessageFields)
// Semantically well-known wrapper types represent primitive values, so we do not "merge" them.
if (options.ReplaceMessageFields || field.MessageType.IsWrapperType)
{
if (sourceField == null)
{
Expand Down

0 comments on commit cef9f67

Please sign in to comment.