From f50204dab41650a8673996c9dec06b0f0fde2495 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Mon, 30 Sep 2019 15:53:30 -0700 Subject: [PATCH] Initialize well known type values (#6713) (#6714) * Initialize well known type values Also fixes #6667 * Revert unwanted change --- php/ext/google/protobuf/message.c | 128 ++++++++++++++++-------------- php/tests/encode_decode_test.php | 9 +++ php/tests/memory_leak_test.php | 1 + php/tests/proto/test.proto | 5 ++ 4 files changed, 82 insertions(+), 61 deletions(-) diff --git a/php/ext/google/protobuf/message.c b/php/ext/google/protobuf/message.c index d60124ef08bf..303f5d4741f2 100644 --- a/php/ext/google/protobuf/message.c +++ b/php/ext/google/protobuf/message.c @@ -1770,19 +1770,19 @@ zend_class_entry* api_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Api", Api, api) - zend_declare_property_null(api_type, "name", strlen("name"), - ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_string(api_type, "name", strlen("name"), "", + ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(api_type, "methods", strlen("methods"), ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(api_type, "options", strlen("options"), ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(api_type, "version", strlen("version"), - ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_string(api_type, "version", strlen("version"), "", + ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(api_type, "source_context", strlen("source_context"), ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(api_type, "mixins", strlen("mixins"), ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(api_type, "syntax", strlen("syntax"), + zend_declare_property_long(api_type, "syntax", strlen("syntax"), 0, ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -1816,7 +1816,7 @@ zend_class_entry* bool_value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\BoolValue", BoolValue, bool_value) - zend_declare_property_null(bool_value_type, "value", strlen("value"), + zend_declare_property_bool(bool_value_type, "value", strlen("value"), 0, ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -1844,8 +1844,8 @@ zend_class_entry* bytes_value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\BytesValue", BytesValue, bytes_value) - zend_declare_property_null(bytes_value_type, "value", strlen("value"), - ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_string(bytes_value_type, "value", strlen("value"), "", + ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(BytesValue, __construct) { @@ -1872,8 +1872,8 @@ zend_class_entry* double_value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\DoubleValue", DoubleValue, double_value) - zend_declare_property_null(double_value_type, "value", strlen("value"), - ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_double(double_value_type, "value", strlen("value"), 0, + ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(DoubleValue, __construct) { @@ -1908,15 +1908,15 @@ zend_class_entry* enum_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Enum", Enum, enum) - zend_declare_property_null(enum_type, "name", strlen("name"), - ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_string(enum_type, "name", strlen("name"), "", + ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(enum_type, "enumvalue", strlen("enumvalue"), ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(enum_type, "options", strlen("options"), ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(enum_type, "source_context", strlen("source_context"), ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(enum_type, "syntax", strlen("syntax"), + zend_declare_property_long(enum_type, "syntax", strlen("syntax"), 0, ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -1952,9 +1952,9 @@ zend_class_entry* enum_value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\EnumValue", EnumValue, enum_value) - zend_declare_property_null(enum_value_type, "name", strlen("name"), - ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(enum_value_type, "number", strlen("number"), + zend_declare_property_string(enum_value_type, "name", strlen("name"), "", + ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_long(enum_value_type, "number", strlen("number"), 0, ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(enum_value_type, "options", strlen("options"), ZEND_ACC_PRIVATE TSRMLS_CC); @@ -2032,26 +2032,27 @@ zend_class_entry* field_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Field", Field, field) - zend_declare_property_null(field_type, "kind", strlen("kind"), + zend_declare_property_long(field_type, "kind", strlen("kind"), 0, ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(field_type, "cardinality", strlen("cardinality"), + zend_declare_property_long(field_type, "cardinality", strlen("cardinality"), + 0, ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_long(field_type, "number", strlen("number"), 0, ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(field_type, "number", strlen("number"), - ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(field_type, "name", strlen("name"), - ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(field_type, "type_url", strlen("type_url"), - ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(field_type, "oneof_index", strlen("oneof_index"), - ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(field_type, "packed", strlen("packed"), + zend_declare_property_string(field_type, "name", strlen("name"), "", + ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_string(field_type, "type_url", strlen("type_url"), "", + ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_long(field_type, "oneof_index", strlen("oneof_index"), + 0, ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_bool(field_type, "packed", strlen("packed"), false, ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(field_type, "options", strlen("options"), ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(field_type, "json_name", strlen("json_name"), - ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(field_type, "default_value", strlen("default_value"), - ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_string(field_type, "json_name", strlen("json_name"), + "", ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_string(field_type, "default_value", + strlen("default_value"), "", + ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(Field, __construct) { @@ -2087,8 +2088,8 @@ zend_class_entry* float_value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\FloatValue", FloatValue, float_value) - zend_declare_property_null(float_value_type, "value", strlen("value"), - ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_double(float_value_type, "value", strlen("value"), 0, + ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(FloatValue, __construct) { @@ -2138,7 +2139,7 @@ zend_class_entry* int32_value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Int32Value", Int32Value, int32_value) - zend_declare_property_null(int32_value_type, "value", strlen("value"), + zend_declare_property_long(int32_value_type, "value", strlen("value"), 0, ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -2166,7 +2167,7 @@ zend_class_entry* int64_value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Int64Value", Int64Value, int64_value) - zend_declare_property_null(int64_value_type, "value", strlen("value"), + zend_declare_property_long(int64_value_type, "value", strlen("value"), 0, ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -2234,19 +2235,23 @@ zend_class_entry* method_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Method", Method, method) - zend_declare_property_null(method_type, "name", strlen("name"), - ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(method_type, "request_type_url", strlen("request_type_url"), - ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(method_type, "request_streaming", strlen("request_streaming"), + zend_declare_property_string(method_type, "name", strlen("name"), "", + ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_string(method_type, "request_type_url", + strlen("request_type_url"), "", + ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_bool(method_type, "request_streaming", + strlen("request_streaming"), 0, ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(method_type, "response_type_url", strlen("response_type_url"), - ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(method_type, "response_streaming", strlen("response_streaming"), + zend_declare_property_string(method_type, "response_type_url", + strlen("response_type_url"), "", + ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_bool(method_type, "response_streaming", + strlen("response_streaming"), 0, ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(method_type, "options", strlen("options"), ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(method_type, "syntax", strlen("syntax"), + zend_declare_property_long(method_type, "syntax", strlen("syntax"), 0, ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -2282,10 +2287,10 @@ zend_class_entry* mixin_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Mixin", Mixin, mixin) - zend_declare_property_null(mixin_type, "name", strlen("name"), - ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(mixin_type, "root", strlen("root"), - ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_string(mixin_type, "name", strlen("name"), "", + ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_string(mixin_type, "root", strlen("root"), "", + ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(Mixin, __construct) { @@ -2315,8 +2320,8 @@ zend_class_entry* option_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Option", Option, option) - zend_declare_property_null(option_type, "name", strlen("name"), - ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_string(option_type, "name", strlen("name"), "", + ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(option_type, "value", strlen("value"), ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -2346,8 +2351,9 @@ zend_class_entry* source_context_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\SourceContext", SourceContext, source_context) - zend_declare_property_null(source_context_type, "file_name", strlen("file_name"), - ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_string(source_context_type, "file_name", + strlen("file_name"), "", + ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(SourceContext, __construct) { @@ -2374,8 +2380,8 @@ zend_class_entry* string_value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\StringValue", StringValue, string_value) - zend_declare_property_null(string_value_type, "value", strlen("value"), - ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_string(string_value_type, "value", strlen("value"), "", + ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END PHP_METHOD(StringValue, __construct) { @@ -2440,17 +2446,17 @@ zend_class_entry* type_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Type", Type, type) - zend_declare_property_null(type_type, "name", strlen("name"), - ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_string(type_type, "name", strlen("name"), "", + ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(type_type, "fields", strlen("fields"), ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(type_type, "oneofs", strlen("oneofs"), - ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_string(type_type, "oneofs", strlen("oneofs"), "", + ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(type_type, "options", strlen("options"), ZEND_ACC_PRIVATE TSRMLS_CC); zend_declare_property_null(type_type, "source_context", strlen("source_context"), ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(type_type, "syntax", strlen("syntax"), + zend_declare_property_long(type_type, "syntax", strlen("syntax"), 0, ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -2483,7 +2489,7 @@ zend_class_entry* u_int32_value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\UInt32Value", UInt32Value, u_int32_value) - zend_declare_property_null(u_int32_value_type, "value", strlen("value"), + zend_declare_property_long(u_int32_value_type, "value", strlen("value"), 0, ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END @@ -2511,7 +2517,7 @@ zend_class_entry* u_int64_value_type; // Init class entry. PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\UInt64Value", UInt64Value, u_int64_value) - zend_declare_property_null(u_int64_value_type, "value", strlen("value"), + zend_declare_property_long(u_int64_value_type, "value", strlen("value"), 0, ZEND_ACC_PRIVATE TSRMLS_CC); PHP_PROTO_INIT_SUBMSGCLASS_END diff --git a/php/tests/encode_decode_test.php b/php/tests/encode_decode_test.php index f2a50180c65e..26b765652d44 100644 --- a/php/tests/encode_decode_test.php +++ b/php/tests/encode_decode_test.php @@ -5,6 +5,7 @@ use Google\Protobuf\RepeatedField; use Google\Protobuf\GPBType; +use Foo\TestStringValue; use Foo\TestAny; use Foo\TestEnum; use Foo\TestMessage; @@ -167,6 +168,14 @@ public function testEncodeTopLevelStringValue() $this->assertSame("\"a\"", $m->serializeToJsonString()); } + public function testEncodeStringValue() + { + $m = new TestStringValue(['field' => new StringValue(['value' => ''])]); + var_dump($m->getField()); + var_dump($m->serializeToJsonString()); + $this->assertSame("{\"field\":\"\"}", $m->serializeToJsonString()); + } + public function testDecodeTopLevelBytesValue() { $m = new BytesValue(); diff --git a/php/tests/memory_leak_test.php b/php/tests/memory_leak_test.php index 142f46766926..2873f34432cf 100644 --- a/php/tests/memory_leak_test.php +++ b/php/tests/memory_leak_test.php @@ -31,6 +31,7 @@ require_once('generated/Foo/TestPhpDoc.php'); require_once('generated/Foo/TestRandomFieldOrder.php'); require_once('generated/Foo/TestReverseFieldOrder.php'); +require_once('generated/Foo/TestStringValue.php'); require_once('generated/Foo/TestUnpackedMessage.php'); require_once('generated/Foo/testLowerCaseMessage.php'); require_once('generated/Foo/testLowerCaseEnum.php'); diff --git a/php/tests/proto/test.proto b/php/tests/proto/test.proto index 715f08b83230..ea00fec88d28 100644 --- a/php/tests/proto/test.proto +++ b/php/tests/proto/test.proto @@ -1,6 +1,7 @@ syntax = "proto3"; import 'google/protobuf/any.proto'; +import 'google/protobuf/wrappers.proto'; import 'google/protobuf/struct.proto'; import 'proto/test_include.proto'; import 'proto/test_no_namespace.proto'; @@ -207,3 +208,7 @@ enum testLowerCaseEnum { message TestAny { google.protobuf.Any any = 1; } + +message TestStringValue { + google.protobuf.StringValue field = 1; +}