fix empty message serialization for Any

pull/10595/head
zajca 2 years ago
parent c4644b77cb
commit 743c675c4f
  1. 8
      php/src/Google/Protobuf/Internal/Message.php
  2. 19
      php/tests/EncodeDecodeTest.php
  3. 4
      php/tests/proto/test.proto

@ -1980,8 +1980,12 @@ class Message
$size += 9;
$size += $value_msg->jsonByteSize();
} else {
// Size for value. +1 for comma, -2 for "{}".
$size += $value_msg->jsonByteSize() -1;
$value_size = $value_msg->jsonByteSize();
// size === 2 it's empty message {} which is not serialized inside any
if ($value_size !== 2) {
// Size for value. +1 for comma, -2 for "{}".
$size += $value_size -1;
}
}
} elseif (get_class($this) === 'Google\Protobuf\FieldMask') {
$field_mask = GPBUtil::formatFieldMask($this);

@ -5,6 +5,7 @@ require_once('test_util.php');
use Google\Protobuf\RepeatedField;
use Google\Protobuf\GPBType;
use Foo\EmptyAnySerialization;
use Foo\TestInt32Value;
use Foo\TestInt64Value;
use Foo\TestUInt32Value;
@ -1513,4 +1514,22 @@ class EncodeDecodeTest extends TestBase
[TestStringValue::class, "a", "\"a\"", "", "\"\""],
];
}
public function testEmptyAnySerialization()
{
$m = new EmptyAnySerialization();
$any = new Any();
$any->pack($m);
$data = $any->serializeToJsonString();
$this->assertEquals('{"@type":"type.googleapis.com/foo.EmptyAnySerialization"}', $data);
$any = new Any();
$any->mergeFromJsonString($data);
$m = $any->unpack();
$this->assertInstanceOf(EmptyAnySerialization::class, $m);
$this->assertEquals('', $m->getA());
}
}

@ -168,6 +168,10 @@ message ARRAY {
int32 a = 1;
}
message EmptyAnySerialization {
string a = 1;
}
message TestPackedMessage {
repeated int32 repeated_int32 = 90 [packed = true];
repeated int64 repeated_int64 = 91 [packed = true];

Loading…
Cancel
Save