mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-09 19:19:29 +03:00
Merge pull request #2037 from nelmio/APIPLATFORM
Add Api-Platform 3.x support
This commit is contained in:
commit
a46458e1f3
1
.github/workflows/continuous-integration.yml
vendored
1
.github/workflows/continuous-integration.yml
vendored
@ -36,7 +36,6 @@ jobs:
|
|||||||
symfony-require: "5.4.*"
|
symfony-require: "5.4.*"
|
||||||
- php-version: 8.1
|
- php-version: 8.1
|
||||||
symfony-require: "6.0.*"
|
symfony-require: "6.0.*"
|
||||||
api-platform: "early"
|
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: "Checkout"
|
- name: "Checkout"
|
||||||
|
@ -168,7 +168,7 @@ final class NelmioApiDocExtension extends Extension implements PrependExtensionI
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ApiPlatform support
|
// 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');
|
$loader->load('api_platform.xml');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,14 +11,16 @@
|
|||||||
|
|
||||||
namespace Nelmio\ApiDocBundle\Describer;
|
namespace Nelmio\ApiDocBundle\Describer;
|
||||||
|
|
||||||
use ApiPlatform\Core\Documentation\Documentation;
|
|
||||||
use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer;
|
use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer;
|
||||||
|
use ApiPlatform\Documentation\DocumentationInterface;
|
||||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||||
|
|
||||||
final class ApiPlatformDescriber extends ExternalDocDescriber
|
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')) {
|
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)));
|
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 +29,7 @@ final class ApiPlatformDescriber extends ExternalDocDescriber
|
|||||||
$documentation = (array) $normalizer->normalize(
|
$documentation = (array) $normalizer->normalize(
|
||||||
$documentation,
|
$documentation,
|
||||||
null,
|
null,
|
||||||
[DocumentationNormalizer::SPEC_VERSION => 3]
|
class_exists(DocumentationNormalizer::class) ? [DocumentationNormalizer::SPEC_VERSION => 3] : []
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO: remove this
|
// TODO: remove this
|
||||||
|
@ -5,18 +5,17 @@
|
|||||||
|
|
||||||
<services>
|
<services>
|
||||||
<service id="nelmio_api_doc.describers.api_platform" class="Nelmio\ApiDocBundle\Describer\ApiPlatformDescriber" public="false">
|
<service id="nelmio_api_doc.describers.api_platform" class="Nelmio\ApiDocBundle\Describer\ApiPlatformDescriber" public="false">
|
||||||
<argument type="service" id="nelmio_api_doc.describers.api_platform.documentation" />
|
<argument type="service" id="nelmio_api_doc.describers.api_platform.openapi" />
|
||||||
<argument type="service" id="api_platform.swagger.normalizer.documentation" />
|
<argument type="service" id="api_platform.openapi.normalizer" />
|
||||||
|
|
||||||
<tag name="nelmio_api_doc.describer" priority="-100" />
|
<tag name="nelmio_api_doc.describer" priority="-100" />
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
<service id="nelmio_api_doc.describers.api_platform.documentation" class="ApiPlatform\Core\Documentation\Documentation" public="false">
|
<service id="nelmio_api_doc.describers.api_platform.openapi" class="ApiPlatform\OpenApi\OpenApi" public="false">
|
||||||
<factory service="api_platform.action.documentation" method="__invoke" />
|
<factory service="api_platform.openapi.factory" method="__invoke" />
|
||||||
<argument type="service">
|
|
||||||
<service class="Symfony\Component\HttpFoundation\Request" />
|
|
||||||
</argument>
|
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
|
|
||||||
</services>
|
</services>
|
||||||
|
|
||||||
</container>
|
</container>
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
namespace Nelmio\ApiDocBundle\Tests\Describer;
|
namespace Nelmio\ApiDocBundle\Tests\Describer;
|
||||||
|
|
||||||
use ApiPlatform\Core\Documentation\Documentation;
|
use ApiPlatform\Documentation\Documentation;
|
||||||
use ApiPlatform\Metadata\Resource\ResourceNameCollection;
|
use ApiPlatform\Metadata\Resource\ResourceNameCollection;
|
||||||
use Nelmio\ApiDocBundle\Describer\ApiPlatformDescriber;
|
use Nelmio\ApiDocBundle\Describer\ApiPlatformDescriber;
|
||||||
use OpenApi\Annotations\OpenApi;
|
use OpenApi\Annotations\OpenApi;
|
||||||
|
@ -12,48 +12,13 @@
|
|||||||
namespace Nelmio\ApiDocBundle\Tests\Functional\Entity;
|
namespace Nelmio\ApiDocBundle\Tests\Functional\Entity;
|
||||||
|
|
||||||
use ApiPlatform\Core\Annotation\ApiProperty;
|
use ApiPlatform\Core\Annotation\ApiProperty;
|
||||||
use ApiPlatform\Core\Annotation\ApiResource;
|
use Nelmio\ApiDocBundle\Tests\Functional\EntityExcluded;
|
||||||
use Symfony\Component\Validator\Constraints as Assert;
|
|
||||||
|
|
||||||
/**
|
// BC Api-Platform < 3.x
|
||||||
* @author Guilhem N. <egetick@gmail.com>
|
if (!class_exists(ApiProperty::class)) {
|
||||||
*
|
class Dummy extends EntityExcluded\ApiPlatform3\Dummy
|
||||||
* @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;
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
class_alias(EntityExcluded\ApiPlatform2\Dummy::class, Dummy::class);
|
||||||
}
|
}
|
||||||
|
59
Tests/Functional/EntityExcluded/ApiPlatform2/Dummy.php
Normal file
59
Tests/Functional/EntityExcluded/ApiPlatform2/Dummy.php
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the NelmioApiDocBundle package.
|
||||||
|
*
|
||||||
|
* (c) Nelmio
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Nelmio\ApiDocBundle\Tests\Functional\EntityExcluded\ApiPlatform2;
|
||||||
|
|
||||||
|
use ApiPlatform\Core\Annotation\ApiProperty;
|
||||||
|
use ApiPlatform\Core\Annotation\ApiResource;
|
||||||
|
use Symfony\Component\Validator\Constraints as Assert;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Guilhem N. <egetick@gmail.com>
|
||||||
|
*
|
||||||
|
* @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;
|
||||||
|
}
|
||||||
|
}
|
63
Tests/Functional/EntityExcluded/ApiPlatform3/Dummy.php
Normal file
63
Tests/Functional/EntityExcluded/ApiPlatform3/Dummy.php
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the NelmioApiDocBundle package.
|
||||||
|
*
|
||||||
|
* (c) Nelmio
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Nelmio\ApiDocBundle\Tests\Functional\EntityExcluded\ApiPlatform3;
|
||||||
|
|
||||||
|
use ApiPlatform\Metadata\ApiProperty;
|
||||||
|
use ApiPlatform\Metadata\ApiResource;
|
||||||
|
use ApiPlatform\Metadata\Get;
|
||||||
|
use ApiPlatform\Metadata\GetCollection;
|
||||||
|
use ApiPlatform\Metadata\Post;
|
||||||
|
use Symfony\Component\Validator\Constraints as Assert;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Guilhem N. <egetick@gmail.com>
|
||||||
|
*/
|
||||||
|
#[
|
||||||
|
ApiResource(
|
||||||
|
operations: [
|
||||||
|
new Get(name: 'get'),
|
||||||
|
new Get(name: 'custom2', uriTemplate: '/foo'),
|
||||||
|
new Post(name: 'custom', uriTemplate: '/foo'),
|
||||||
|
new GetCollection(),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
]
|
||||||
|
class Dummy
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @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;
|
||||||
|
}
|
||||||
|
}
|
@ -11,7 +11,8 @@
|
|||||||
|
|
||||||
namespace Nelmio\ApiDocBundle\Tests\Functional;
|
namespace Nelmio\ApiDocBundle\Tests\Functional;
|
||||||
|
|
||||||
use ApiPlatform\Core\Bridge\Symfony\Bundle\ApiPlatformBundle;
|
use ApiPlatform\Core\Annotation\ApiProperty;
|
||||||
|
use ApiPlatform\Symfony\Bundle\ApiPlatformBundle;
|
||||||
use Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle;
|
use Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle;
|
||||||
use FOS\RestBundle\FOSRestBundle;
|
use FOS\RestBundle\FOSRestBundle;
|
||||||
use Hateoas\Configuration\Embedded;
|
use Hateoas\Configuration\Embedded;
|
||||||
@ -163,7 +164,11 @@ class TestKernel extends Kernel
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$c->loadFromExtension('api_platform', [
|
$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', [
|
$c->loadFromExtension('fos_rest', [
|
||||||
|
@ -48,7 +48,7 @@
|
|||||||
"symfony/twig-bundle": "^4.4|^5.2|^6.0",
|
"symfony/twig-bundle": "^4.4|^5.2|^6.0",
|
||||||
"symfony/validator": "^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",
|
"symfony/deprecation-contracts": "^2.1|^3",
|
||||||
|
|
||||||
"friendsofsymfony/rest-bundle": "^2.8|^3.0",
|
"friendsofsymfony/rest-bundle": "^2.8|^3.0",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user