From d83a9f66bcf9dfcee6bfd739ba09f405362036d9 Mon Sep 17 00:00:00 2001 From: Brandon Cole Date: Thu, 30 Aug 2018 11:04:55 -0400 Subject: [PATCH 1/3] Add unit test for loading unknown fields in Any messages Use ./tests.sh cpp to run it --- src/google/protobuf/util/json_util_test.cc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/google/protobuf/util/json_util_test.cc b/src/google/protobuf/util/json_util_test.cc index cbfc7adabb..078d194b76 100644 --- a/src/google/protobuf/util/json_util_test.cc +++ b/src/google/protobuf/util/json_util_test.cc @@ -56,6 +56,7 @@ using proto3::TestEnumValue; using proto3::TestMap; using proto3::TestMessage; using proto3::TestOneof; +using proto3::TestAny; static const char kTypeUrlPrefix[] = "type.googleapis.com"; @@ -357,6 +358,23 @@ TEST_F(JsonUtilTest, TestDynamicMessage) { EXPECT_EQ(ToJson(generated, options), ToJson(*message, options)); } +TEST_F(JsonUtilTest, TestParsingUnknownAnyFields) { + string input = + "{\n" + " \"value\": {\n" + " \"@type\": \"type.googleapis.com/proto3.TestMessage\",\n" + " \"unknown_field\": \"UNKOWN_VALUE\"\n" + " }\n" + "}"; + + TestAny m; + JsonParseOptions options; + EXPECT_FALSE(FromJson(input, &m, options)); + + options.ignore_unknown_fields = true; + EXPECT_TRUE(FromJson(input, &m, options)); +} + TEST_F(JsonUtilTest, TestParsingUnknownEnumsProto2) { string input = "{\n" From 3d32de9b2c05c209461042bb242147c02f84d353 Mon Sep 17 00:00:00 2001 From: Brandon Cole Date: Thu, 30 Aug 2018 11:32:45 -0400 Subject: [PATCH 2/3] Code fixes for the original unit test. Credit goes to @mercret for the fix. --- .../util/internal/protostream_objectwriter.cc | 14 +++++++++++++- .../util/internal/protostream_objectwriter.h | 5 +++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/google/protobuf/util/internal/protostream_objectwriter.cc b/src/google/protobuf/util/internal/protostream_objectwriter.cc index 3d06e12d30..3751e07c43 100644 --- a/src/google/protobuf/util/internal/protostream_objectwriter.cc +++ b/src/google/protobuf/util/internal/protostream_objectwriter.cc @@ -73,6 +73,18 @@ ProtoStreamObjectWriter::ProtoStreamObjectWriter( set_use_lower_camel_for_enums(options_.use_lower_camel_for_enums); } +ProtoStreamObjectWriter::ProtoStreamObjectWriter( + const TypeInfo* typeinfo, const google::protobuf::Type& type, + strings::ByteSink* output, ErrorListener* listener, + const ProtoStreamObjectWriter::Options& options) + : ProtoWriter(typeinfo, type, output, listener), + master_type_(type), + current_(nullptr), + options_(options) { + set_ignore_unknown_fields(options_.ignore_unknown_fields); + set_use_lower_camel_for_enums(options.use_lower_camel_for_enums); +} + ProtoStreamObjectWriter::ProtoStreamObjectWriter( const TypeInfo* typeinfo, const google::protobuf::Type& type, strings::ByteSink* output, ErrorListener* listener) @@ -342,7 +354,7 @@ void ProtoStreamObjectWriter::AnyWriter::StartAny(const DataPiece& value) { // Create our object writer and initialize it with the first StartObject // call. ow_.reset(new ProtoStreamObjectWriter(parent_->typeinfo(), *type, &output_, - parent_->listener())); + parent_->listener(), parent_->options_)); // Don't call StartObject() for well-known types yet. Depending on the // type of actual data, we may not need to call StartObject(). For diff --git a/src/google/protobuf/util/internal/protostream_objectwriter.h b/src/google/protobuf/util/internal/protostream_objectwriter.h index 714c55619e..df56a8bd8e 100644 --- a/src/google/protobuf/util/internal/protostream_objectwriter.h +++ b/src/google/protobuf/util/internal/protostream_objectwriter.h @@ -323,6 +323,11 @@ class LIBPROTOBUF_EXPORT ProtoStreamObjectWriter : public ProtoWriter { const google::protobuf::Type& type, strings::ByteSink* output, ErrorListener* listener); + ProtoStreamObjectWriter(const TypeInfo* typeinfo, + const google::protobuf::Type& type, + strings::ByteSink* output, ErrorListener* listener, + const ProtoStreamObjectWriter::Options& options); + // Returns true if the field is a map. inline bool IsMap(const google::protobuf::Field& field); From 52870ad8fdd96ce27dc371a03552844af4f9a147 Mon Sep 17 00:00:00 2001 From: Brandon Cole Date: Thu, 30 Aug 2018 11:45:39 -0400 Subject: [PATCH 3/3] Also make sure known fields come across as expected --- src/google/protobuf/util/json_util_test.cc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/google/protobuf/util/json_util_test.cc b/src/google/protobuf/util/json_util_test.cc index 078d194b76..125d4d4a03 100644 --- a/src/google/protobuf/util/json_util_test.cc +++ b/src/google/protobuf/util/json_util_test.cc @@ -363,7 +363,8 @@ TEST_F(JsonUtilTest, TestParsingUnknownAnyFields) { "{\n" " \"value\": {\n" " \"@type\": \"type.googleapis.com/proto3.TestMessage\",\n" - " \"unknown_field\": \"UNKOWN_VALUE\"\n" + " \"unknown_field\": \"UNKOWN_VALUE\",\n" + " \"string_value\": \"expected_value\"\n" " }\n" "}"; @@ -373,6 +374,10 @@ TEST_F(JsonUtilTest, TestParsingUnknownAnyFields) { options.ignore_unknown_fields = true; EXPECT_TRUE(FromJson(input, &m, options)); + + TestMessage t; + EXPECT_TRUE(m.value().UnpackTo(&t)); + EXPECT_EQ("expected_value", t.string_value()); } TEST_F(JsonUtilTest, TestParsingUnknownEnumsProto2) {