From c0b57fd371955029b46e56d4f09824659155855c Mon Sep 17 00:00:00 2001 From: Daniel Gelling Date: Thu, 4 Nov 2021 00:07:01 +0100 Subject: [PATCH 1/2] 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 7cc20f8..02cb4c2 100644 --- a/Model/ModelRegistry.php +++ b/Model/ModelRegistry.php @@ -126,8 +126,8 @@ final class ModelRegistry 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 @@ final class ModelRegistry 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 @@ final class ModelRegistry 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 defb84f..89b50f4 100644 --- a/PropertyDescriber/ArrayPropertyDescriber.php +++ b/PropertyDescriber/ArrayPropertyDescriber.php @@ -31,7 +31,10 @@ class ArrayPropertyDescriber implements PropertyDescriberInterface, ModelRegistr 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 cc9619e..27ca76f 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)) From f5fb7a408824d44d36453f6edff20f9fa05296a1 Mon Sep 17 00:00:00 2001 From: Guilhem Niot Date: Sat, 11 Dec 2021 14:22:14 +0100 Subject: [PATCH 2/2] Fix deprecations (#1924) --- DependencyInjection/Configuration.php | 2 +- Resources/doc/index.rst | 2 +- Tests/Functional/ArrayItemsErrorTest.php | 3 ++- Tests/Functional/BazingaFunctionalTest.php | 3 ++- Tests/Functional/JMSFunctionalTest.php | 4 +++- Tests/Functional/TestKernel.php | 6 +++--- Tests/Functional/WebTestCase.php | 3 ++- 7 files changed, 14 insertions(+), 9 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 7fcbb65..f24561a 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -16,7 +16,7 @@ use Symfony\Component\Config\Definition\ConfigurationInterface; final class Configuration implements ConfigurationInterface { - public function getConfigTreeBuilder() + public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('nelmio_api_doc'); diff --git a/Resources/doc/index.rst b/Resources/doc/index.rst index 97277f7..d4200b6 100644 --- a/Resources/doc/index.rst +++ b/Resources/doc/index.rst @@ -39,7 +39,7 @@ Open a command console, enter your project directory and execute the following c class AppKernel extends Kernel { - public function registerBundles() + public function registerBundles(): iterable { $bundles = [ // ... diff --git a/Tests/Functional/ArrayItemsErrorTest.php b/Tests/Functional/ArrayItemsErrorTest.php index cd8214e..2f840e7 100644 --- a/Tests/Functional/ArrayItemsErrorTest.php +++ b/Tests/Functional/ArrayItemsErrorTest.php @@ -12,6 +12,7 @@ namespace Nelmio\ApiDocBundle\Tests\Functional; use Nelmio\ApiDocBundle\Exception\UndocumentedArrayItemsException; +use Symfony\Component\HttpKernel\KernelInterface; class ArrayItemsErrorTest extends WebTestCase { @@ -30,7 +31,7 @@ class ArrayItemsErrorTest extends WebTestCase $this->getSwaggerDefinition(); } - protected static function createKernel(array $options = []) + protected static function createKernel(array $options = []): KernelInterface { return new TestKernel(TestKernel::ERROR_ARRAY_ITEMS); } diff --git a/Tests/Functional/BazingaFunctionalTest.php b/Tests/Functional/BazingaFunctionalTest.php index 6be482b..cf4a786 100644 --- a/Tests/Functional/BazingaFunctionalTest.php +++ b/Tests/Functional/BazingaFunctionalTest.php @@ -12,6 +12,7 @@ namespace Nelmio\ApiDocBundle\Tests\Functional; use Hateoas\Configuration\Embedded; +use Symfony\Component\HttpKernel\KernelInterface; class BazingaFunctionalTest extends WebTestCase { @@ -120,7 +121,7 @@ class BazingaFunctionalTest extends WebTestCase ], $this->getModel('BazingaUserTyped')->toArray()); } - protected static function createKernel(array $options = []) + protected static function createKernel(array $options = []): KernelInterface { return new TestKernel(TestKernel::USE_JMS | TestKernel::USE_BAZINGA); } diff --git a/Tests/Functional/JMSFunctionalTest.php b/Tests/Functional/JMSFunctionalTest.php index 5645f0f..363323e 100644 --- a/Tests/Functional/JMSFunctionalTest.php +++ b/Tests/Functional/JMSFunctionalTest.php @@ -11,6 +11,8 @@ namespace Nelmio\ApiDocBundle\Tests\Functional; +use Symfony\Component\HttpKernel\KernelInterface; + class JMSFunctionalTest extends WebTestCase { protected function setUp(): void @@ -302,7 +304,7 @@ class JMSFunctionalTest extends WebTestCase ], $this->getModel('JMSNamingStrategyConstraints')->toArray()); } - protected static function createKernel(array $options = []) + protected static function createKernel(array $options = []): KernelInterface { return new TestKernel(TestKernel::USE_JMS); } diff --git a/Tests/Functional/TestKernel.php b/Tests/Functional/TestKernel.php index b56220d..dc1a47e 100644 --- a/Tests/Functional/TestKernel.php +++ b/Tests/Functional/TestKernel.php @@ -52,7 +52,7 @@ class TestKernel extends Kernel /** * {@inheritdoc} */ - public function registerBundles() + public function registerBundles(): iterable { $bundles = [ new FrameworkBundle(), @@ -241,7 +241,7 @@ class TestKernel extends Kernel /** * {@inheritdoc} */ - public function getCacheDir() + public function getCacheDir(): string { return parent::getCacheDir().'/'.$this->flags; } @@ -249,7 +249,7 @@ class TestKernel extends Kernel /** * {@inheritdoc} */ - public function getLogDir() + public function getLogDir(): string { return parent::getLogDir().'/'.$this->flags; } diff --git a/Tests/Functional/WebTestCase.php b/Tests/Functional/WebTestCase.php index 24d371e..4fc2d72 100644 --- a/Tests/Functional/WebTestCase.php +++ b/Tests/Functional/WebTestCase.php @@ -14,10 +14,11 @@ namespace Nelmio\ApiDocBundle\Tests\Functional; use EXSyst\Component\Swagger\Operation; use EXSyst\Component\Swagger\Schema; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase; +use Symfony\Component\HttpKernel\KernelInterface; class WebTestCase extends BaseWebTestCase { - protected static function createKernel(array $options = []) + protected static function createKernel(array $options = []): KernelInterface { return new TestKernel(); }