Skip to content

Commit 4f48ade

Browse files
authored
Fix for readonly properties that declared in parent (#96)
* Fix for readonly properties that declared in parent * More tests for readonly properties that declared in parent
1 parent d49075c commit 4f48ade

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/Serializers/Native.php

+4
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,10 @@ protected function mapByReference(&$data)
504504
continue;
505505
}
506506

507+
if (PHP_VERSION >= 8.1 && $property->isReadOnly() && $property->class !== $reflection->name) {
508+
continue;
509+
}
510+
507511
$value = $property->getValue($instance);
508512

509513
if (is_array($value) || is_object($value)) {

tests/SerializerPhp81Test.php

+31
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,28 @@ enum SerializerScopedBackedEnum: string {
183183
);
184184
})->with('serializers');
185185

186+
test('readonly properties declared in parent', function () {
187+
$childWithDefaultValue = new SerializerPhp81Child();
188+
189+
$f = static function () use ($childWithDefaultValue) {
190+
return $childWithDefaultValue;
191+
};
192+
193+
$f = s($f);
194+
195+
expect($f()->property)->toBe(1);
196+
197+
$child = new SerializerPhp81Child(100);
198+
199+
$f = static function () use ($child) {
200+
return $child;
201+
};
202+
203+
$f = s($f);
204+
205+
expect($f()->property)->toBe(100);
206+
})->with('serializers');
207+
186208
test('first-class callable with closures', function () {
187209
$f = function ($value) {
188210
return $value;
@@ -584,6 +606,15 @@ enum SerializerScopedBackedEnum: string {
584606
interface SerializerPhp81HasId {}
585607
interface SerializerPhp81HasName {}
586608

609+
class SerializerPhp81Child extends SerializerPhp81Parent {}
610+
611+
class SerializerPhp81Parent
612+
{
613+
public function __construct(
614+
public readonly int $property = 1,
615+
) {}
616+
}
617+
587618
class SerializerPhp81Service implements SerializerPhp81HasId, SerializerPhp81HasName
588619
{
589620
final public const X = 'foo';

0 commit comments

Comments
 (0)