Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion features/swagger/doc.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Feature: Documentation support
# Root properties
And the JSON node "info.title" should be equal to "My Dummy API"
And the JSON node "info.description" should be equal to "This is a test API."
#And the JSON node "host" should be equal to "exemple.com"
And the JSON node "basePath" should be equal to "/"
# Supported classes
And the Swagger class "CircularReference" exist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function load(array $configs, ContainerBuilder $container)

$container->setParameter('api_platform.title', $config['title']);
$container->setParameter('api_platform.description', $config['description']);
$container->setParameter('api_platform.version', $config['version']);
$container->setParameter('api_platform.formats', $formats);
$container->setParameter('api_platform.collection.order', $config['collection']['order']);
$container->setParameter('api_platform.collection.order_parameter_name', $config['collection']['order_parameter_name']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function getConfigTreeBuilder()
->children()
->scalarNode('title')->defaultValue('')->info('The title of the API.')->end()
->scalarNode('description')->defaultValue('')->info('The description of the API.')->end()
->scalarNode('version')->defaultValue('0.0.0')->info('The version of the API.')->end()
->arrayNode('formats')
->defaultValue(['jsonld' => ['mime_types' => ['application/ld+json']]])
->info('The list of enabled formats. The first one will be the default.')
Expand Down
5 changes: 2 additions & 3 deletions src/Bridge/Symfony/Bundle/Resources/config/swagger.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
<argument type="service" id="api_platform.operation_method_resolver" />
<argument type="service" id="api_platform.router" />
<argument type="service" id="api_platform.iri_converter" />
<argument>%api_platform.formats%</argument>
<argument>%api_platform.title%</argument>
<argument>%api_platform.description%</argument>
<argument>2.0</argument>
<argument>%router.request_context.host%</argument>
<argument>%router.request_context.scheme%</argument>
<argument>%api_platform.version%</argument>
</service>

<service id="api_platform.swagger.command.swagger_command" class="ApiPlatform\Core\Bridge\Symfony\Bundle\Command\SwaggerCommand">
Expand Down
170 changes: 96 additions & 74 deletions src/Swagger/ApiDocumentationBuilder.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ApiPlatformExtensionTest extends \PHPUnit_Framework_TestCase
'api_platform' => [
'title' => 'title',
'description' => 'description',
'version' => 'version',
],
];

