Skip to content

Commit

Permalink
fix: use short name when generating parameter description
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Feb 12, 2024
1 parent 86be7df commit 0a84f49
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/OpenApi/Factory/OpenApiFactory.php
Expand Up @@ -70,7 +70,7 @@ final class OpenApiFactory implements OpenApiFactoryInterface
*/
public const OPENAPI_DEFINITION_NAME = 'openapi_definition_name';

public function __construct(private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly SchemaFactoryInterface $jsonSchemaFactory, private readonly TypeFactoryInterface $jsonSchemaTypeFactory, ContainerInterface $filterLocator, private readonly array $formats = [], ?Options $openApiOptions = null, ?PaginationOptions $paginationOptions = null, private readonly ?RouterInterface $router = null)
public function __construct(private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly SchemaFactoryInterface $jsonSchemaFactory, private readonly TypeFactoryInterface $jsonSchemaTypeFactory, ContainerInterface $filterLocator, private readonly array $formats = [], Options $openApiOptions = null, PaginationOptions $paginationOptions = null, private readonly ?RouterInterface $router = null)
{
$this->filterLocator = $filterLocator;
$this->openApiOptions = $openApiOptions ?: new Options('API Platform');
Expand Down Expand Up @@ -265,7 +265,14 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
continue;
}

$parameter = new Parameter($parameterName, 'path', (new \ReflectionClass($uriVariable->getFromClass()))->getShortName().' identifier', true, false, false, ['type' => 'string']);
$fromClass = $this->resourceMetadataFactory->create($uriVariable->getFromClass());
$shortName = $fromClass->getOperation()->getShortName();

if (!$shortName) {
$shortName = (new \ReflectionClass($uriVariable->getFromClass()))->getShortName();

Check warning on line 272 in src/OpenApi/Factory/OpenApiFactory.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Factory/OpenApiFactory.php#L272

Added line #L272 was not covered by tests
}

$parameter = new Parameter($parameterName, 'path', $shortName.' identifier', true, false, false, ['type' => 'string']);
if ($this->hasParameter($openapiOperation, $parameter)) {
continue;
}
Expand Down Expand Up @@ -388,7 +395,7 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
}
}

