Description
Symfony version(s) affected: 5.3.0-RC1
Description
When enabling auto_mapping validation, crash appear when validating an entity with a json-type attribute (e.g. roles attribute of an User entity)
How to reproduce
1/ Enable auto_mapping validation
framework:
auto_mapping:
App\Entity\: [ ]
2/Create an entity with a 'json' type (with make:user
for example)
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
//...
/**
* @ORM\Column(type="json")
*/
private array $roles = [];
//...
3/ Validate an instance of this entity in any way. It will results in a Warning: Undefined array key 0
error.
Possible Solution
I think it is due to the recent changes made in Symfony\Component\PropertyInfo\Type
class (MR #39020). getCollectionValueTypes()
is now used instead of getCollectionValueType()
in Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader#loadClassMetadata()
method.
There is a type mismatching in a condition now :
// Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader.php (line 120)
if (!$hasTypeConstraint) {
if (1 === \count($builtinTypes)) {
if ($types[0]->isCollection() && (null !== $collectionValueType = $types[0]->getCollectionValueTypes())) {
[$collectionValueType] = $collectionValueType;
$this->handleAllConstraint($property, $allConstraint, $collectionValueType, $metadata);
}
$metadata->addPropertyConstraint($property, $this->getTypeConstraint($builtinTypes[0], $types[0]));
} elseif ($scalar) {
$metadata->addPropertyConstraint($property, new Type(['type' => 'scalar']));
}
}
I think condition (null !== $collectionValueType = $types[0]->getCollectionValueTypes()))
should be changed to ([] !== $collectionValueType = $types[0]->getCollectionValueTypes()))
because getCollectionValueTypes()
returns only array.
At least, this solution fixes my problem but I am not aware of other getCollectionValueTypes()
usages and they should be verified because this new method is slightly different than the older one (getCollectionValueType()
).