Skip to content

[JsonStreamer] Fix reading/writing objects with generics #60281

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

Merged
merged 1 commit into from
Apr 29, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ public function load(string $className, array $options = [], array $context = []

foreach ($result as &$metadata) {
$type = $metadata->getType();

if (isset($variableTypes[(string) $type])) {
$metadata = $metadata->withType($this->replaceVariableTypes($type, $variableTypes));
}
$metadata = $metadata->withType($this->replaceVariableTypes($type, $variableTypes));
}

return $result;
Expand Down Expand Up @@ -122,19 +119,19 @@ private function replaceVariableTypes(Type $type, array $variableTypes): Type
}

if ($type instanceof UnionType) {
return new UnionType(...array_map(fn (Type $t): Type => $this->replaceVariableTypes($t, $variableTypes), $type->getTypes()));
return Type::union(...array_map(fn (Type $t): Type => $this->replaceVariableTypes($t, $variableTypes), $type->getTypes()));
}

if ($type instanceof IntersectionType) {
return new IntersectionType(...array_map(fn (Type $t): Type => $this->replaceVariableTypes($t, $variableTypes), $type->getTypes()));
return Type::intersection(...array_map(fn (Type $t): Type => $this->replaceVariableTypes($t, $variableTypes), $type->getTypes()));
}

if ($type instanceof CollectionType) {
return new CollectionType($this->replaceVariableTypes($type->getWrappedType(), $variableTypes), $type->isList());
}

if ($type instanceof GenericType) {
return new GenericType(
return Type::generic(
$this->replaceVariableTypes($type->getWrappedType(), $variableTypes),
...array_map(fn (Type $t): Type => $this->replaceVariableTypes($t, $variableTypes), $type->getVariableTypes()),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Symfony\Component\TypeInfo\Type\BuiltinType;
use Symfony\Component\TypeInfo\Type\CollectionType;
use Symfony\Component\TypeInfo\Type\EnumType;
use Symfony\Component\TypeInfo\Type\GenericType;
use Symfony\Component\TypeInfo\Type\ObjectType;
use Symfony\Component\TypeInfo\Type\UnionType;

Expand Down Expand Up @@ -118,6 +119,10 @@ public function createDataModel(Type $type, array $options = [], array $context
return new BackedEnumNode($type);
}

if ($type instanceof GenericType) {
$type = $type->getWrappedType();
}

if ($type instanceof ObjectType && !$type instanceof EnumType) {
$typeString = (string) $type;
$className = $type->getClassName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class DummyWithGenerics
{
/**
* @var array<int, T>
* @var list<T>
*/
public array $dummies = [];
}
12 changes: 12 additions & 0 deletions src/Symfony/Component/JsonStreamer/Tests/JsonStreamReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\JsonStreamer\Tests\Fixtures\Enum\DummyBackedEnum;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\ClassicDummy;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithDateTimes;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithGenerics;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNameAttributes;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNullableProperties;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithPhpDoc;
Expand Down Expand Up @@ -100,6 +101,17 @@ public function testReadObject()
}, '{"id": 10, "name": "dummy name"}', Type::object(ClassicDummy::class));
}

public function testReadObjectWithGenerics()
{
$reader = JsonStreamReader::create(streamReadersDir: $this->streamReadersDir, lazyGhostsDir: $this->lazyGhostsDir);

$this->assertRead($reader, function (mixed $read) {
$this->assertInstanceOf(DummyWithGenerics::class, $read);
$this->assertSame(10, $read->dummies[0]->id);
$this->assertSame('dummy name', $read->dummies[0]->name);
}, '{"dummies":[{"id":10,"name":"dummy name"}]}', Type::generic(Type::object(DummyWithGenerics::class), Type::object(ClassicDummy::class)));
}

public function testReadObjectWithStreamedName()
{
$reader = JsonStreamReader::create(streamReadersDir: $this->streamReadersDir, lazyGhostsDir: $this->lazyGhostsDir);
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/JsonStreamer/Tests/JsonStreamWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\JsonStreamer\Tests\Fixtures\Enum\DummyBackedEnum;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\ClassicDummy;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithDateTimes;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithGenerics;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNameAttributes;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNullableProperties;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithPhpDoc;
Expand Down Expand Up @@ -117,6 +118,18 @@ public function testWriteObject()
$this->assertWritten('{"id":10,"name":"dummy name"}', $dummy, Type::object(ClassicDummy::class));
}

public function testWriteObjectWithGenerics()
{
$nestedDummy = new DummyWithNameAttributes();
$nestedDummy->id = 10;
$nestedDummy->name = 'dummy name';

$dummy = new DummyWithGenerics();
$dummy->dummies = [$nestedDummy];

$this->assertWritten('{"dummies":[{"id":10,"name":"dummy name"}]}', $dummy, Type::generic(Type::object(DummyWithGenerics::class), Type::object(ClassicDummy::class)));
}

public function testWriteObjectWithStreamedName()
{
$dummy = new DummyWithNameAttributes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Symfony\Component\TypeInfo\Type\BuiltinType;
use Symfony\Component\TypeInfo\Type\CollectionType;
use Symfony\Component\TypeInfo\Type\EnumType;
use Symfony\Component\TypeInfo\Type\GenericType;
use Symfony\Component\TypeInfo\Type\ObjectType;
use Symfony\Component\TypeInfo\Type\UnionType;

Expand Down Expand Up @@ -124,6 +125,10 @@ private function createDataModel(Type $type, DataAccessorInterface $accessor, ar
return new BackedEnumNode($accessor, $type);
}

if ($type instanceof GenericType) {
$type = $type->getWrappedType();
}

if ($type instanceof ObjectType && !$type instanceof EnumType) {
$typeString = (string) $type;
$className = $type->getClassName();
Expand All @@ -133,7 +138,7 @@ private function createDataModel(Type $type, DataAccessorInterface $accessor, ar
}

$context['generated_classes'][$typeString] = true;
$propertiesMetadata = $this->propertyMetadataLoader->load($className, $options, ['original_type' => $type] + $context);
$propertiesMetadata = $this->propertyMetadataLoader->load($className, $options, $context);

try {
$classReflection = new \ReflectionClass($className);
Expand Down
Loading