Skip to content

Commit

Permalink
Initialize wrapper message during parsing (#6974)
Browse files Browse the repository at this point in the history
* Initialize wrapper message during parsing
In case the internal value is default

* Fix zts build
  • Loading branch information
TeBoring authored Dec 3, 2019
1 parent 7bb8b10 commit 2929bb3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions php/ext/google/protobuf/encode_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,12 @@ static void* wrapper_submsg_handler(void* closure, const void* hd) {
frame->submsg = submsg;
frame->is_msg = true;
} else {
if (Z_TYPE_P(CACHED_PTR_TO_ZVAL_PTR(cached)) == IS_NULL) {
// Needs to initiate the wrapper message
const upb_msgdef* msgdef = subdesc->msgdef;
const upb_fielddef* f = upb_msgdef_itof(msgdef, 1);
native_slot_get_default(upb_fielddef_type(f), cached TSRMLS_CC);
}
// In this case, wrapper message hasn't been created and value will be
// stored in cache directly.
frame->submsg = cached;
Expand All @@ -1024,6 +1030,12 @@ static void* wrapper_oneofsubmsg_handler(void* closure, const void* hd) {

if (oldcase != oneofdata->oneof_case_num) {
oneof_cleanup(msg, oneofdata);
if (Z_TYPE_P(CACHED_PTR_TO_ZVAL_PTR(cached)) == IS_NULL) {
// Needs to initiate the wrapper message
const upb_msgdef* msgdef = subdesc->msgdef;
const upb_fielddef* f = upb_msgdef_itof(msgdef, 1);
native_slot_get_default(upb_fielddef_type(f), cached TSRMLS_CC);
}
frame->submsg = cached;
frame->is_msg = false;
} else if (Z_TYPE_P(CACHED_PTR_TO_ZVAL_PTR(cached)) == IS_OBJECT) {
Expand Down
12 changes: 12 additions & 0 deletions php/tests/encode_decode_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,18 @@ public function testEncodeTopLevelAny()
$m->serializeToJsonString());
}

public function testEncodeAnyWithDefaultWrapperMessagePacked()
{
$any = new Any();
$any->pack(new TestInt32Value([
'field' => new Int32Value(['value' => 0]),
]));
$this->assertSame(
"{\"@type\":\"type.googleapis.com/foo.TestInt32Value\"," .
"\"field\":0}",
$any->serializeToJsonString());
}

public function testDecodeTopLevelFieldMask()
{
$m = new TestMessage();
Expand Down

0 comments on commit 2929bb3

Please sign in to comment.