private function buildOpenApiResponse(array $existingResponses, int|string $status, string $description, ?Model\Operation $openapiOperation = null, ?HttpOperation $operation = null, ?array $responseMimeTypes = null, ?array $operationOutputSchemas = null, ?ResourceMetadataCollection $resourceMetadataCollection = null): Model\Operation
private function buildOpenApiResponse(array $existingResponses, int|string $status, string $description, Model\Operation $openapiOperation = null, HttpOperation $operation = null, array $responseMimeTypes = null, array $operationOutputSchemas = null, ResourceMetadataCollection $resourceMetadataCollection = null): Model\Operation
{
if (isset($existingResponses[$status])) {
return $openapiOperation;
Expand Down
41 changes: 41 additions & 0 deletions src/OpenApi/Tests/Factory/OpenApiFactoryTest.php
Expand Up @@ -54,6 +54,7 @@
use ApiPlatform\OpenApi\Options;
use ApiPlatform\OpenApi\Tests\Fixtures\Dummy;
use ApiPlatform\OpenApi\Tests\Fixtures\DummyFilter;
use ApiPlatform\OpenApi\Tests\Fixtures\DummyWithDifferentShortName;
use ApiPlatform\OpenApi\Tests\Fixtures\OutputDto;
use ApiPlatform\State\Pagination\PaginationOptions;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -83,6 +84,7 @@ public function testInvoke(): void
'ignored' => new NotExposed(),
'ignoredWithUriTemplate' => (new NotExposed())->withUriTemplate('/dummies/{id}'),
'getDummyItem' => (new Get())->withUriTemplate('/dummies/{id}')->withOperation($baseOperation)->withUriVariables(['id' => (new Link())->withFromClass(Dummy::class)->withIdentifiers(['id'])]),
'postDummyItemFromDummyWithDifferentShortName' => (new Post())->withUriTemplate('/otherdummies/{id}/dummy')->withOperation($baseOperation)->withUriVariables(['id' => (new Link())->withFromClass(DummyWithDifferentShortName::class)->withIdentifiers(['id'])]),
'putDummyItem' => (new Put())->withUriTemplate('/dummies/{id}')->withOperation($baseOperation)->withUriVariables(['id' => (new Link())->withFromClass(Dummy::class)->withIdentifiers(['id'])]),
'deleteDummyItem' => (new Delete())->withUriTemplate('/dummies/{id}')->withOperation($baseOperation)->withUriVariables(['id' => (new Link())->withFromClass(Dummy::class)->withIdentifiers(['id'])]),
'customDummyItem' => (new HttpOperation())->withMethod('HEAD')->withUriTemplate('/foo/{id}')->withOperation($baseOperation)->withUriVariables(['id' => (new Link())->withFromClass(Dummy::class)->withIdentifiers(['id'])])->withOpenapi(new OpenApiOperation(
Expand Down Expand Up @@ -242,11 +244,18 @@ public function testInvoke(): void
])
);

$dummyWithDifferentShortNameResource = (new ApiResource())
->withShortName('DummyShortName')
->withOperations(new Operations([
(new Get())->withUriTemplate('/dummiesWithDifferentShortName/{id}')->withUriVariables(['id' => (new Link())->withFromClass(DummyWithDifferentShortName::class)->withIdentifiers(['id'])]),
]));

$resourceNameCollectionFactoryProphecy = $this->prophesize(ResourceNameCollectionFactoryInterface::class);
$resourceNameCollectionFactoryProphecy->create()->shouldBeCalled()->willReturn(new ResourceNameCollection([Dummy::class]));

$resourceCollectionMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
$resourceCollectionMetadataFactoryProphecy->create(Dummy::class)->shouldBeCalled()->willReturn(new ResourceMetadataCollection(Dummy::class, [$dummyResource]));
$resourceCollectionMetadataFactoryProphecy->create(DummyWithDifferentShortName::class)->shouldBeCalled()->willReturn(new ResourceMetadataCollection(DummyWithDifferentShortName::class, [$dummyWithDifferentShortNameResource]));

$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(Dummy::class, Argument::any())->shouldBeCalled()->willReturn(new PropertyNameCollection(['id', 'name', 'description', 'dummyDate', 'enum']));
Expand Down Expand Up @@ -927,5 +936,37 @@ public function testInvoke(): void
]),
]
), $dummyItemPath->getGet());

$dummyItemPath = $paths->getPath('/otherdummies/{id}/dummy');

$this->assertEquals(new Operation(
'postDummyItemFromDummyWithDifferentShortName',
['Dummy'],
[
'201' => new Response(
'Dummy resource created',
new \ArrayObject([
'application/ld+json' => new MediaType(new \ArrayObject(new \ArrayObject(['$ref' => '#/components/schemas/Dummy.OutputDto']))),
]),
null,
new \ArrayObject(['getDummyItem' => new Model\Link('getDummyItem', new \ArrayObject(['id' => '$response.body#/id']), null, 'This is a dummy')])
),
'400' => new Response('Invalid input'),
'422' => new Response('Unprocessable entity'),
],
'Creates a Dummy resource.',
'Creates a Dummy resource.',
null,
[
new Parameter('id', 'path', 'DummyWithDifferentShortName identifier', true, false, false, ['type' => 'string']),
],
new RequestBody(
'The new Dummy resource',
new \ArrayObject([
'application/ld+json' => new MediaType(new \ArrayObject(new \ArrayObject(['$ref' => '#/components/schemas/Dummy']))),
]),
true
)
), $dummyItemPath->getPost());
}
}
26 changes: 26 additions & 0 deletions src/OpenApi/Tests/Fixtures/DummyWithDifferentShortName.php
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\OpenApi\Tests\Fixtures;

use ApiPlatform\Metadata\ApiResource;

/**
* Dummy with a different short name.
*
* @author Priyadi Iman Nurcahyo <priyadi@rekalogika.com>
*/
#[ApiResource(shortName: 'DummyShortName')]
class DummyWithDifferentShortName
{
}

0 comments on commit 0a84f49

Please sign in to comment.