diff options
author | Jisi Liu <jisi.liu@gmail.com> | 2017-10-18 12:22:18 -0700 |
---|---|---|
committer | Jisi Liu <jisi.liu@gmail.com> | 2017-10-18 12:22:18 -0700 |
commit | 1a7a7fca804afa1cf67f8be5e71092898ba40334 (patch) | |
tree | 04b3da27c71c607510f34a12cf7856a1b94181ae /java/util | |
parent | c4f59dcc5c13debc572154c8f636b8a9361aacde (diff) | |
download | protobuf-1a7a7fca804afa1cf67f8be5e71092898ba40334.tar.gz protobuf-1a7a7fca804afa1cf67f8be5e71092898ba40334.tar.bz2 protobuf-1a7a7fca804afa1cf67f8be5e71092898ba40334.zip |
Merge from google internal
Diffstat (limited to 'java/util')
-rw-r--r-- | java/util/src/main/java/com/google/protobuf/util/FieldMaskTree.java | 6 | ||||
-rw-r--r-- | java/util/src/test/java/com/google/protobuf/util/FieldMaskTreeTest.java | 8 |
2 files changed, 12 insertions, 2 deletions
diff --git a/java/util/src/main/java/com/google/protobuf/util/FieldMaskTree.java b/java/util/src/main/java/com/google/protobuf/util/FieldMaskTree.java index b192b53e..e017b987 100644 --- a/java/util/src/main/java/com/google/protobuf/util/FieldMaskTree.java +++ b/java/util/src/main/java/com/google/protobuf/util/FieldMaskTree.java @@ -34,7 +34,6 @@ import com.google.protobuf.Descriptors.Descriptor; import com.google.protobuf.Descriptors.FieldDescriptor; import com.google.protobuf.FieldMask; import com.google.protobuf.Message; - import java.util.ArrayList; import java.util.List; import java.util.Map.Entry; @@ -244,6 +243,11 @@ final class FieldMaskTree { + "singluar message field and cannot have sub-fields."); continue; } + if (!source.hasField(field) && !destination.hasField(field)) { + // If the message field is not present in both source and destination, skip recursing + // so we don't create unneccessary empty messages. + continue; + } String childPath = path.isEmpty() ? entry.getKey() : path + "." + entry.getKey(); merge( entry.getValue(), diff --git a/java/util/src/test/java/com/google/protobuf/util/FieldMaskTreeTest.java b/java/util/src/test/java/com/google/protobuf/util/FieldMaskTreeTest.java index 3ee0fc6e..853b6151 100644 --- a/java/util/src/test/java/com/google/protobuf/util/FieldMaskTreeTest.java +++ b/java/util/src/test/java/com/google/protobuf/util/FieldMaskTreeTest.java @@ -33,7 +33,6 @@ package com.google.protobuf.util; import protobuf_unittest.UnittestProto.NestedTestAllTypes; import protobuf_unittest.UnittestProto.TestAllTypes; import protobuf_unittest.UnittestProto.TestAllTypes.NestedMessage; - import junit.framework.TestCase; public class FieldMaskTreeTest extends TestCase { @@ -222,6 +221,13 @@ public class FieldMaskTreeTest extends TestCase { new FieldMaskTree().addFieldPath("payload").merge(clearedSource, builder, options); assertEquals(false, builder.hasPayload()); + // Skip a message field if they are unset in both source and target. + builder = NestedTestAllTypes.newBuilder(); + new FieldMaskTree() + .addFieldPath("payload.optional_int32") + .merge(clearedSource, builder, options); + assertEquals(false, builder.hasPayload()); + // Change to replace message fields. options.setReplaceMessageFields(true); builder = NestedTestAllTypes.newBuilder(); |