Unverified Commit 6a4fec61 authored by Feng Xiao's avatar Feng Xiao Committed by GitHub

Merge pull request #4813 from htuch/fix-any-in-map

protostream_objectwriter: fix bug when Any is directly embedded in a …
parents f35669b8 2d31d0c7
......@@ -534,7 +534,7 @@ ProtoStreamObjectWriter* ProtoStreamObjectWriter::StartObject(
Push("", Item::MESSAGE, false, false);
ProtoWriter::RenderDataPiece("key",
DataPiece(name, use_strict_base64_decoding()));
Push("value", Item::MESSAGE, true, false);
Push("value", IsAny(*Lookup("value")) ? Item::ANY : Item::MESSAGE, true, false);
// Make sure we are valid so far after starting map fields.
if (invalid_depth() > 0) return this;
......
......@@ -1679,6 +1679,22 @@ TEST_P(ProtoStreamObjectWriterMapTest, RepeatedMapKeyTest) {
->EndObject();
}
TEST_P(ProtoStreamObjectWriterMapTest, AnyInMap) {
MapIn mm;
google::protobuf::DoubleValue d;
d.set_value(40.2);
(*mm.mutable_map_any())["foo"].PackFrom(d);
ow_->StartObject("")
->StartObject("map_any")
->StartObject("foo")
->RenderString("@type", "type.googleapis.com/google.protobuf.DoubleValue")
->RenderDouble("value", 40.2)
->EndObject()
->EndObject()
->EndObject();
CheckOutput(mm);
}
class ProtoStreamObjectWriterAnyTest : public BaseProtoStreamObjectWriterTest {
protected:
ProtoStreamObjectWriterAnyTest() {
......
......@@ -32,6 +32,8 @@ syntax = "proto3";
package google.protobuf.testing;
import "google/protobuf/any.proto";
// Top-level test cases proto used by MarshallingTest. See description
// at the top of the class MarshallingTest for details on how to write
// test cases.
......@@ -103,6 +105,7 @@ message MapIn {
string other = 1;
repeated string things = 2;
map<string, string> map_input = 3;
map<string, google.protobuf.Any> map_any = 4;
}
message MapOut {
......
......@@ -284,7 +284,7 @@ TEST_F(JsonUtilTest, ParsePrimitiveMapIn) {
JsonPrintOptions print_options;
print_options.always_print_primitive_fields = true;
JsonParseOptions parse_options;
EXPECT_EQ("{\"other\":\"\",\"things\":[],\"mapInput\":{}}",
EXPECT_EQ("{\"other\":\"\",\"things\":[],\"mapInput\":{},\"mapAny\":{}}",
ToJson(message, print_options));
MapIn other;
ASSERT_TRUE(FromJson(ToJson(message, print_options), &other, parse_options));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment