diff options
author | Feng Xiao <xfxyjwf@gmail.com> | 2017-04-12 10:42:18 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-12 10:42:18 -0700 |
commit | 8aa927f08f6b92808976b0c22fa4a45850279987 (patch) | |
tree | 9f1c55151e63924378d5dc0a4a6a4853ff77b76a | |
parent | efec7571047c680e11354bd30bf8bd3dfda15ff0 (diff) | |
parent | bf483dfb99a5b7c57ee0a222bd041275cda9cf98 (diff) | |
download | protobuf-8aa927f08f6b92808976b0c22fa4a45850279987.tar.gz protobuf-8aa927f08f6b92808976b0c22fa4a45850279987.tar.bz2 protobuf-8aa927f08f6b92808976b0c22fa4a45850279987.zip |
Merge pull request #2950 from anuraaga/dev_rag
Allow unknown values for Map put*Value methods just like every other …
4 files changed, 8 insertions, 25 deletions
diff --git a/java/core/src/test/java/com/google/protobuf/MapTest.java b/java/core/src/test/java/com/google/protobuf/MapTest.java index 81e951cc..e0d3f0cd 100644 --- a/java/core/src/test/java/com/google/protobuf/MapTest.java +++ b/java/core/src/test/java/com/google/protobuf/MapTest.java @@ -490,19 +490,13 @@ public class MapTest extends TestCase { public void testPutForUnknownEnumValues() throws Exception { TestMap.Builder builder = TestMap.newBuilder() .putInt32ToEnumFieldValue(0, 0) - .putInt32ToEnumFieldValue(1, 1); - - try { - builder.putInt32ToEnumFieldValue(2, 1000); // unknown value. - fail(); - } catch (IllegalArgumentException e) { - // expected - } - + .putInt32ToEnumFieldValue(1, 1) + .putInt32ToEnumFieldValue(2, 1000); // unknown value. TestMap message = builder.build(); assertEquals(0, message.getInt32ToEnumFieldValueOrThrow(0)); assertEquals(1, message.getInt32ToEnumFieldValueOrThrow(1)); - assertEquals(2, message.getInt32ToEnumFieldCount()); + assertEquals(1000, message.getInt32ToEnumFieldValueOrThrow(2)); + assertEquals(3, message.getInt32ToEnumFieldCount()); } public void testPutChecksNullKeysAndValues() throws Exception { @@ -1250,12 +1244,9 @@ public class MapTest extends TestCase { builder.putInt32ToEnumFieldValue(1, TestMap.EnumValue.BAR.getNumber()); assertEquals( TestMap.EnumValue.BAR.getNumber(), builder.getInt32ToEnumFieldValueOrThrow(1)); - try { - builder.putInt32ToEnumFieldValue(1, -1); - fail(); - } catch (IllegalArgumentException e) { - // expected - } + builder.putInt32ToEnumFieldValue(1, -1); + assertEquals(-1, builder.getInt32ToEnumFieldValueOrThrow(1)); + assertEquals(TestMap.EnumValue.UNRECOGNIZED, builder.getInt32ToEnumFieldOrThrow(1)); builder.putStringToInt32Field("a", 1); assertEquals(1, builder.getStringToInt32FieldOrThrow("a")); 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 808e542d..33bc1403 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 @@ -229,9 +229,7 @@ public class JsonFormatTest extends TestCase { TestMap.Builder mapBuilder = TestMap.newBuilder(); mapBuilder.putInt32ToEnumMapValue(1, 0); - Map<Integer, Integer> mapWithInvalidValues = new HashMap<Integer, Integer>(); - mapWithInvalidValues.put(2, 12345); - mapBuilder.putAllInt32ToEnumMapValue(mapWithInvalidValues); + mapBuilder.putInt32ToEnumMapValue(2, 12345); TestMap mapMessage = mapBuilder.build(); assertEquals( "{\n" diff --git a/src/google/protobuf/compiler/java/java_map_field.cc b/src/google/protobuf/compiler/java/java_map_field.cc index 3fe68ae3..608b5378 100644 --- a/src/google/protobuf/compiler/java/java_map_field.cc +++ b/src/google/protobuf/compiler/java/java_map_field.cc @@ -417,9 +417,6 @@ GenerateBuilderMembers(io::Printer* printer) const { " $key_type$ key,\n" " $value_type$ value) {\n" " $key_null_check$\n" - " if ($value_enum_type$.forNumber(value) == null) {\n" - " throw new java.lang.IllegalArgumentException();\n" - " }\n" " internalGetMutable$capitalized_name$().getMutableMap()\n" " .put(key, value);\n" " return this;\n" diff --git a/src/google/protobuf/compiler/java/java_map_field_lite.cc b/src/google/protobuf/compiler/java/java_map_field_lite.cc index 523052cc..d7424c08 100644 --- a/src/google/protobuf/compiler/java/java_map_field_lite.cc +++ b/src/google/protobuf/compiler/java/java_map_field_lite.cc @@ -642,9 +642,6 @@ GenerateBuilderMembers(io::Printer* printer) const { " $key_type$ key,\n" " $value_type$ value) {\n" " $key_null_check$\n" - " if ($value_enum_type$.forNumber(value) == null) {\n" - " throw new java.lang.IllegalArgumentException();\n" - " }\n" " copyOnWrite();\n" " instance.getMutable$capitalized_name$ValueMap().put(key, value);\n" " return this;\n" |