Fix usage of getCollectionValueTypes and getCollectionKeyTypes (#1910)

* Fix usage of getCollectionValueTypes and getCollectionKeyTypes

* fix cs
This commit is contained in:
Guilhem Niot 2021-11-30 13:06:32 +01:00 committed by GitHub
parent d59dbbd859
commit 82bb3cb916
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 20 deletions

View File

@ -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

View File

@ -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);