10000 bug #41463 [Serializer][Validator] Fix not null return from "getColle… · symfony/symfony@f8e021a · GitHub
[go: up one dir, main page]

Skip to content

Commit f8e021a

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 ------- c4dcfd1 Fix not null return from "getCollectionValueTypes"
2 parents f58bc9f + c4dcfd1 commit f8e021a

File tree

5 files changed

+15
-13
lines changed

5 files changed

+15
-13
lines changed

src/Symfony/Component/PropertyInfo/Type.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,7 @@ public function getCollectionValueType(): ?self
179179
{
180180
trigger_deprecation('symfony/property-info', '5.3', 'The "%s()" method is deprecated, use "getCollectionValueTypes()" instead.', __METHOD__);
181181

182-
$type = $this->getCollectionValueTypes();
183-
if (0 === \count($type)) {
184-
return null;
185-
}
186-
187-
if (\is_array($type)) {
188-
[$type] = $type;
189-
}
190-
191-
return $type;
182+
return $this->getCollectionValueTypes()[0] ?? null;
192183
}
193184

194185
/**

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,13 +473,13 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
473473
if (null !== $collectionKeyType = $type->getCollectionKeyTypes()) {
474474
[$context['key_type']] = $collectionKeyType;
475475
}
476-
} elseif ($type->isCollection() && null !== ($collectionValueType = $type->getCollectionValueTypes()) && \count($collectionValueType) > 0 && Type::BUILTIN_TYPE_ARRAY === $collectionValueType[0]->getBuiltinType()) {
476+
} elseif ($type->isCollection() && \count($collectionValueType = $type->getCollectionValueTypes()) > 0 && Type::BUILTIN_TYPE_ARRAY === $collectionValueType[0]->getBuiltinType()) {
477477
// get inner type for any nested array
478478
[$innerType] = $collectionValueType;
479479

480480
// note that it will break for any other builtinType
481481
$dimensions = '[]';
482-
while (null !== $innerType->getCollectionValueTypes() && Type::BUILTIN_TYPE_ARRAY === $innerType->getBuiltinType()) {
482+
while (\count($innerType->getCollectionValueTypes()) > 0 && Type::BUILTIN_TYPE_ARRAY === $innerType->getBuiltinType()) {
483483
$dimensions .= '[]';
484484
[$innerType] = $innerType->getCollectionValueTypes();
485485
}

src/Symfony/Component/Validator/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
}

src/Symfony/Component/Validator/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")

src/Symfony/Component/Validator/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)
0