From 383fb78360235ea3b633d11ffd0486d909a6afff Mon Sep 17 00:00:00 2001 From: Guilhem Niot Date: Sun, 25 Sep 2022 21:32:09 +0200 Subject: [PATCH 1/4] Add Api-Platform 3.x support --- DependencyInjection/NelmioApiDocExtension.php | 2 +- Describer/ApiPlatformDescriber.php | 9 ++- Resources/config/api_platform.xml | 13 ++-- Tests/Describer/ApiPlatformDescriberTest.php | 2 +- Tests/Functional/Entity/Dummy.php | 49 ++------------ Tests/Functional/Entity/Dummy71.php | 60 +++++++++++++++++ Tests/Functional/Entity/Dummy81.php | 64 +++++++++++++++++++ Tests/Functional/TestKernel.php | 2 +- composer.json | 2 +- 9 files changed, 145 insertions(+), 58 deletions(-) create mode 100644 Tests/Functional/Entity/Dummy71.php create mode 100644 Tests/Functional/Entity/Dummy81.php diff --git a/DependencyInjection/NelmioApiDocExtension.php b/DependencyInjection/NelmioApiDocExtension.php index ce0d0e6..a95beb9 100644 --- a/DependencyInjection/NelmioApiDocExtension.php +++ b/DependencyInjection/NelmioApiDocExtension.php @@ -168,7 +168,7 @@ final class NelmioApiDocExtension extends Extension implements PrependExtensionI } // ApiPlatform support - if (isset($bundles['ApiPlatformBundle']) && class_exists('ApiPlatform\Core\Documentation\Documentation')) { + if (isset($bundles['ApiPlatformBundle']) && class_exists('ApiPlatform\Documentation\Documentation')) { $loader->load('api_platform.xml'); } diff --git a/Describer/ApiPlatformDescriber.php b/Describer/ApiPlatformDescriber.php index 88f092d..4936c48 100644 --- a/Describer/ApiPlatformDescriber.php +++ b/Describer/ApiPlatformDescriber.php @@ -11,14 +11,17 @@ namespace Nelmio\ApiDocBundle\Describer; -use ApiPlatform\Core\Documentation\Documentation; +use ApiPlatform\Documentation\DocumentationInterface; use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer; +use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; final class ApiPlatformDescriber extends ExternalDocDescriber { - public function __construct(Documentation $documentation, NormalizerInterface $normalizer) + public function __construct(DocumentationInterface $documentation, NormalizerInterface $normalizer) { + // var_dump(get_class($documentation)); + // die(); if (!$normalizer->supportsNormalization($documentation, 'json')) { throw new \InvalidArgumentException(sprintf('Argument 2 passed to %s() must implement %s and support normalization of %s. The normalizer provided is an instance of %s.', __METHOD__, NormalizerInterface::class, Documentation::class, get_class($normalizer))); } @@ -27,7 +30,7 @@ final class ApiPlatformDescriber extends ExternalDocDescriber $documentation = (array) $normalizer->normalize( $documentation, null, - [DocumentationNormalizer::SPEC_VERSION => 3] + class_exists(DocumentationNormalizer::class) ? [DocumentationNormalizer::SPEC_VERSION => 3] : [] ); // TODO: remove this diff --git a/Resources/config/api_platform.xml b/Resources/config/api_platform.xml index 24d747c..8edbfe1 100644 --- a/Resources/config/api_platform.xml +++ b/Resources/config/api_platform.xml @@ -5,18 +5,17 @@ - - + + - - - - - + + + + diff --git a/Tests/Describer/ApiPlatformDescriberTest.php b/Tests/Describer/ApiPlatformDescriberTest.php index 03a113b..ffdef4b 100644 --- a/Tests/Describer/ApiPlatformDescriberTest.php +++ b/Tests/Describer/ApiPlatformDescriberTest.php @@ -11,7 +11,7 @@ namespace Nelmio\ApiDocBundle\Tests\Describer; -use ApiPlatform\Core\Documentation\Documentation; +use ApiPlatform\Documentation\Documentation; use ApiPlatform\Metadata\Resource\ResourceNameCollection; use Nelmio\ApiDocBundle\Describer\ApiPlatformDescriber; use OpenApi\Annotations\OpenApi; diff --git a/Tests/Functional/Entity/Dummy.php b/Tests/Functional/Entity/Dummy.php index 1a07b9a..e74c70f 100644 --- a/Tests/Functional/Entity/Dummy.php +++ b/Tests/Functional/Entity/Dummy.php @@ -12,48 +12,9 @@ namespace Nelmio\ApiDocBundle\Tests\Functional\Entity; use ApiPlatform\Core\Annotation\ApiProperty; -use ApiPlatform\Core\Annotation\ApiResource; -use Symfony\Component\Validator\Constraints as Assert; -/** - * @author Guilhem N. - * - * @ApiResource( - * collectionOperations={ - * "get"={"method"="GET"}, - * "custom2"={"path"="/foo", "method"="GET"}, - * "custom"={"path"="/foo", "method"="POST"}, - * }, - * itemOperations={"get"={"method"="GET"}}) - * ) - */ -class Dummy -{ - /** - * @var int - */ - private $id; - - /** - * @var string - * - * @Assert\NotBlank - * @ApiProperty(iri="http://schema.org/name") - */ - private $name; - - public function getId(): int - { - return $this->id; - } - - public function setName(string $name) - { - $this->name = $name; - } - - public function getName(): string - { - return $this->name; - } -} +if (!class_exists(ApiProperty::class)) { + class_alias(Dummy81::class, Dummy::class); +} else { + class_alias(Dummy71::class, Dummy::class); +} \ No newline at end of file diff --git a/Tests/Functional/Entity/Dummy71.php b/Tests/Functional/Entity/Dummy71.php new file mode 100644 index 0000000..ef05cdc --- /dev/null +++ b/Tests/Functional/Entity/Dummy71.php @@ -0,0 +1,60 @@ + + * + * @ApiResource( + * shortName="Dummy", + * collectionOperations={ + * "get"={"method"="GET"}, + * "custom2"={"path"="/foo", "method"="GET"}, + * "custom"={"path"="/foo", "method"="POST"}, + * }, + * itemOperations={"get"={"method"="GET"}}) + * ) + */ +class Dummy71 +{ + /** + * @var int + */ + private $id; + + /** + * @var string + * + * @Assert\NotBlank + * @ApiProperty(iri="http://schema.org/name") + */ + private $name; + + public function getId(): int + { + return $this->id; + } + + public function setName(string $name) + { + $this->name = $name; + } + + public function getName(): string + { + return $this->name; + } +} diff --git a/Tests/Functional/Entity/Dummy81.php b/Tests/Functional/Entity/Dummy81.php new file mode 100644 index 0000000..2f24947 --- /dev/null +++ b/Tests/Functional/Entity/Dummy81.php @@ -0,0 +1,64 @@ + + */ +#[ + ApiResource( + shortName: "Dummy", + operations: [ + new Get(name: "get"), + new Get(name: "custom2", uriTemplate: "/foo"), + new Post(name: "custom", uriTemplate: "/foo"), + new GetCollection(), + ], + ) +] +class Dummy81 +{ + /** + * @var int + */ + private $id; + + /** + * @var string + * + * @Assert\NotBlank + */ + #[ApiProperty(iris: ["http://schema.org/name"])] + private $name; + + public function getId(): int + { + return $this->id; + } + + public function setName(string $name) + { + $this->name = $name; + } + + public function getName(): string + { + return $this->name; + } +} diff --git a/Tests/Functional/TestKernel.php b/Tests/Functional/TestKernel.php index f6ca42d..15d9771 100644 --- a/Tests/Functional/TestKernel.php +++ b/Tests/Functional/TestKernel.php @@ -11,7 +11,7 @@ namespace Nelmio\ApiDocBundle\Tests\Functional; -use ApiPlatform\Core\Bridge\Symfony\Bundle\ApiPlatformBundle; +use ApiPlatform\Symfony\Bundle\ApiPlatformBundle; use Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle; use FOS\RestBundle\FOSRestBundle; use Hateoas\Configuration\Embedded; diff --git a/composer.json b/composer.json index 403be59..8f35aad 100644 --- a/composer.json +++ b/composer.json @@ -48,7 +48,7 @@ "symfony/twig-bundle": "^4.4|^5.2|^6.0", "symfony/validator": "^4.4|^5.2|^6.0", - "api-platform/core": "^2.7.0", + "api-platform/core": "^2.7.0|^3@dev", "symfony/deprecation-contracts": "^2.1|^3", "friendsofsymfony/rest-bundle": "^2.8|^3.0", From cf8635eb958725272678c7e2540604c933653bfc Mon Sep 17 00:00:00 2001 From: Guilhem Niot Date: Sun, 25 Sep 2022 21:33:08 +0200 Subject: [PATCH 2/4] Fix CS --- Describer/ApiPlatformDescriber.php | 3 +-- Tests/Functional/Entity/Dummy.php | 2 +- Tests/Functional/Entity/Dummy81.php | 10 +++++----- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/Describer/ApiPlatformDescriber.php b/Describer/ApiPlatformDescriber.php index 4936c48..920e291 100644 --- a/Describer/ApiPlatformDescriber.php +++ b/Describer/ApiPlatformDescriber.php @@ -11,9 +11,8 @@ namespace Nelmio\ApiDocBundle\Describer; -use ApiPlatform\Documentation\DocumentationInterface; use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer; -use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface; +use ApiPlatform\Documentation\DocumentationInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; final class ApiPlatformDescriber extends ExternalDocDescriber diff --git a/Tests/Functional/Entity/Dummy.php b/Tests/Functional/Entity/Dummy.php index e74c70f..ece0fb1 100644 --- a/Tests/Functional/Entity/Dummy.php +++ b/Tests/Functional/Entity/Dummy.php @@ -17,4 +17,4 @@ if (!class_exists(ApiProperty::class)) { class_alias(Dummy81::class, Dummy::class); } else { class_alias(Dummy71::class, Dummy::class); -} \ No newline at end of file +} diff --git a/Tests/Functional/Entity/Dummy81.php b/Tests/Functional/Entity/Dummy81.php index 2f24947..7b5387a 100644 --- a/Tests/Functional/Entity/Dummy81.php +++ b/Tests/Functional/Entity/Dummy81.php @@ -23,11 +23,11 @@ use Symfony\Component\Validator\Constraints as Assert; */ #[ ApiResource( - shortName: "Dummy", + shortName: 'Dummy', operations: [ - new Get(name: "get"), - new Get(name: "custom2", uriTemplate: "/foo"), - new Post(name: "custom", uriTemplate: "/foo"), + new Get(name: 'get'), + new Get(name: 'custom2', uriTemplate: '/foo'), + new Post(name: 'custom', uriTemplate: '/foo'), new GetCollection(), ], ) @@ -44,7 +44,7 @@ class Dummy81 * * @Assert\NotBlank */ - #[ApiProperty(iris: ["http://schema.org/name"])] + #[ApiProperty(iris: ['http://schema.org/name'])] private $name; public function getId(): int From 2862d8cf6fca9834a7a5f0e3e77c26c1d5d94ea7 Mon Sep 17 00:00:00 2001 From: Guilhem Niot Date: Sun, 25 Sep 2022 21:47:11 +0200 Subject: [PATCH 3/4] fixes --- .github/workflows/continuous-integration.yml | 1 - Tests/Functional/Entity/Dummy.php | 6 ++++-- .../ApiPlatform2/Dummy.php} | 9 ++++----- .../ApiPlatform3/Dummy.php} | 5 ++--- Tests/Functional/TestKernel.php | 7 ++++++- 5 files changed, 16 insertions(+), 12 deletions(-) rename Tests/Functional/{Entity/Dummy71.php => EntityExcluded/ApiPlatform2/Dummy.php} (84%) rename Tests/Functional/{Entity/Dummy81.php => EntityExcluded/ApiPlatform3/Dummy.php} (92%) diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 6fabe66..a505eda 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -36,7 +36,6 @@ jobs: symfony-require: "5.4.*" - php-version: 8.1 symfony-require: "6.0.*" - api-platform: "early" steps: - name: "Checkout" diff --git a/Tests/Functional/Entity/Dummy.php b/Tests/Functional/Entity/Dummy.php index ece0fb1..145aa46 100644 --- a/Tests/Functional/Entity/Dummy.php +++ b/Tests/Functional/Entity/Dummy.php @@ -12,9 +12,11 @@ namespace Nelmio\ApiDocBundle\Tests\Functional\Entity; use ApiPlatform\Core\Annotation\ApiProperty; +use Nelmio\ApiDocBundle\Tests\Functional\EntityExcluded; +// BC Api-Platform < 3.x if (!class_exists(ApiProperty::class)) { - class_alias(Dummy81::class, Dummy::class); + class Dummy extends EntityExcluded\ApiPlatform3\Dummy {} } else { - class_alias(Dummy71::class, Dummy::class); + class_alias(EntityExcluded\ApiPlatform2\Dummy::class, Dummy::class); } diff --git a/Tests/Functional/Entity/Dummy71.php b/Tests/Functional/EntityExcluded/ApiPlatform2/Dummy.php similarity index 84% rename from Tests/Functional/Entity/Dummy71.php rename to Tests/Functional/EntityExcluded/ApiPlatform2/Dummy.php index ef05cdc..5b9ce09 100644 --- a/Tests/Functional/Entity/Dummy71.php +++ b/Tests/Functional/EntityExcluded/ApiPlatform2/Dummy.php @@ -9,17 +9,16 @@ * file that was distributed with this source code. */ -namespace Nelmio\ApiDocBundle\Tests\Functional\Entity; +namespace Nelmio\ApiDocBundle\Tests\Functional\EntityExcluded\ApiPlatform2; -use ApiPlatform\Metadata\ApiProperty; -use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Core\Annotation\ApiProperty; +use ApiPlatform\Core\Annotation\ApiResource; use Symfony\Component\Validator\Constraints as Assert; /** * @author Guilhem N. * * @ApiResource( - * shortName="Dummy", * collectionOperations={ * "get"={"method"="GET"}, * "custom2"={"path"="/foo", "method"="GET"}, @@ -28,7 +27,7 @@ use Symfony\Component\Validator\Constraints as Assert; * itemOperations={"get"={"method"="GET"}}) * ) */ -class Dummy71 +class Dummy { /** * @var int diff --git a/Tests/Functional/Entity/Dummy81.php b/Tests/Functional/EntityExcluded/ApiPlatform3/Dummy.php similarity index 92% rename from Tests/Functional/Entity/Dummy81.php rename to Tests/Functional/EntityExcluded/ApiPlatform3/Dummy.php index 7b5387a..1ed84fa 100644 --- a/Tests/Functional/Entity/Dummy81.php +++ b/Tests/Functional/EntityExcluded/ApiPlatform3/Dummy.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Nelmio\ApiDocBundle\Tests\Functional\Entity; +namespace Nelmio\ApiDocBundle\Tests\Functional\EntityExcluded\ApiPlatform3; use ApiPlatform\Metadata\ApiProperty; use ApiPlatform\Metadata\ApiResource; @@ -23,7 +23,6 @@ use Symfony\Component\Validator\Constraints as Assert; */ #[ ApiResource( - shortName: 'Dummy', operations: [ new Get(name: 'get'), new Get(name: 'custom2', uriTemplate: '/foo'), @@ -32,7 +31,7 @@ use Symfony\Component\Validator\Constraints as Assert; ], ) ] -class Dummy81 +class Dummy { /** * @var int diff --git a/Tests/Functional/TestKernel.php b/Tests/Functional/TestKernel.php index 15d9771..0006cc5 100644 --- a/Tests/Functional/TestKernel.php +++ b/Tests/Functional/TestKernel.php @@ -11,6 +11,7 @@ namespace Nelmio\ApiDocBundle\Tests\Functional; +use ApiPlatform\Core\Annotation\ApiProperty; use ApiPlatform\Symfony\Bundle\ApiPlatformBundle; use Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle; use FOS\RestBundle\FOSRestBundle; @@ -163,7 +164,11 @@ class TestKernel extends Kernel ]); $c->loadFromExtension('api_platform', [ - 'mapping' => ['paths' => ['%kernel.project_dir%/Tests/Functional/Entity']], + 'mapping' => ['paths' => [ + !class_exists(ApiProperty::class) + ? '%kernel.project_dir%/Tests/Functional/EntityExcluded/ApiPlatform3' + : '%kernel.project_dir%/Tests/Functional/EntityExcluded/ApiPlatform2' + ]], ]); $c->loadFromExtension('fos_rest', [ From a011ff8f866d2698d598a7c00de93f6a3b83bf0e Mon Sep 17 00:00:00 2001 From: Guilhem Niot Date: Sun, 25 Sep 2022 21:48:03 +0200 Subject: [PATCH 4/4] CS --- Tests/Functional/Entity/Dummy.php | 4 +++- Tests/Functional/TestKernel.php | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Tests/Functional/Entity/Dummy.php b/Tests/Functional/Entity/Dummy.php index 145aa46..33737a8 100644 --- a/Tests/Functional/Entity/Dummy.php +++ b/Tests/Functional/Entity/Dummy.php @@ -16,7 +16,9 @@ use Nelmio\ApiDocBundle\Tests\Functional\EntityExcluded; // BC Api-Platform < 3.x if (!class_exists(ApiProperty::class)) { - class Dummy extends EntityExcluded\ApiPlatform3\Dummy {} + class Dummy extends EntityExcluded\ApiPlatform3\Dummy + { + } } else { class_alias(EntityExcluded\ApiPlatform2\Dummy::class, Dummy::class); } diff --git a/Tests/Functional/TestKernel.php b/Tests/Functional/TestKernel.php index 0006cc5..f716027 100644 --- a/Tests/Functional/TestKernel.php +++ b/Tests/Functional/TestKernel.php @@ -167,7 +167,7 @@ class TestKernel extends Kernel 'mapping' => ['paths' => [ !class_exists(ApiProperty::class) ? '%kernel.project_dir%/Tests/Functional/EntityExcluded/ApiPlatform3' - : '%kernel.project_dir%/Tests/Functional/EntityExcluded/ApiPlatform2' + : '%kernel.project_dir%/Tests/Functional/EntityExcluded/ApiPlatform2', ]], ]);