Skip to content

Commit 80e9085

Browse files
committed
fix empty message serialization for Any
1 parent c4644b7 commit 80e9085

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

php/src/Google/Protobuf/Internal/Message.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,8 +1980,12 @@ public function jsonByteSize()
19801980
$size += 9;
19811981
$size += $value_msg->jsonByteSize();
19821982
} else {
1983-
// Size for value. +1 for comma, -2 for "{}".
1984-
$size += $value_msg->jsonByteSize() -1;
1983+
$value_size = $value_msg->jsonByteSize();
1984+
// size === 2 it's empty message {} which is not serialized inside any
1985+
if ($value_size !== 2) {
1986+
// Size for value. +1 for comma, -2 for "{}".
1987+
$size += $value_msg->jsonByteSize() -1;
1988+
}
19851989
}
19861990
} elseif (get_class($this) === 'Google\Protobuf\FieldMask') {
19871991
$field_mask = GPBUtil::formatFieldMask($this);

php/tests/EncodeDecodeTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require_once('test_base.php');
44
require_once('test_util.php');
55

6+
use Foo\EmptyAnySerialization;
67
use Google\Protobuf\RepeatedField;
78
use Google\Protobuf\GPBType;
89
use Foo\TestInt32Value;
@@ -162,6 +163,15 @@ public function testEncodeTopLevelInt64Value()
162163
$this->assertSame("\"1\"", $m->serializeToJsonString());
163164
}
164165

166+
public function testEncodeTopLevelInt64ValueInAny()
167+
{
168+
$m = new Int64Value();
169+
$m->setValue(1);
170+
$any = new Any();
171+
$any->pack($m);
172+
$this->assertSame('{"@type":"type.googleapis.com/google.protobuf.Int64Value","value":"1"}', $any->serializeToJsonString());
173+
}
174+
165175
public function testDecodeTopLevelUInt64Value()
166176
{
167177
$m = new UInt64Value();
@@ -1513,4 +1523,22 @@ public function wrappersDataProvider()
15131523
[TestStringValue::class, "a", "\"a\"", "", "\"\""],
15141524
];
15151525
}
1526+
1527+
public function testEmptyAnySerialization()
1528+
{
1529+
$m = new EmptyAnySerialization();
1530+
1531+
$any = new Any();
1532+
$any->pack($m);
1533+
1534+
$data = $any->serializeToJsonString();
1535+
$this->assertEquals('{"@type":"type.googleapis.com/foo.EmptyAnySerialization"}', $data);
1536+
1537+
$any = new Any();
1538+
$any->mergeFromJsonString($data);
1539+
1540+
$m = $any->unpack();
1541+
$this->assertInstanceOf(EmptyAnySerialization::class, $m);
1542+
$this->assertEquals('', $m->getA());
1543+
}
15161544
}

php/tests/proto/test.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ message ARRAY {
168168
int32 a = 1;
169169
}
170170

171+
message EmptyAnySerialization {
172+
string a = 1;
173+
}
174+
171175
message TestPackedMessage {
172176
repeated int32 repeated_int32 = 90 [packed = true];
173177
repeated int64 repeated_int64 = 91 [packed = true];

0 commit comments

Comments
 (0)