Expand Down Expand Up @@ -172,6 +173,7 @@ private function getContainerBuilderProphecy()
$parameters = [
'api_platform.title' => 'title',
'api_platform.description' => 'description',
'api_platform.version' => 'version',
'api_platform.formats' => ['application/ld+json' => 'jsonld'],
'api_platform.collection.order' => null,
'api_platform.collection.order_parameter_name' => 'order',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ public function testDefaultConfig()
$configuration = new Configuration();
$treeBuilder = $configuration->getConfigTreeBuilder();
$processor = new Processor();
$config = $processor->processConfiguration($configuration, ['api_platform' => ['title' => 'title', 'description' => 'description']]);
$config = $processor->processConfiguration($configuration, ['api_platform' => ['title' => 'title', 'description' => 'description', 'version' => '1.0.0']]);

$this->assertInstanceOf(ConfigurationInterface::class, $configuration);
$this->assertInstanceOf(TreeBuilder::class, $treeBuilder);
$this->assertEquals([
'title' => 'title',
'description' => 'description',
'version' => '1.0.0',
'formats' => ['jsonld' => ['mime_types' => ['application/ld+json']]],
'naming' => [
'resource_path_naming_strategy' => 'api_platform.naming.resource_path_naming_strategy.underscore',
Expand Down
98 changes: 98 additions & 0 deletions tests/Swagger/ApiDocumentationBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?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.
*/

namespace ApiPlatform\Core\Tests\Swagger;

use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Core\Api\OperationMethodResolverInterface;
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
use ApiPlatform\Core\Api\UrlGeneratorInterface;
use ApiPlatform\Core\JsonLd\ContextBuilderInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
use ApiPlatform\Core\Metadata\Property\PropertyNameCollection;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use ApiPlatform\Core\Metadata\Resource\ResourceNameCollection;
use ApiPlatform\Core\Swagger\ApiDocumentationBuilder;
use Symfony\Component\PropertyInfo\Type;

/**
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
*/
class ApiDocumentationBuilderTest extends \PHPUnit_Framework_TestCase /**/
{
public function testGetApiDocumention()
{
$resourceNameCollectionFactoryProphecy = $this->prophesize(ResourceNameCollectionFactoryInterface::class);
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$contextBuilderProphecy = $this->prophesize(ContextBuilderInterface::class);
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$operationMethodResolverProphecy = $this->prophesize(OperationMethodResolverInterface::class);
$urlGeneratorProphecy = $this->prophesize(UrlGeneratorInterface::class);
$titre = 'Test Api';
$desc = 'test ApiGerard';
$iriConverter = $this->prophesize(IriConverterInterface::class);
$version = '1.0.0';
$host = 'http://exemple.com';
$schema = 'http';
$formats = ['application/ld+json' => 'ld+json'];
$dummyMetadata = new ResourceMetadata('dummy', 'dummy', '#dummy', ['get' => ['method' => 'GET'], 'put' => ['method' => 'PUT']], ['get' => ['method' => 'GET'], 'post' => ['method' => 'POST']], []);
$resourceNameCollectionFactoryProphecy->create()->willReturn(new ResourceNameCollection(['dummy' => 'dummy']))->shouldBeCalled();
$resourceMetadataFactoryProphecy->create('dummy')->shouldBeCalled()->willReturn($dummyMetadata);
$propertyNameCollectionFactoryProphecy->create('dummy', [])->shouldBeCalled()->willReturn(new PropertyNameCollection(['name']));
$propertyMetadataFactoryProphecy->create('dummy', 'name')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'name', true, true, true, true, false, false, null, []));
$operationMethodResolverProphecy->getItemOperationMethod('dummy', 'get')->shouldBeCalled()->willReturn('get');
$operationMethodResolverProphecy->getItemOperationMethod('dummy', 'put')->shouldBeCalled()->willReturn('put');
$operationMethodResolverProphecy->getCollectionOperationMethod('dummy', 'get')->shouldBeCalled()->willReturn('get');
$operationMethodResolverProphecy->getCollectionOperationMethod('dummy', 'post')->shouldBeCalled()->willReturn('post');
$iriConverter->getIriFromResourceClass('dummy')->shouldBeCalled()->willReturn('/dummies');
$apiDocumentationBuilder = new ApiDocumentationBuilder($resourceNameCollectionFactoryProphecy->reveal(), $resourceMetadataFactoryProphecy->reveal(), $propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $contextBuilderProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $operationMethodResolverProphecy->reveal(), $urlGeneratorProphecy->reveal(), $iriConverter->reveal(), $formats, $titre, $desc);

$swaggerDocumentation = $apiDocumentationBuilder->getApiDocumentation();
$this->assertEquals($swaggerDocumentation['swagger'], 2.0);
$this->assertEquals($swaggerDocumentation['info']['title'], $titre);
$this->assertEquals($swaggerDocumentation['info']['description'], $desc);
$this->assertEquals($swaggerDocumentation['definitions'], ['dummy' => ['type' => 'object', 'properties' => ['name' => ['type' => 'string']]]]);
$this->assertEquals($swaggerDocumentation['externalDocs'
], ['description' => 'Find more about API Platform', 'url' => 'https://api-platform.com']);

$this->assertEquals($swaggerDocumentation['paths']['/dummies']['get'], [
'tags' => [0 => 'dummy'],
'produces' => ['application/ld+json'],
'consumes' => ['application/ld+json'],
]
);

$this->assertEquals($swaggerDocumentation['paths']['/dummies']['post'], [
'tags' => [0 => 'dummy'],
'produces' => ['application/ld+json'],
'consumes' => ['application/ld+json'],
]
);
$this->assertEquals($swaggerDocumentation['paths']['/dummies/{id}']['get'], [
'tags' => [0 => 'dummy'],
'produces' => ['application/ld+json'],
'consumes' => ['application/ld+json'],
]
);
$this->assertEquals($swaggerDocumentation['paths']['/dummies/{id}']['put'], [
'tags' => [0 => 'dummy'],
'produces' => ['application/ld+json'],
'consumes' => ['application/ld+json'],
]
);
}
}