Skip to content
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 @@ -26,7 +26,7 @@ public function testPhpDocPriority()
$this->markTestSkipped();
}

$this->assertEquals(Type::list(Type::int()), $propertyInfo->getType(Dummy::class, 'codes'));
$this->assertEquals(Type::array(Type::int()), $propertyInfo->getType(Dummy::class, 'codes'));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Symfony\Component\JsonStreamer\Tests\Fixtures\Model;

class DummyWithList
{
/** @var list<ClassicDummy> */
public array $dummies;

public string $customProperty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Symfony\Component\JsonStreamer\Tests\Fixtures\Model;

class DummyWithNestedList
{
/** @var list<DummyWithList> */
public array $dummies;

public string $stringProperty;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 32 additions & 2 deletions src/Symfony/Component/JsonStreamer/Tests/JsonStreamWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithArray;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithDateTimes;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithGenerics;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithList;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNameAttributes;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNestedArray;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNestedList;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNullableProperties;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithPhpDoc;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithUnionProperties;
Expand Down Expand Up @@ -119,7 +121,7 @@ public function testWriteCollection()
$dummyWithArray2->customProperty = 'customProperty2';

$this->assertWritten(
'[{"dummies":[{"id":1,"name":"dummy"}],"customProperty":"customProperty1"},{"dummies":[{"id":1,"name":"dummy"}],"customProperty":"customProperty2"}]',
'[{"dummies":{"0":{"id":1,"name":"dummy"}},"customProperty":"customProperty1"},{"dummies":{"0":{"id":1,"name":"dummy"}},"customProperty":"customProperty2"}]',
[$dummyWithArray1, $dummyWithArray2],
Type::list(Type::object(DummyWithArray::class)),
);
Expand All @@ -133,10 +135,38 @@ public function testWriteCollection()
$dummyWithNestedArray2->stringProperty = 'stringProperty2';

$this->assertWritten(
'[{"dummies":[{"dummies":[{"id":1,"name":"dummy"}],"customProperty":"customProperty1"}],"stringProperty":"stringProperty1"},{"dummies":[{"dummies":[{"id":1,"name":"dummy"}],"customProperty":"customProperty2"}],"stringProperty":"stringProperty2"}]',
'[{"dummies":{"0":{"dummies":{"0":{"id":1,"name":"dummy"}},"customProperty":"customProperty1"}},"stringProperty":"stringProperty1"},{"dummies":{"0":{"dummies":{"0":{"id":1,"name":"dummy"}},"customProperty":"customProperty2"}},"stringProperty":"stringProperty2"}]',
[$dummyWithNestedArray1, $dummyWithNestedArray2],
Type::list(Type::object(DummyWithNestedArray::class)),
);

$dummyWithList1 = new DummyWithList();
$dummyWithList1->dummies = [new ClassicDummy()];
$dummyWithList1->customProperty = 'customProperty1';

$dummyWithList2 = new DummyWithList();
$dummyWithList2->dummies = [new ClassicDummy()];
$dummyWithList2->customProperty = 'customProperty2';

$this->assertWritten(
'[{"dummies":[{"id":1,"name":"dummy"}],"customProperty":"customProperty1"},{"dummies":[{"id":1,"name":"dummy"}],"customProperty":"customProperty2"}]',
[$dummyWithList1, $dummyWithList2],
Type::list(Type::object(DummyWithList::class)),
);

$dummyWithNestedList1 = new DummyWithNestedList();
$dummyWithNestedList1->dummies = [$dummyWithList1];
$dummyWithNestedList1->stringProperty = 'stringProperty1';

$dummyWithNestedList2 = new DummyWithNestedList();
$dummyWithNestedList2->dummies = [$dummyWithList2];
$dummyWithNestedList2->stringProperty = 'stringProperty2';

$this->assertWritten(
'[{"dummies":[{"dummies":[{"id":1,"name":"dummy"}],"customProperty":"customProperty1"}],"stringProperty":"stringProperty1"},{"dummies":[{"dummies":[{"id":1,"name":"dummy"}],"customProperty":"customProperty2"}],"stringProperty":"stringProperty2"}]',
[$dummyWithNestedList1, $dummyWithNestedList2],
Type::list(Type::object(DummyWithNestedList::class)),
);
}

public function testWriteObject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
use Symfony\Component\JsonStreamer\Tests\Fixtures\Enum\DummyEnum;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\ClassicDummy;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithArray;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithList;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNameAttributes;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNestedArray;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNestedList;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithOtherDummies;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithUnionProperties;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithValueTransformerAttributes;
Expand Down Expand Up @@ -95,8 +97,11 @@ public static function generatedStreamWriterDataProvider(): iterable
yield ['null_list', Type::list(Type::null())];
yield ['object_list', Type::list(Type::object(DummyWithNameAttributes::class))];
yield ['nullable_object_list', Type::nullable(Type::list(Type::object(DummyWithNameAttributes::class)))];
yield ['nested_list', Type::list(Type::object(DummyWithArray::class))];
yield ['double_nested_list', Type::list(Type::object(DummyWithNestedArray::class))];
yield ['nested_list', Type::list(Type::object(DummyWithList::class))];
yield ['double_nested_list', Type::list(Type::object(DummyWithNestedList::class))];

yield ['nested_array', Type::list(Type::object(DummyWithArray::class))];
yield ['double_nested_array', Type::list(Type::object(DummyWithNestedArray::class))];

yield ['dict', Type::dict()];
yield ['object_dict', Type::dict(Type::object(DummyWithNameAttributes::class))];
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/JsonStreamer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"psr/container": "^1.1|^2.0",
"psr/log": "^1|^2|^3",
"symfony/filesystem": "^7.2",
"symfony/type-info": "^7.3.3",
"symfony/type-info": "~7.3.8|^7.4.1",
"symfony/var-exporter": "^7.2"
},
"require-dev": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,12 +691,12 @@ public static function typesProvider(): iterable
yield ['foo2', Type::float()];
yield ['foo3', Type::callable()];
yield ['foo5', Type::mixed()];
yield ['files', Type::union(Type::list(Type::object(\SplFileInfo::class)), Type::resource()), null, null];
yield ['files', Type::union(Type::array(Type::object(\SplFileInfo::class)), Type::resource()), null, null];
yield ['bal', Type::object(\DateTimeImmutable::class)];
yield ['parent', Type::object(ParentDummy::class)];
yield ['collection', Type::list(Type::object(\DateTimeImmutable::class))];
yield ['nestedCollection', Type::list(Type::list(Type::string()))];
yield ['mixedCollection', Type::list()];
yield ['collection', Type::array(Type::object(\DateTimeImmutable::class))];
yield ['nestedCollection', Type::array(Type::array(Type::string()))];
yield ['mixedCollection', Type::array(Type::mixed())];
yield ['a', Type::int()];
yield ['b', Type::nullable(Type::object(ParentDummy::class))];
yield ['c', Type::nullable(Type::bool())];
Expand All @@ -707,7 +707,7 @@ public static function typesProvider(): iterable
yield ['h', Type::nullable(Type::string())];
yield ['i', Type::union(Type::int(), Type::string(), Type::null())];
yield ['j', Type::nullable(Type::object(\DateTimeImmutable::class))];
yield ['nullableCollectionOfNonNullableElements', Type::nullable(Type::list(Type::int()))];
yield ['nullableCollectionOfNonNullableElements', Type::nullable(Type::array(Type::int()))];
yield ['donotexist', null];
yield ['staticGetter', null];
yield ['staticSetter', null];
Expand All @@ -716,7 +716,7 @@ public static function typesProvider(): iterable
yield ['arrayOfMixed', Type::dict(Type::mixed())];
yield ['listOfStrings', Type::list(Type::string())];
yield ['self', Type::object(Dummy::class)];
yield ['rootDummyItems', Type::list(Type::object(RootDummyItem::class))];
yield ['rootDummyItems', Type::array(Type::object(RootDummyItem::class))];
yield ['rootDummyItem', Type::object(RootDummyItem::class)];
yield ['collectionAsObject', Type::collection(Type::object(DummyCollection::class), Type::string(), Type::int())];
}
Expand Down Expand Up @@ -767,12 +767,12 @@ public static function typesWithNoPrefixesProvider(): iterable
yield ['foo2', Type::float()];
yield ['foo3', Type::callable()];
yield ['foo5', Type::mixed()];
yield ['files', Type::union(Type::list(Type::object(\SplFileInfo::class)), Type::resource())];
yield ['files', Type::union(Type::array(Type::object(\SplFileInfo::class)), Type::resource())];
yield ['bal', Type::object(\DateTimeImmutable::class)];
yield ['parent', Type::object(ParentDummy::class)];
yield ['collection', Type::list(Type::object(\DateTimeImmutable::class))];
yield ['nestedCollection', Type::list(Type::list(Type::string()))];
yield ['mixedCollection', Type::list()];
yield ['collection', Type::array(Type::object(\DateTimeImmutable::class))];
yield ['nestedCollection', Type::array(Type::array(Type::string()))];
yield ['mixedCollection', Type::array(Type::mixed())];
yield ['a', null];
yield ['b', null];
yield ['c', null];
Expand All @@ -783,7 +783,7 @@ public static function typesWithNoPrefixesProvider(): iterable
yield ['h', Type::nullable(Type::string())];
yield ['i', Type::union(Type::int(), Type::string(), Type::null())];
yield ['j', Type::nullable(Type::object(\DateTimeImmutable::class))];
yield ['nullableCollectionOfNonNullableElements', Type::nullable(Type::list(Type::int()))];
yield ['nullableCollectionOfNonNullableElements', Type::nullable(Type::array(Type::int()))];
yield ['donotexist', null];
yield ['staticGetter', null];
yield ['staticSetter', null];
Expand Down Expand Up @@ -830,12 +830,12 @@ public static function typesWithCustomPrefixesProvider(): iterable
yield ['foo2', Type::float()];
yield ['foo3', Type::callable()];
yield ['foo5', Type::mixed()];
yield ['files', Type::union(Type::list(Type::object(\SplFileInfo::class)), Type::resource())];
yield ['files', Type::union(Type::array(Type::object(\SplFileInfo::class)), Type::resource())];
yield ['bal', Type::object(\DateTimeImmutable::class)];
yield ['parent', Type::object(ParentDummy::class)];
yield ['collection', Type::list(Type::object(\DateTimeImmutable::class))];
yield ['nestedCollection', Type::list(Type::list(Type::string()))];
yield ['mixedCollection', Type::list()];
yield ['collection', Type::array(Type::object(\DateTimeImmutable::class))];
yield ['nestedCollection', Type::array(Type::array(Type::string()))];
yield ['mixedCollection', Type::array(Type::mixed())];
yield ['a', null];
yield ['b', null];
yield ['c', Type::nullable(Type::bool())];
Expand All @@ -846,7 +846,7 @@ public static function typesWithCustomPrefixesProvider(): iterable
yield ['h', Type::nullable(Type::string())];
yield ['i', Type::union(Type::int(), Type::string(), Type::null())];
yield ['j', Type::nullable(Type::object(\DateTimeImmutable::class))];
yield ['nullableCollectionOfNonNullableElements', Type::nullable(Type::list(Type::int()))];
yield ['nullableCollectionOfNonNullableElements', Type::nullable(Type::array(Type::int()))];
yield ['nonNullableCollectionOfNullableElements', Type::array(Type::nullable(Type::int()))];
yield ['nullableCollectionOfMultipleNonNullableElementTypes', Type::nullable(Type::array(Type::union(Type::int(), Type::string())))];
yield ['donotexist', null];
Expand Down Expand Up @@ -976,16 +976,16 @@ public function testExtractorUnionTypes(string $property, ?Type $type)
public static function unionTypesProvider(): iterable
{
yield ['a', Type::union(Type::string(), Type::int())];
yield ['b', Type::list(Type::union(Type::string(), Type::int()))];
yield ['b', Type::array(Type::union(Type::string(), Type::int()))];
yield ['c', Type::array(Type::union(Type::string(), Type::int()))];
yield ['d', Type::array(Type::array(Type::string()), Type::union(Type::string(), Type::int()))];
yield ['e', Type::union(
Type::generic(
Type::object(Dummy::class),
Type::array(Type::string(), Type::mixed()),
Type::array(Type::string()),
Type::union(
Type::int(),
Type::list(Type::collection(Type::object(\Traversable::class), Type::object(DefaultValue::class))),
Type::array(Type::collection(Type::object(\Traversable::class), Type::object(DefaultValue::class))),
),
),
Type::object(ParentDummy::class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class DummyUnionType
public $d;

/**
* @var (Dummy<array<mixed, string>, (int | (\Traversable<DefaultValue>)[])> | ParentDummy | null)
* @var (Dummy<array<string>, (int | (\Traversable<DefaultValue>)[])> | ParentDummy | null)
*/
public $e;

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/PropertyInfo/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"php": ">=8.2",
"symfony/deprecation-contracts": "^2.5|^3",
"symfony/string": "^6.4|^7.0",
"symfony/type-info": "^7.3.5"
"symfony/type-info": "~7.3.8|^7.4.1"
},
"require-dev": {
"symfony/serializer": "^6.4|^7.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,16 @@ public static function resolveDataProvider(): iterable
yield [Type::callable(), 'callable(string, int): mixed'];

// array
yield [Type::list(Type::bool()), 'bool[]'];
yield [Type::array(Type::bool()), 'bool[]'];
yield [Type::array(Type::bool()), 'array<bool>'];
yield [Type::array(Type::bool(), Type::int()), 'array<int, bool>'];
yield [Type::array(Type::bool(), Type::arrayKey()), 'array<array-key, bool>'];
yield [Type::array(Type::bool(), Type::arrayKey()), 'array<int|string, bool>'];
yield [Type::array(Type::bool(), Type::arrayKey()), 'non-empty-array<int|string, bool>'];

// list
yield [Type::list(Type::bool()), 'list<bool>'];
yield [Type::list(Type::bool()), 'non-empty-list<bool>'];

// array shape
yield [Type::arrayShape(['foo' => Type::true(), 1 => Type::false()]), 'array{foo: true, 1: false}'];
Expand Down Expand Up @@ -253,4 +262,28 @@ public function testCannotResolveValueOfInvalidType()
$this->expectException(UnsupportedException::class);
$this->resolver->resolve('value-of<int>');
}

public function testCannotResolveListWithKeyType()
{
$this->expectException(UnsupportedException::class);
$this->resolver->resolve('list<int, string>');
}

public function testCannotResolveInvalidNonEmptyListWithKeyType()
{
$this->expectException(UnsupportedException::class);
$this->resolver->resolve('non-empty-list<int, string>');
}

public function testCannotResolveInvalidArrayKeyType()
{
$this->expectException(InvalidArgumentException::class);
$this->resolver->resolve('array<mixed, string>');
}

public function testCannotResolveInvalidUnionArrayKeyType()
{
$this->expectException(InvalidArgumentException::class);
$this->resolver->resolve('array<int|mixed, string>');
}
}
10 changes: 10 additions & 0 deletions src/Symfony/Component/TypeInfo/Type/CollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public function __construct(
if (!$keyType instanceof BuiltinType || TypeIdentifier::INT !== $keyType->getTypeIdentifier()) {
throw new InvalidArgumentException(\sprintf('"%s" is not a valid list key type.', (string) $keyType));
}
} elseif ($type instanceof GenericType && $type->getWrappedType() instanceof BuiltinType && TypeIdentifier::ARRAY === $type->getWrappedType()->getTypeIdentifier()) {
$keyType = $this->getCollectionKeyType();

$keyTypes = $keyType instanceof UnionType ? $keyType->getTypes() : [$keyType];

foreach ($keyTypes as $type) {
if (!$type instanceof BuiltinType || !\in_array($type->getTypeIdentifier(), [TypeIdentifier::INT, TypeIdentifier::STRING], true)) {
throw new InvalidArgumentException(\sprintf('"%s" is not a valid array key type.', (string) $keyType));
}
}
}
}

Expand Down
Loading
Loading