Skip to content

Commit

Permalink
Apply #1863 to 3.x branch (#1888)
Browse files Browse the repository at this point in the history
This also fixes #1877
  • Loading branch information
danielgelling committed Nov 3, 2021
1 parent aa18c6c commit c0b57fd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
18 changes: 14 additions & 4 deletions Model/ModelRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ private function generateModelName(Model $model): string

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 @@ -144,13 +144,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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public function __construct(iterable $propertyDescribers = [])

public function describe(Type $type, Schema $property, array $groups = null)
{
$type = $type->getCollectionValueType();
// BC layer for symfony < 5.3
$type = method_exists($type, 'getCollectionValueTypes') ?
($type->getCollectionValueTypes()[0] ?? null) :
$type->getCollectionValueType();
if (null === $type) {
throw new UndocumentedArrayItemsException();
}
Expand Down
14 changes: 13 additions & 1 deletion PropertyDescriber/ObjectPropertyDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,19 @@ class ObjectPropertyDescriber implements PropertyDescriberInterface, ModelRegist

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

$property->setRef(
$this->modelRegistry->register(new Model($type, $groups))
Expand Down

0 comments on commit c0b57fd

Please sign in to comment.