Skip to content

Commit

Permalink
bug #41463 [Serializer][Validator] Fix not null return from "getColle…
Browse files Browse the repository at this point in the history
…ctionValueTypes" (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"
  • Loading branch information
fabpot committed Jun 2, 2021
2 parents 8a729fa + 62e93b1 commit 111e71a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Mapping/Loader/PropertyInfoLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
}
if (!$hasTypeConstraint) {
if (1 === \count($builtinTypes)) {
if ($types[0]->isCollection() && (null !== $collectionValueType = $types[0]->getCollectionValueTypes())) {
if ($types[0]->isCollection() && \count($collectionValueType = $types[0]->getCollectionValueTypes()) > 0) {
[$collectionValueType] = $collectionValueType;
$this->handleAllConstraint($property, $allConstraint, $collectionValueType, $metadata);
}
Expand Down
1 change: 1 addition & 0 deletions Tests/Fixtures/PropertyInfoLoaderEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class PropertyInfoLoaderEntity
public $scalar;
public $object;
public $collection;
public $collectionOfUnknown;

/**
* @Assert\Type(type="int")
Expand Down
10 changes: 10 additions & 0 deletions Tests/Mapping/Loader/PropertyInfoLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function testLoadClassMetadata()
'scalar',
'object',
'collection',
'collectionOfUnknown',
'alreadyMappedType',
'alreadyMappedNotNull',
'alreadyMappedNotBlank',
Expand All @@ -61,6 +62,7 @@ public function testLoadClassMetadata()
[new Type(Type::BUILTIN_TYPE_STRING, true), new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_BOOL)],
[new Type(Type::BUILTIN_TYPE_OBJECT, true, Entity::class)],
[new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true, null, new Type(Type::BUILTIN_TYPE_OBJECT, false, Entity::class))],
[new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true)],
[new Type(Type::BUILTIN_TYPE_FLOAT, true)], // The existing constraint is float
[new Type(Type::BUILTIN_TYPE_STRING, true)],
[new Type(Type::BUILTIN_TYPE_STRING, true)],
Expand All @@ -81,6 +83,7 @@ public function testLoadClassMetadata()
true,
true,
true,
true,
false,
true
))
Expand Down Expand Up @@ -135,6 +138,13 @@ public function testLoadClassMetadata()
$this->assertInstanceOf(TypeConstraint::class, $collectionConstraints[0]->constraints[1]);
$this->assertSame(Entity::class, $collectionConstraints[0]->constraints[1]->type);

$collectionOfUnknownMetadata = $classMetadata->getPropertyMetadata('collectionOfUnknown');
$this->assertCount(1, $collectionOfUnknownMetadata);
$collectionOfUnknownConstraints = $collectionOfUnknownMetadata[0]->getConstraints();
$this->assertCount(1, $collectionOfUnknownConstraints);
$this->assertInstanceOf(TypeConstraint::class, $collectionOfUnknownConstraints[0]);
$this->assertSame('array', $collectionOfUnknownConstraints[0]->type);

$alreadyMappedTypeMetadata = $classMetadata->getPropertyMetadata('alreadyMappedType');
$this->assertCount(1, $alreadyMappedTypeMetadata);
$alreadyMappedTypeConstraints = $alreadyMappedTypeMetadata[0]->getConstraints();
Expand Down

0 comments on commit 111e71a

Please sign in to comment.