From 80e9085ce3a375648ca8792f8f129c4ce12eee18 Mon Sep 17 00:00:00 2001 From: zajca Date: Fri, 16 Sep 2022 11:44:36 +0200 Subject: [PATCH] fix empty message serialization for Any --- php/src/Google/Protobuf/Internal/Message.php | 8 ++++-- php/tests/EncodeDecodeTest.php | 28 ++++++++++++++++++++ php/tests/proto/test.proto | 4 +++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/php/src/Google/Protobuf/Internal/Message.php b/php/src/Google/Protobuf/Internal/Message.php index 1d1fbf27c560..4a1991839b81 100644 --- a/php/src/Google/Protobuf/Internal/Message.php +++ b/php/src/Google/Protobuf/Internal/Message.php @@ -1980,8 +1980,12 @@ public function jsonByteSize() $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_msg->jsonByteSize() -1; + } } } elseif (get_class($this) === 'Google\Protobuf\FieldMask') { $field_mask = GPBUtil::formatFieldMask($this); diff --git a/php/tests/EncodeDecodeTest.php b/php/tests/EncodeDecodeTest.php index 33d8da179533..29544dc46b42 100644 --- a/php/tests/EncodeDecodeTest.php +++ b/php/tests/EncodeDecodeTest.php @@ -3,6 +3,7 @@ require_once('test_base.php'); require_once('test_util.php'); +use Foo\EmptyAnySerialization; use Google\Protobuf\RepeatedField; use Google\Protobuf\GPBType; use Foo\TestInt32Value; @@ -162,6 +163,15 @@ public function testEncodeTopLevelInt64Value() $this->assertSame("\"1\"", $m->serializeToJsonString()); } + public function testEncodeTopLevelInt64ValueInAny() + { + $m = new Int64Value(); + $m->setValue(1); + $any = new Any(); + $any->pack($m); + $this->assertSame('{"@type":"type.googleapis.com/google.protobuf.Int64Value","value":"1"}', $any->serializeToJsonString()); + } + public function testDecodeTopLevelUInt64Value() { $m = new UInt64Value(); @@ -1513,4 +1523,22 @@ public function wrappersDataProvider() [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()); + } } diff --git a/php/tests/proto/test.proto b/php/tests/proto/test.proto index 421292ab5287..4217800507b0 100644 --- a/php/tests/proto/test.proto +++ b/php/tests/proto/test.proto @@ -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];