Skip to content

Commit

Permalink
Fix Symfony\Component\PropertyInfo\Type::getCollectionValueType() dep…
Browse files Browse the repository at this point in the history
…recation notice for symfony >=5.3
  • Loading branch information
Alexander Onatskiy committed Aug 18, 2021
1 parent 78bff2f commit ec96853
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
14 changes: 12 additions & 2 deletions Model/ModelRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private function generateModelName(Model $model): string

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

Expand All @@ -142,7 +142,7 @@ private function typeToString(Type $type): string
if (Type::BUILTIN_TYPE_OBJECT === $type->getBuiltinType()) {
return $type->getClassName();
} elseif ($type->isCollection()) {
if (null !== $collectionType = $type->getCollectionValueTypes()[0] ?? null) {
if (null !== $collectionType = $this->getCollectionValueType($type)) {
return $this->typeToString($collectionType).'[]';
} else {
return 'mixed[]';
Expand All @@ -151,4 +151,14 @@ private function typeToString(Type $type): string
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;
}
}
6 changes: 5 additions & 1 deletion PropertyDescriber/ArrayPropertyDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public function __construct(iterable $propertyDescribers = [])

public function describe(array $types, OA\Schema $property, array $groups = null)
{
if (null === $type = $types[0]->getCollectionValueTypes()[0] ?? null) {
// 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
5 changes: 4 additions & 1 deletion PropertyDescriber/ObjectPropertyDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public function describe(array $types, OA\Schema $property, array $groups = null
$types[0]->getClassName(),
$types[0]->isCollection(),
$types[0]->getCollectionKeyType(),
$types[0]->getCollectionValueTypes()[0] ?? null
// 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()) {
Expand Down

0 comments on commit ec96853

Please sign in to comment.