Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swagger Path Identifier to reflect the annotation of ApiProperty(identifier=true) #1367

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/Swagger/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
/**
* Creates a machine readable Swagger API documentation.
*
* @author Philippe Guilbault <philippe.guilbault@gmail.com>
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
* @author Teoh Han Hui <teohhanhui@gmail.com>
* @author Kévin Dunglas <dunglas@gmail.com>
Expand Down Expand Up @@ -302,12 +303,22 @@ private function updateGetOperation(\ArrayObject $pathOperation, array $mimeType
return $pathOperation;
}

$pathIdentifier = 'id';
$pathType = 'string';
foreach ($this->propertyNameCollectionFactory->create($resourceClass, []) as $propertyName) {
$property = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
if ($property->isIdentifier()) {
$pathIdentifier = $propertyName;
$pathType = $property->getType()->getBuiltinType();
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have an issue on composite identifiers here. Indeed, a composite identifier would be written as a string (for example foo=1;bar=2). With this code, the identifier would only be bar./
About the id name, I think it's like this so that it's being used in the path from Swagger. I'm not sure how Swagger is identifying the url parameter like this.

You could maybe reuse the same code from a new method instead of repeating it.


$pathOperation['summary'] ?? $pathOperation['summary'] = sprintf('Retrieves a %s resource.', $resourceShortName);
$pathOperation['parameters'] ?? $pathOperation['parameters'] = [[
'name' => 'id',
'name' => $pathIdentifier,
'in' => 'path',
'required' => true,
'type' => 'string',
'type' => $pathType,
]];
$pathOperation['responses'] ?? $pathOperation['responses'] = [
'200' => [
Expand Down Expand Up @@ -373,14 +384,24 @@ private function updatePostOperation(\ArrayObject $pathOperation, array $mimeTyp
*/
private function updatePutOperation(\ArrayObject $pathOperation, array $mimeTypes, string $operationType, ResourceMetadata $resourceMetadata, string $resourceClass, string $resourceShortName, string $operationName, \ArrayObject $definitions)
{
$pathIdentifier = 'id';
$pathType = 'string';
foreach ($this->propertyNameCollectionFactory->create($resourceClass, []) as $propertyName) {
$property = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
if ($property->isIdentifier()) {
$pathIdentifier = $propertyName;
$pathType = $property->getType()->getBuiltinType();
}
}

$pathOperation['consumes'] ?? $pathOperation['consumes'] = $mimeTypes;
$pathOperation['produces'] ?? $pathOperation['produces'] = $mimeTypes;
$pathOperation['summary'] ?? $pathOperation['summary'] = sprintf('Replaces the %s resource.', $resourceShortName);
$pathOperation['parameters'] ?? $pathOperation['parameters'] = [
[
'name' => 'id',
'name' => $pathIdentifier,
'in' => 'path',
'type' => 'string',
'type' => $pathType,
'required' => true,
],
[
Expand Down
126 changes: 126 additions & 0 deletions tests/Swagger/Serializer/DocumentationNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;

/**
* @author Philippe Guilbault <philippe.guilbault@gmail.com>
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
* @author Kévin Dunglas <dunglas@gmail.com>
*/
Expand Down Expand Up @@ -1823,4 +1824,129 @@ public function testNormalizeWithSubResource()

$this->assertEquals($expected, $normalizer->normalize($documentation));
}

/**
*
* @group yourmom
*/
public function testNormalizeWithIdentifierName()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a test with composite identifiers?

{
$documentation = new Documentation(new ResourceNameCollection([Dummy::class]), '', '', '0.0.0', ['jsonld' => ['application/ld+json']]);

$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->shouldBeCalled()->willReturn(new PropertyNameCollection(['id', 'name', 'description']));

$dummyMetadata = new ResourceMetadata(
'Dummy',
'This is a dummy.',
'http://schema.example.com/Dummy',
['get' => ['method' => 'GET']],
[],
[]
);
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create(Dummy::class)->shouldBeCalled()->willReturn($dummyMetadata);

$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create(Dummy::class, 'id')->shouldBeCalled()->willReturn(new PropertyMetadata(
new Type(Type::BUILTIN_TYPE_INT),
'This is an id.',
true,
false,
null,
null,
false
));
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name')->shouldBeCalled()->willReturn(new PropertyMetadata(
new Type(Type::BUILTIN_TYPE_STRING),
'This is a name.',
true,
false,
null,
null,
true,
true
));
$propertyMetadataFactoryProphecy->create(Dummy::class, 'description')->shouldBeCalled()->willReturn(new PropertyMetadata(
new Type(Type::BUILTIN_TYPE_STRING),
'This is a description.',
true,
true
));

$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);

$operationMethodResolverProphecy = $this->prophesize(OperationMethodResolverInterface::class);
$operationMethodResolverProphecy->getItemOperationMethod(Dummy::class, 'get')->shouldBeCalled()->willReturn('GET');

$urlGeneratorProphecy = $this->prophesize(UrlGeneratorInterface::class);
//$urlGeneratorProphecy->generate('api_entrypoint')->willReturn('/')->shouldBeCalled();

$operationPathResolver = new CustomOperationPathResolver(new UnderscoreOperationPathResolver());

$normalizer = new DocumentationNormalizer(
$resourceMetadataFactoryProphecy->reveal(),
$propertyNameCollectionFactoryProphecy->reveal(),
$propertyMetadataFactoryProphecy->reveal(),
$resourceClassResolverProphecy->reveal(),
$operationMethodResolverProphecy->reveal(),
$operationPathResolver,
$urlGeneratorProphecy->reveal()
);

$expected = array (
'swagger' => '2.0',
'basePath' => '/',
'info' => ['title' => '', 'version' => '0.0.0', ],
'paths' => new \ArrayObject([
'/dummies/{id}' => [
'get' => new \ArrayObject([
'tags' => ['Dummy'],
'operationId' => 'getDummyItem',
'produces' => ['application/ld+json'],
'summary' => 'Retrieves a Dummy resource.',
'parameters' => [[
'name' => 'name',
'type' => 'string',
'in' => 'path',
'required' => true,
]],
'responses' => [
200 => [
'description' => 'Dummy resource response',
'schema' => ['$ref' => '#/definitions/Dummy'],
],
404 => ['description' => 'Resource not found'],
],
]),
],
]),
'definitions' => new \ArrayObject([
'Dummy' => new \ArrayObject([
'type' => 'object',
'description' => 'This is a dummy.',
'externalDocs' => ['url' => 'http://schema.example.com/Dummy'],
'properties' => [
'id' => new \ArrayObject([
'readOnly' => true,
'description' => 'This is an id.',
'type' => 'integer',
]),
'name' => new \ArrayObject([
'readOnly' => true,
'description' => 'This is a name.',
'type' => 'string',
]),
'description' => new \ArrayObject([
'description' => 'This is a description.',
'type' => 'string',
]),
],
]),
]),
);

$this->assertEquals($expected, $normalizer->normalize($documentation));
}
}