diff options
author | Caleb Welton <caleb@autonomic.ai> | 2016-11-19 10:54:13 -0800 |
---|---|---|
committer | Caleb Welton <cwelton@apache.org> | 2016-12-05 18:01:20 -0800 |
commit | 1a56251a3582d8b060ca65db6ef1d178c744f81f (patch) | |
tree | 98d6adbfcee91265acb500469f9c21658d408956 /java/util/src | |
parent | cd315dcbadc02569e145bde16e3f66c2fbb08e31 (diff) |
oneOf fix for JsonFormat includingDefaultValueFields
Diffstat (limited to 'java/util/src')
-rw-r--r-- | java/util/src/main/java/com/google/protobuf/util/JsonFormat.java | 19 | ||||
-rw-r--r-- | java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java | 17 |
2 files changed, 30 insertions, 6 deletions
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 6361b4ac..f0a168e2 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 @@ -49,6 +49,7 @@ import com.google.protobuf.Descriptors.EnumDescriptor; import com.google.protobuf.Descriptors.EnumValueDescriptor; import com.google.protobuf.Descriptors.FieldDescriptor; import com.google.protobuf.Descriptors.FileDescriptor; +import com.google.protobuf.Descriptors.OneofDescriptor; import com.google.protobuf.DoubleValue; import com.google.protobuf.Duration; import com.google.protobuf.DynamicMessage; @@ -774,12 +775,18 @@ public class JsonFormat { if (includingDefaultValueFields) { fieldsToPrint = new TreeMap<FieldDescriptor, Object>(); for (FieldDescriptor field : message.getDescriptorForType().getFields()) { - if (field.isOptional() - && field.getJavaType() == FieldDescriptor.JavaType.MESSAGE - && !message.hasField(field)) { - // Always skip empty optional message fields. If not we will recurse indefinitely if - // a message has itself as a sub-field. - continue; + if (field.isOptional()) { + if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE + && !message.hasField(field)){ + // Always skip empty optional message fields. If not we will recurse indefinitely if + // a message has itself as a sub-field. + continue; + } + OneofDescriptor oneof = field.getContainingOneof(); + if (oneof != null && !message.hasField(field)) { + // Skip all oneof fields except the one that is actually set + continue; + } } fieldsToPrint.put(field, message.getField(field)); } 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 32739d44..823ed971 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 @@ -1261,6 +1261,23 @@ public class JsonFormatTest extends TestCase { + " }\n" + "}", JsonFormat.printer().includingDefaultValueFields().print(mapMessage)); + + TestOneof oneofMessage = TestOneof.getDefaultInstance(); + assertEquals("{\n}", JsonFormat.printer().print(oneofMessage)); + assertEquals("{\n}", JsonFormat.printer().includingDefaultValueFields().print(oneofMessage)); + + oneofMessage = TestOneof.newBuilder().setOneofInt32(42).build(); + assertEquals("{\n \"oneofInt32\": 42\n}", + JsonFormat.printer().print(oneofMessage)); + assertEquals("{\n \"oneofInt32\": 42\n}", + JsonFormat.printer().includingDefaultValueFields().print(oneofMessage)); + + TestOneof.Builder oneofBuilder = TestOneof.newBuilder(); + mergeFromJson("{\n" + " \"oneofNullValue\": null \n" + "}", oneofBuilder); + oneofMessage = oneofBuilder.build(); + assertEquals("{\n \"oneofNullValue\": null\n}", JsonFormat.printer().print(oneofMessage)); + assertEquals("{\n \"oneofNullValue\": null\n}", + JsonFormat.printer().includingDefaultValueFields().print(oneofMessage)); } public void testPreservingProtoFieldNames() throws Exception { |