Skip to content

Commit

Permalink
Merge pull request #1863 from onatskyy/fix_getCollectionValueType_dep…
Browse files Browse the repository at this point in the history
…recation

Fix Symfony\Component\PropertyInfo\Type::getCollectionValueType() deprecation notice for symfony >=5.3
  • Loading branch information
GuilhemN committed Aug 27, 2021
2 parents 74a440a + 0667881 commit 2f25ca6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
18 changes: 14 additions & 4 deletions Model/ModelRegistry.php
Expand Up @@ -158,8 +158,8 @@ private function modelToArray(Model $model): array

private function getTypeShortName(Type $type): string
{
if (null !== $type->getCollectionValueType()) {
return $this->getTypeShortName($type->getCollectionValueType()).'[]';
if (null !== $collectionType = $this->getCollectionValueType($type)) {
return $this->getTypeShortName($collectionType).'[]';
}

if (Type::BUILTIN_TYPE_OBJECT === $type->getBuiltinType()) {
Expand All @@ -176,13 +176,23 @@ private function typeToString(Type $type): string
if (Type::BUILTIN_TYPE_OBJECT === $type->getBuiltinType()) {
return $type->getClassName();
} elseif ($type->isCollection()) {
if (null !== $type->getCollectionValueType()) {
return $this->typeToString($type->getCollectionValueType()).'[]';
if (null !== $collectionType = $this->getCollectionValueType($type)) {
return $this->typeToString($collectionType).'[]';
} else {
return 'mixed[]';
}
} else {
return $type->getBuiltinType();
}
}

private function getCollectionValueType(Type $type): ?Type
{
// BC layer, this condition should be removed after removing support for symfony < 5.3
if (!method_exists($type, 'getCollectionValueTypes')) {
return $type->getCollectionValueType();
}

return $type->getCollectionValueTypes()[0] ?? null;
}
}
5 changes: 4 additions & 1 deletion PropertyDescriber/ArrayPropertyDescriber.php
Expand Up @@ -32,7 +32,10 @@ public function __construct(iterable $propertyDescribers = [])

public function describe(array $types, OA\Schema $property, array $groups = null)
{
$type = $types[0]->getCollectionValueType();
// BC layer for symfony < 5.3
$type = method_exists($types[0], 'getCollectionValueTypes') ?
($types[0]->getCollectionValueTypes()[0] ?? null) :
$types[0]->getCollectionValueType();
if (null === $type) {
throw new UndocumentedArrayItemsException();
}
Expand Down
12 changes: 11 additions & 1 deletion PropertyDescriber/ObjectPropertyDescriber.php
Expand Up @@ -23,7 +23,17 @@ class ObjectPropertyDescriber implements PropertyDescriberInterface, ModelRegist

public function describe(array $types, OA\Schema $property, array $groups = null)
{
$type = new Type($types[0]->getBuiltinType(), false, $types[0]->getClassName(), $types[0]->isCollection(), $types[0]->getCollectionKeyType(), $types[0]->getCollectionValueType()); // ignore nullable field
$type = new Type(
$types[0]->getBuiltinType(),
false,
$types[0]->getClassName(),
$types[0]->isCollection(),
$types[0]->getCollectionKeyType(),
// BC layer for symfony < 5.3
method_exists($types[0], 'getCollectionValueTypes') ?
($types[0]->getCollectionValueTypes()[0] ?? null) :
$types[0]->getCollectionValueType()
); // ignore nullable field

if ($types[0]->isNullable()) {
$property->nullable = true;
Expand Down

0 comments on commit 2f25ca6

Please sign in to comment.