Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle optional fields correctly in Message_get method #18982

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion php/ext/google/protobuf/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,14 @@ static zval* Message_read_property(zend_object* obj, zend_string* member,
const upb_FieldDef* f = get_field(intern, member);

if (!f) return &EG(uninitialized_zval);
Message_get(intern, f, rv);

if (upb_FieldDef_IsOptional(f) && upb_FieldDef_HasPresence(f)
&& Message_has_property(obj, member, 0, cache_slot) == false
) {
ZVAL_NULL(rv);
} else {
Message_get(intern, f, rv);
}
return rv;
}

Expand Down
40 changes: 40 additions & 0 deletions php/tests/WellKnownTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,44 @@ public function enumNameValueConversionDataProvider()
['\Google\Protobuf\Syntax'],
];
}

/**
* @dataProvider optionalFieldsDataProvider
*/
public function testReflectionProperty($property, $default)
{
$testMessage = new TestMessage();

$functionName = implode('', array_map('ucwords', explode('_', $property)));

$reflectionProperty = new \ReflectionProperty($testMessage, $property);

self::assertFalse(call_user_func([$testMessage, 'has'.$functionName]));
self::assertEquals($default, call_user_func([$testMessage, 'get'.$functionName]));
self::assertNull($reflectionProperty->getValue($testMessage));
}

public function optionalFieldsDataProvider()
{
return [
['true_optional_int32', 0],
['true_optional_int64', 0],
['true_optional_uint32', 0],
['true_optional_uint64', 0],
['true_optional_sint32', 0],
['true_optional_sint64', 0],
['true_optional_fixed32', 0],
['true_optional_fixed64', 0],
['true_optional_sfixed32', 0],
['true_optional_sfixed64', 0],
['true_optional_float', 0.0],
['true_optional_double', 0.0],
['true_optional_bool', false],
['true_optional_string', ''],
['true_optional_bytes', ''],
['true_optional_enum', null],
['true_optional_message', null],
['true_optional_included_message', null],
];
}
}
Loading