Skip to content

Commit

Permalink
Fix for readonly properties that declared in parent (#96)
Browse files Browse the repository at this point in the history
* Fix for readonly properties that declared in parent

* More tests for readonly properties that declared in parent
  • Loading branch information
MrMeshok authored Nov 14, 2024
1 parent d49075c commit 4f48ade
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Serializers/Native.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,10 @@ protected function mapByReference(&$data)
continue;
}

if (PHP_VERSION >= 8.1 && $property->isReadOnly() && $property->class !== $reflection->name) {
continue;
}

$value = $property->getValue($instance);

if (is_array($value) || is_object($value)) {
Expand Down
31 changes: 31 additions & 0 deletions tests/SerializerPhp81Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,28 @@ enum SerializerScopedBackedEnum: string {
);
})->with('serializers');

test('readonly properties declared in parent', function () {
$childWithDefaultValue = new SerializerPhp81Child();

$f = static function () use ($childWithDefaultValue) {
return $childWithDefaultValue;
};

$f = s($f);

expect($f()->property)->toBe(1);

$child = new SerializerPhp81Child(100);

$f = static function () use ($child) {
return $child;
};

$f = s($f);

expect($f()->property)->toBe(100);
})->with('serializers');

test('first-class callable with closures', function () {
$f = function ($value) {
return $value;
Expand Down Expand Up @@ -584,6 +606,15 @@ enum SerializerScopedBackedEnum: string {
interface SerializerPhp81HasId {}
interface SerializerPhp81HasName {}

class SerializerPhp81Child extends SerializerPhp81Parent {}

class SerializerPhp81Parent
{
public function __construct(
public readonly int $property = 1,
) {}
}

class SerializerPhp81Service implements SerializerPhp81HasId, SerializerPhp81HasName
{
final public const X = 'foo';
Expand Down

0 comments on commit 4f48ade

Please sign in to comment.