diff --git a/Model/ModelRegistry.php b/Model/ModelRegistry.php index 8926264..db346fb 100644 --- a/Model/ModelRegistry.php +++ b/Model/ModelRegistry.php @@ -144,8 +144,8 @@ final class ModelRegistry 'built_in_type' => $type->getBuiltinType(), 'nullable' => $type->isNullable(), 'collection' => $type->isCollection(), - 'collection_key_types' => $type->isCollection() ? array_map($getType, $type->getCollectionKeyTypes()) : null, - 'collection_value_types' => $type->isCollection() ? array_map($getType, $type->getCollectionValueTypes()) : null, + 'collection_key_types' => $type->isCollection() ? array_map($getType, $this->getCollectionKeyTypes($type)) : null, + 'collection_value_types' => $type->isCollection() ? array_map($getType, $this->getCollectionValueTypes($type)) : null, ]; }; @@ -186,6 +186,26 @@ final class ModelRegistry } } + private function getCollectionKeyTypes(Type $type): array + { + // BC layer, this condition should be removed after removing support for symfony < 5.3 + if (!method_exists($type, 'getCollectionKeyTypes')) { + return null !== $type->getCollectionKeyType() ? [$type->getCollectionKeyType()] : []; + } + + return $type->getCollectionKeyTypes(); + } + + private function getCollectionValueTypes(Type $type): array + { + // BC layer, this condition should be removed after removing support for symfony < 5.3 + if (!method_exists($type, 'getCollectionValueTypes')) { + return null !== $type->getCollectionValueType() ? [$type->getCollectionValueType()] : []; + } + + return $type->getCollectionValueTypes(); + } + private function getCollectionValueType(Type $type): ?Type { // BC layer, this condition should be removed after removing support for symfony < 5.3 diff --git a/Tests/Model/ModelRegistryTest.php b/Tests/Model/ModelRegistryTest.php index 66eb16c..ef7fd85 100644 --- a/Tests/Model/ModelRegistryTest.php +++ b/Tests/Model/ModelRegistryTest.php @@ -34,7 +34,10 @@ class ModelRegistryTest extends TestCase $this->assertEquals('#/components/schemas/array', $registry->register(new Model($type, ['group1']))); } - public function testNameCollisionsAreLogged() + /** + * @dataProvider provideNameCollisionsTypes + */ + public function testNameCollisionsAreLogged(Type $type, array $arrayType) { $logger = $this->createMock(LoggerInterface::class); $logger @@ -43,26 +46,12 @@ class ModelRegistryTest extends TestCase ->with( 'Can not assign a name for the model, the name "ModelRegistryTest" has already been taken.', [ 'model' => [ - 'type' => [ - 'class' => 'Nelmio\\ApiDocBundle\\Tests\\Model\\ModelRegistryTest', - 'built_in_type' => 'object', - 'nullable' => false, - 'collection' => false, - 'collection_key_types' => null, - 'collection_value_types' => null, - ], + 'type' => $arrayType, 'options' => null, 'groups' => ['group2'], ], 'taken_by' => [ - 'type' => [ - 'class' => 'Nelmio\\ApiDocBundle\\Tests\\Model\\ModelRegistryTest', - 'built_in_type' => 'object', - 'nullable' => false, - 'collection' => false, - 'collection_key_types' => null, - 'collection_value_types' => null, - ], + 'type' => $arrayType, 'options' => null, 'groups' => ['group1'], ], @@ -71,11 +60,46 @@ class ModelRegistryTest extends TestCase $registry = new ModelRegistry([], new OA\OpenApi([]), []); $registry->setLogger($logger); - $type = new Type(Type::BUILTIN_TYPE_OBJECT, false, self::class); $registry->register(new Model($type, ['group1'])); $registry->register(new Model($type, ['group2'])); } + public function provideNameCollisionsTypes() + { + yield [ + new Type(Type::BUILTIN_TYPE_OBJECT, false, self::class), + [ + 'class' => 'Nelmio\\ApiDocBundle\\Tests\\Model\\ModelRegistryTest', + 'built_in_type' => 'object', + 'nullable' => false, + 'collection' => false, + 'collection_key_types' => null, + 'collection_value_types' => null, + ], + ]; + + yield [ + new Type(Type::BUILTIN_TYPE_OBJECT, false, self::class, true, new Type(Type::BUILTIN_TYPE_OBJECT)), + [ + 'class' => 'Nelmio\\ApiDocBundle\\Tests\\Model\\ModelRegistryTest', + 'built_in_type' => 'object', + 'nullable' => false, + 'collection' => true, + 'collection_key_types' => [ + [ + 'class' => null, + 'built_in_type' => 'object', + 'nullable' => false, + 'collection' => false, + 'collection_key_types' => null, + 'collection_value_types' => null, + ], + ], + 'collection_value_types' => [], + ], + ]; + } + public function testNameCollisionsAreLoggedWithAlternativeNames() { $ref = new \ReflectionClass(self::class);