From 864bd030cf60e0ae037fc4c734f86542532e479d Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Mon, 8 Jul 2019 14:20:46 -0700 Subject: [PATCH] Set oneof case in array constructor (#6351) Forgot to set it previously. --- php/ext/google/protobuf/message.c | 3 +++ php/ext/google/protobuf/protobuf.h | 2 ++ php/ext/google/protobuf/storage.c | 4 ++-- php/tests/generated_class_test.php | 2 ++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/php/ext/google/protobuf/message.c b/php/ext/google/protobuf/message.c index 43561551bde1..f147325bb0db 100644 --- a/php/ext/google/protobuf/message.c +++ b/php/ext/google/protobuf/message.c @@ -469,11 +469,14 @@ void Message_construct(zval* msg, zval* array_wrapper) { if (upb_fielddef_containingoneof(field)) { void* memory = slot_memory(intern->descriptor->layout, message_data(intern), field); + uint32_t* oneof_case = slot_oneof_case(intern->descriptor->layout, + message_data(intern), field); int property_cache_index = intern->descriptor->layout->fields[upb_fielddef_index(field)] .cache_index; cached = OBJ_PROP(Z_OBJ_P(msg), property_cache_index); *(CACHED_VALUE**)(memory) = cached; + *oneof_case = upb_fielddef_number(field); } else { zend_property_info* property_info; PHP_PROTO_FAKE_SCOPE_BEGIN(Z_OBJCE_P(msg)); diff --git a/php/ext/google/protobuf/protobuf.h b/php/ext/google/protobuf/protobuf.h index b2a9b94c828f..4e922ba9cfb5 100644 --- a/php/ext/google/protobuf/protobuf.h +++ b/php/ext/google/protobuf/protobuf.h @@ -948,6 +948,8 @@ void layout_merge(MessageLayout* layout, MessageHeader* from, const char* layout_get_oneof_case(MessageLayout* layout, const void* storage, const upb_oneofdef* oneof TSRMLS_DC); void free_layout(MessageLayout* layout); +uint32_t* slot_oneof_case(MessageLayout* layout, const void* storage, + const upb_fielddef* field); void* slot_memory(MessageLayout* layout, const void* storage, const upb_fielddef* field); diff --git a/php/ext/google/protobuf/storage.c b/php/ext/google/protobuf/storage.c index 8f717302dc31..1c28b1c18558 100644 --- a/php/ext/google/protobuf/storage.c +++ b/php/ext/google/protobuf/storage.c @@ -572,8 +572,8 @@ static size_t align_up_to(size_t offset, size_t granularity) { return (offset + granularity - 1) & ~(granularity - 1); } -static uint32_t* slot_oneof_case(MessageLayout* layout, const void* storage, - const upb_fielddef* field) { +uint32_t* slot_oneof_case(MessageLayout* layout, const void* storage, + const upb_fielddef* field) { return (uint32_t*)(((uint8_t*)storage) + layout->fields[upb_fielddef_index(field)].case_offset); } diff --git a/php/tests/generated_class_test.php b/php/tests/generated_class_test.php index a9ad793be315..d808e3f35277 100644 --- a/php/tests/generated_class_test.php +++ b/php/tests/generated_class_test.php @@ -1452,6 +1452,8 @@ public function testOneofMessageInArrayConstructor() $m = new TestMessage([ 'oneof_message' => new Sub(), ]); + $this->assertSame('oneof_message', $m->getMyOneof()); + $this->assertNotNull($m->getOneofMessage()); } public function testOneofStringInArrayConstructor()