From c0b57fd371955029b46e56d4f09824659155855c Mon Sep 17 00:00:00 2001 From: Daniel Gelling Date: Thu, 4 Nov 2021 00:07:01 +0100 Subject: [PATCH] Apply #1863 to 3.x branch (#1888) This also fixes #1877 --- Model/ModelRegistry.php | 18 ++++++++++++++---- PropertyDescriber/ArrayPropertyDescriber.php | 5 ++++- PropertyDescriber/ObjectPropertyDescriber.php | 14 +++++++++++++- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Model/ModelRegistry.php b/Model/ModelRegistry.php index 7cc20f893..02cb4c220 100644 --- a/Model/ModelRegistry.php +++ b/Model/ModelRegistry.php @@ -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()) { @@ -144,8 +144,8 @@ 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[]'; } @@ -153,4 +153,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; + } } diff --git a/PropertyDescriber/ArrayPropertyDescriber.php b/PropertyDescriber/ArrayPropertyDescriber.php index defb84f5e..89b50f451 100644 --- a/PropertyDescriber/ArrayPropertyDescriber.php +++ b/PropertyDescriber/ArrayPropertyDescriber.php @@ -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(); } diff --git a/PropertyDescriber/ObjectPropertyDescriber.php b/PropertyDescriber/ObjectPropertyDescriber.php index cc9619e1e..27ca76fb0 100644 --- a/PropertyDescriber/ObjectPropertyDescriber.php +++ b/PropertyDescriber/ObjectPropertyDescriber.php @@ -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))