Skip to content

Commit 111e71a

Browse files
committed
bug #41463 [Serializer][Validator] Fix not null return from "getCollectionValueTypes" (jderusse)
This PR was merged into the 5.3 branch. Discussion ---------- [Serializer][Validator] Fix not null return from "getCollectionValueTypes" | Q | A | ------------- | --- | Branch? | 5.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Currently experimenting `An exception has been thrown during the rendering of a template ("Notice: Undefined offset: 0").` When a property is an array and PropertyInfo is not able to guess the type of CollectionValue Commits ------- c4dcfd1fde Fix not null return from "getCollectionValueTypes"
2 parents 8a729fa + 62e93b1 commit 111e71a

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

Mapping/Loader/PropertyInfoLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
119119
}
120120
if (!$hasTypeConstraint) {
121121
if (1 === \count($builtinTypes)) {
122-
if ($types[0]->isCollection() && (null !== $collectionValueType = $types[0]->getCollectionValueTypes())) {
122+
if ($types[0]->isCollection() && \count($collectionValueType = $types[0]->getCollectionValueTypes()) > 0) {
123123
[$collectionValueType] = $collectionValueType;
124124
$this->handleAllConstraint($property, $allConstraint, $collectionValueType, $metadata);
125125
}

Tests/Fixtures/PropertyInfoLoaderEntity.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class PropertyInfoLoaderEntity
2323
public $scalar;
2424
public $object;
2525
public $collection;
26+
public $collectionOfUnknown;
2627

2728
/**
2829
* @Assert\Type(type="int")

Tests/Mapping/Loader/PropertyInfoLoaderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function testLoadClassMetadata()
4444
'scalar',
4545
'object',
4646
'collection',
47+
'collectionOfUnknown',
4748
'alreadyMappedType',
4849
'alreadyMappedNotNull',
4950
'alreadyMappedNotBlank',
@@ -61,6 +62,7 @@ public function testLoadClassMetadata()
6162
[new Type(Type::BUILTIN_TYPE_STRING, true), new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_BOOL)],
6263
[new Type(Type::BUILTIN_TYPE_OBJECT, true, Entity::class)],
6364
[new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true, null, new Type(Type::BUILTIN_TYPE_OBJECT, false, Entity::class))],
65+
[new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true)],
6466
[new Type(Type::BUILTIN_TYPE_FLOAT, true)], // The existing constraint is float
6567
[new Type(Type::BUILTIN_TYPE_STRING, true)],
6668
[new Type(Type::BUILTIN_TYPE_STRING, true)],
@@ -81,6 +83,7 @@ public function testLoadClassMetadata()
8183
true,
8284
true,
8385
true,
86+
true,
8487
false,
8588
true
8689
))
@@ -135,6 +138,13 @@ public function testLoadClassMetadata()
135138
$this->assertInstanceOf(TypeConstraint::class, $collectionConstraints[0]->constraints[1]);
136139
$this->assertSame(Entity::class, $collectionConstraints[0]->constraints[1]->type);
137140

141+
$collectionOfUnknownMetadata = $classMetadata->getPropertyMetadata('collectionOfUnknown');
142+
$this->assertCount(1, $collectionOfUnknownMetadata);
143+
$collectionOfUnknownConstraints = $collectionOfUnknownMetadata[0]->getConstraints();
144+
$this->assertCount(1, $collectionOfUnknownConstraints);
145+
$this->assertInstanceOf(TypeConstraint::class, $collectionOfUnknownConstraints[0]);
146+
$this->assertSame('array', $collectionOfUnknownConstraints[0]->type);
147+
138148
$alreadyMappedTypeMetadata = $classMetadata->getPropertyMetadata('alreadyMappedType');
139149
$this->assertCount(1, $alreadyMappedTypeMetadata);
140150
$alreadyMappedTypeConstraints = $alreadyMappedTypeMetadata[0]->getConstraints();

0 commit comments

Comments
 (0)