From c78dbd7c895b8c80cd2f2401d73acf890edf82c6 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Fri, 14 Jul 2017 15:48:48 -0600 Subject: Initial value in generated code cannot be used by c extension. (#3367) In the generated code of previous versions, each php field is given an initial value. In c extension, it was assumed that the field order in the generated code is consistent with upb fields order, so that the correct initial value can be bound to the correct upb field. However, this may not be true. The order of fields in generated code is decided by proto compiler, while the order of upb fields is decided by the hash function used in c extension. This PR fixes the issue by reset the initial value at runtime. --- php/tests/encode_decode_test.php | 8 ++++++++ php/tests/memory_leak_test.php | 1 + php/tests/proto/test.proto | 6 ++++++ 3 files changed, 15 insertions(+) (limited to 'php/tests') diff --git a/php/tests/encode_decode_test.php b/php/tests/encode_decode_test.php index 2ea360b2..b43dffb4 100644 --- a/php/tests/encode_decode_test.php +++ b/php/tests/encode_decode_test.php @@ -9,6 +9,7 @@ use Foo\TestEnum; use Foo\TestMessage; use Foo\TestMessage_Sub; use Foo\TestPackedMessage; +use Foo\TestRandomFieldOrder; use Foo\TestUnpackedMessage; class EncodeDecodeTest extends TestBase @@ -235,6 +236,13 @@ class EncodeDecodeTest extends TestBase $this->assertEquals(-1, $m->getOptionalInt32()); } + public function testRandomFieldOrder() + { + $m = new TestRandomFieldOrder(); + $data = $m->serializeToString(); + $this->assertSame("", $data); + } + /** * @expectedException Exception */ diff --git a/php/tests/memory_leak_test.php b/php/tests/memory_leak_test.php index 6572fdd0..faa1833d 100644 --- a/php/tests/memory_leak_test.php +++ b/php/tests/memory_leak_test.php @@ -20,6 +20,7 @@ require_once('generated/Foo/TestMessage_NestedEnum.php'); require_once('generated/Foo/TestMessage_Sub.php'); require_once('generated/Foo/TestPackedMessage.php'); require_once('generated/Foo/TestPhpDoc.php'); +require_once('generated/Foo/TestRandomFieldOrder.php'); require_once('generated/Foo/TestUnpackedMessage.php'); require_once('generated/GPBMetadata/Proto/Test.php'); require_once('generated/GPBMetadata/Proto/TestEmptyPhpNamespace.php'); diff --git a/php/tests/proto/test.proto b/php/tests/proto/test.proto index dada8b48..d81f66f5 100644 --- a/php/tests/proto/test.proto +++ b/php/tests/proto/test.proto @@ -181,3 +181,9 @@ message TestIncludeNamespaceMessage { TestNamespace namespace_message = 1; TestEmptyNamespace empty_namespace_message = 2; } + +// This will cause upb fields not ordered by the order in the generated code. +message TestRandomFieldOrder { + int64 tag13 = 150; + string tag14 = 160; +} -- cgit v1.2.3