diff options
author | Jie Luo <anandolee@gmail.com> | 2017-03-17 11:10:24 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-17 11:10:24 -0700 |
commit | c12cc3490bb5dc4bb686a597e10e8025b1617dac (patch) | |
tree | 4932f39a517c3ae4b535c65d84ecfcb611e47052 /src/google | |
parent | 6b27c1f981a9a93918e4039f236ead27165a8e91 (diff) | |
parent | d59592af61852023f549e91683ade4f062c2df83 (diff) | |
download | protobuf-c12cc3490bb5dc4bb686a597e10e8025b1617dac.tar.gz protobuf-c12cc3490bb5dc4bb686a597e10e8025b1617dac.tar.bz2 protobuf-c12cc3490bb5dc4bb686a597e10e8025b1617dac.zip |
Merge pull request #2837 from anandolee/master
DefaultValueObjectWriter should populate oneof message field
Diffstat (limited to 'src/google')
-rw-r--r-- | src/google/protobuf/util/internal/default_value_objectwriter.cc | 5 | ||||
-rw-r--r-- | src/google/protobuf/util/json_util_test.cc | 16 |
2 files changed, 19 insertions, 2 deletions
diff --git a/src/google/protobuf/util/internal/default_value_objectwriter.cc b/src/google/protobuf/util/internal/default_value_objectwriter.cc index 1772219a..250cf0be 100644 --- a/src/google/protobuf/util/internal/default_value_objectwriter.cc +++ b/src/google/protobuf/util/internal/default_value_objectwriter.cc @@ -363,8 +363,9 @@ void DefaultValueObjectWriter::Node::PopulateChildren( } // If oneof_index() != 0, the child field is part of a "oneof", which means - // the child field is optional and we shouldn't populate its default value. - if (field.oneof_index() != 0) continue; + // the child field is optional and we shouldn't populate its default + // primitive value. + if (field.oneof_index() != 0 && kind == PRIMITIVE) continue; // If the child field is of primitive type, sets its data to the default // value of its type. diff --git a/src/google/protobuf/util/json_util_test.cc b/src/google/protobuf/util/json_util_test.cc index b0c2f494..53c8a0f8 100644 --- a/src/google/protobuf/util/json_util_test.cc +++ b/src/google/protobuf/util/json_util_test.cc @@ -51,6 +51,7 @@ using proto3::FOO; using proto3::BAR; using proto3::TestMessage; using proto3::TestMap; +using proto3::TestOneof; using testing::MapIn; static const char kTypeUrlPrefix[] = "type.googleapis.com"; @@ -232,6 +233,21 @@ TEST_F(JsonUtilTest, ParsePrimitiveMapIn) { EXPECT_EQ(message.DebugString(), other.DebugString()); } +TEST_F(JsonUtilTest, PrintPrimitiveOneof) { + TestOneof message; + JsonPrintOptions options; + options.always_print_primitive_fields = true; + message.mutable_oneof_message_value(); + EXPECT_EQ( + "{\"oneofMessageValue\":{\"value\":0}}", + ToJson(message, options)); + + message.set_oneof_int32_value(1); + EXPECT_EQ( + "{\"oneofInt32Value\":1}", + ToJson(message, options)); +} + TEST_F(JsonUtilTest, TestParseIgnoreUnknownFields) { TestMessage m; JsonParseOptions options; |