Skip to content

[Serializer][Validator] Fix not null return from "getCollectionValueTypes" #41463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2021
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
11 changes: 1 addition & 10 deletions src/Symfony/Component/PropertyInfo/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,7 @@ public function getCollectionValueType(): ?self
{
trigger_deprecation('symfony/property-info', '5.3', 'The "%s()" method is deprecated, use "getCollectionValueTypes()" instead.', __METHOD__);

$type = $this->getCollectionValueTypes();
if (0 === \count($type)) {
return null;
}

if (\is_array($type)) {
[$type] = $type;
}

return $type;
return $this->getCollectionValueTypes()[0] ?? null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,13 +473,13 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
if (null !== $collectionKeyType = $type->getCollectionKeyTypes()) {
[$context['key_type']] = $collectionKeyType;
}
} elseif ($type->isCollection() && null !== ($collectionValueType = $type->getCollectionValueTypes()) && \count($collectionValueType) > 0 && Type::BUILTIN_TYPE_ARRAY === $collectionValueType[0]->getBuiltinType()) {
} elseif ($type->isCollection() && \count($collectionValueType = $type->getCollectionValueTypes()) > 0 && Type::BUILTIN_TYPE_ARRAY === $collectionValueType[0]->getBuiltinType()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the argument of count() can never be null? (same below)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method has been introduced in symfony/property-info: 5.3 and the method is typehinted to return an array.

public function getCollectionValueTypes(): array

// get inner type for any nested array
[$innerType] = $collectionValueType;

// note that it will break for any other builtinType
$dimensions = '[]';
while (null !== $innerType->getCollectionValueTypes() && Type::BUILTIN_TYPE_ARRAY === $innerType->getBuiltinType()) {
while (\count($innerType->getCollectionValueTypes()) > 0 && Type::BUILTIN_TYPE_ARRAY === $innerType->getBuiltinType()) {
$dimensions .= '[]';
[$innerType] = $innerType->getCollectionValueTypes();
}
Expand Down
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
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
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