aboutsummaryrefslogtreecommitdiff
path: root/java/util
diff options
context:
space:
mode:
authorFeng Xiao <xfxyjwf@gmail.com>2018-08-08 17:14:42 -0700
committerFeng Xiao <xfxyjwf@gmail.com>2018-08-08 17:14:42 -0700
commita8465b738e5a1f346ece58ea6f82612bcd6659d2 (patch)
treeb755540a5d7d2cc988eabc7ac594e6abd9da29d1 /java/util
parent6bbe197e9c1b6fc38cbdc45e3bf83fa7ced792a3 (diff)
parent9bbc4b1fab8262958c172f2d8e87b486c950a051 (diff)
downloadprotobuf-a8465b738e5a1f346ece58ea6f82612bcd6659d2.tar.gz
protobuf-a8465b738e5a1f346ece58ea6f82612bcd6659d2.tar.bz2
protobuf-a8465b738e5a1f346ece58ea6f82612bcd6659d2.zip
Merge master branch.
Diffstat (limited to 'java/util')
-rw-r--r--java/util/pom.xml2
-rw-r--r--java/util/src/main/java/com/google/protobuf/util/FieldMaskTree.java2
-rw-r--r--java/util/src/main/java/com/google/protobuf/util/JsonFormat.java17
-rw-r--r--java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java51
4 files changed, 65 insertions, 7 deletions
diff --git a/java/util/pom.xml b/java/util/pom.xml
index 8ea4a40d..f175cf15 100644
--- a/java/util/pom.xml
+++ b/java/util/pom.xml
@@ -6,7 +6,7 @@
<parent>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-parent</artifactId>
- <version>3.6.0</version>
+ <version>3.6.1</version>
</parent>
<artifactId>protobuf-java-util</artifactId>
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 7b4facc1..86f56ad9 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
@@ -240,7 +240,7 @@ final class FieldMaskTree {
"Field \""
+ field.getFullName()
+ "\" is not a "
- + "singluar message field and cannot have sub-fields.");
+ + "singular message field and cannot have sub-fields.");
continue;
}
if (!source.hasField(field) && !destination.hasField(field)) {
diff --git a/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java b/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java
index 973f1517..955dfd86 100644
--- a/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java
+++ b/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java
@@ -50,6 +50,7 @@ import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.EnumDescriptor;
import com.google.protobuf.Descriptors.EnumValueDescriptor;
import com.google.protobuf.Descriptors.FieldDescriptor;
+import com.google.protobuf.Descriptors.FieldDescriptor.Type;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Descriptors.OneofDescriptor;
import com.google.protobuf.DoubleValue;
@@ -1539,7 +1540,11 @@ public class JsonFormat {
Object key = parseFieldValue(keyField, new JsonPrimitive(entry.getKey()), entryBuilder);
Object value = parseFieldValue(valueField, entry.getValue(), entryBuilder);
if (value == null) {
- throw new InvalidProtocolBufferException("Map value cannot be null.");
+ if(ignoringUnknownFields && valueField.getType() == Type.ENUM) {
+ continue;
+ } else {
+ throw new InvalidProtocolBufferException("Map value cannot be null.");
+ }
}
entryBuilder.setField(keyField, key);
entryBuilder.setField(valueField, value);
@@ -1557,8 +1562,12 @@ public class JsonFormat {
for (int i = 0; i < array.size(); ++i) {
Object value = parseFieldValue(field, array.get(i), builder);
if (value == null) {
- throw new InvalidProtocolBufferException(
- "Repeated field elements cannot be null in field: " + field.getFullName());
+ if(ignoringUnknownFields && field.getType() == Type.ENUM) {
+ continue;
+ } else {
+ throw new InvalidProtocolBufferException(
+ "Repeated field elements cannot be null in field: " + field.getFullName());
+ }
}
builder.addRepeatedField(field, value);
}
@@ -1748,7 +1757,7 @@ public class JsonFormat {
// an exception later.
}
- if (result == null) {
+ if (result == null && !ignoringUnknownFields) {
throw new InvalidProtocolBufferException(
"Invalid enum value: " + value + " for enum type: " + enumDescriptor.getFullName());
}
diff --git a/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java b/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java
index d9bbe587..7637c267 100644
--- a/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java
+++ b/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java
@@ -161,6 +161,10 @@ public class JsonFormatTest extends TestCase {
JsonFormat.parser().merge(json, builder);
}
+ private void mergeFromJsonIgnoringUnknownFields(String json, Message.Builder builder) throws IOException {
+ JsonFormat.parser().ignoringUnknownFields().merge(json, builder);
+ }
+
public void testAllFields() throws Exception {
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
setAllFields(builder);
@@ -668,10 +672,22 @@ public class JsonFormatTest extends TestCase {
+ "}",
builder);
fail();
+
} catch (InvalidProtocolBufferException e) {
// Exception expected.
}
}
+
+ public void testMapEnumNullValueIsIgnored() throws Exception {
+ TestMap.Builder builder = TestMap.newBuilder();
+ mergeFromJsonIgnoringUnknownFields(
+ "{\n"
+ + " \"int32ToEnumMap\": {\"1\": null}\n"
+ + "}",
+ builder);
+ TestMap map = builder.build();
+ assertEquals(0, map.getInt32ToEnumMapMap().entrySet().size());
+ }
public void testParserAcceptNonQuotedObjectKey() throws Exception {
TestMap.Builder builder = TestMap.newBuilder();
@@ -1172,7 +1188,40 @@ public class JsonFormatTest extends TestCase {
String json = "{\n" + " \"unknownField\": \"XXX\"\n" + "}";
JsonFormat.parser().ignoringUnknownFields().merge(json, builder);
}
-
+
+ public void testParserIgnoringUnknownEnums() throws Exception {
+ TestAllTypes.Builder builder = TestAllTypes.newBuilder();
+ String json = "{\n" + " \"optionalNestedEnum\": \"XXX\"\n" + "}";
+ JsonFormat.parser().ignoringUnknownFields().merge(json, builder);
+ assertEquals(0, builder.getOptionalNestedEnumValue());
+ }
+
+ public void testUnknownEnumMap() throws Exception {
+ TestMap.Builder builder = TestMap.newBuilder();
+ JsonFormat.parser().ignoringUnknownFields().merge(
+ "{\n"
+ + " \"int32ToEnumMap\": {1: XXX, 2: FOO}"
+ + "}",
+ builder);
+
+ assertEquals(NestedEnum.FOO, builder.getInt32ToEnumMapMap().get(2));
+ assertEquals(1, builder.getInt32ToEnumMapMap().size());
+ }
+
+ public void testRepeatedUnknownEnum() throws Exception {
+ TestAllTypes.Builder builder = TestAllTypes.newBuilder();
+ JsonFormat.parser().ignoringUnknownFields().merge(
+ "{\n"
+ + " \"repeatedNestedEnum\": [XXX, FOO, BAR, BAZ]"
+ + "}",
+ builder);
+
+ assertEquals(NestedEnum.FOO, builder.getRepeatedNestedEnum(0));
+ assertEquals(NestedEnum.BAR, builder.getRepeatedNestedEnum(1));
+ assertEquals(NestedEnum.BAZ, builder.getRepeatedNestedEnum(2));
+ assertEquals(3, builder.getRepeatedNestedEnumList().size());
+ }
+
public void testParserIntegerEnumValue() throws Exception {
TestAllTypes.Builder actualBuilder = TestAllTypes.newBuilder();
mergeFromJson("{\n" + " \"optionalNestedEnum\": 2\n" + "}", actualBuilder);