Skip to content

Commit 91dc2a4

Browse files
Simperfitdunglas
authored andcommitted
feat: Refactor Documentation and Entrypoint support (#628)
1 parent 5e5f477 commit 91dc2a4

36 files changed

+313
-217
lines changed

features/custom_identifier.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Feature: Using custom identifier on resource
8282
"""
8383

8484
Scenario: API doc is correctly generated
85-
When I send a "GET" request to "/apidoc"
85+
When I send a "GET" request to "/apidoc.jsonld"
8686
Then the response status code should be 200
8787
And the response should be in JSON
8888
And the hydra class "CustomIdentifierDummy" exist

features/custom_normalized.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Feature: Using custom normalized entity
8787
"""
8888

8989
Scenario: API doc is correctly generated
90-
When I send a "GET" request to "/apidoc"
90+
When I send a "GET" request to "/apidoc.jsonld"
9191
Then the response status code should be 200
9292
And the response should be in JSON
9393
And the hydra class "CustomNormalizedDummy" exist

features/custom_writable_identifier.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Feature: Using custom writable identifier on resource
8484
"""
8585

8686
Scenario: API doc is correctly generated
87-
When I send a "GET" request to "/apidoc"
87+
When I send a "GET" request to "/apidoc.jsonld"
8888
Then the response status code should be 200
8989
And the response should be in JSON
9090
And the hydra class "CustomWritableIdentifierDummy" exist

features/hydra/doc.feature

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ Feature: Documentation support
55

66
Scenario: Checks that the Link pointing to the Hydra documentation is set
77
Given I send a "GET" request to "/"
8-
Then the header "Link" should be equal to '<http://example.com/apidoc>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"'
8+
Then the header "Link" should be equal to '<http://example.com/apidoc.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"'
99

1010
Scenario: Retrieve the API vocabulary
11-
Given I send a "GET" request to "/apidoc"
11+
Given I send a "GET" request to "/apidoc.jsonld"
1212
Then the response status code should be 200
1313
And the response should be in JSON
1414
And the header "Content-Type" should be equal to "application/ld+json"
1515
# Context
16-
And the JSON node "@context.@vocab" should be equal to "http://example.com/apidoc#"
16+
And the JSON node "@context.@vocab" should be equal to "http://example.com/apidoc.jsonld#"
1717
And the JSON node "@context.hydra" should be equal to "http://www.w3.org/ns/hydra/core#"
1818
And the JSON node "@context.rdf" should be equal to "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
1919
And the JSON node "@context.rdfs" should be equal to "http://www.w3.org/2000/01/rdf-schema#"
@@ -30,7 +30,7 @@ Feature: Documentation support
3030
And the JSON node "@context.returns.@id" should be equal to "hydra:returns"
3131
And the JSON node "@context.returns.@type" should be equal to "@id"
3232
# Root properties
33-
And the JSON node "@id" should be equal to "/apidoc"
33+
And the JSON node "@id" should be equal to "/apidoc.jsonld"
3434
And the JSON node "hydra:title" should be equal to "My Dummy API"
3535
And the JSON node "hydra:description" should be equal to "This is a test API."
3636
And the JSON node "hydra:entrypoint" should be equal to "/"

features/jsonld/context.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Feature: JSON-LD contexts generation
3636
"""
3737
{
3838
"@context": {
39-
"@vocab": "http://example.com/apidoc#",
39+
"@vocab": "http://example.com/apidoc.jsonld#",
4040
"hydra": "http://www.w3.org/ns/hydra/core#",
4141
"description": "https://schema.org/description",
4242
"dummy": "#Dummy/dummy",
@@ -69,7 +69,7 @@ Feature: JSON-LD contexts generation
6969
"""
7070
{
7171
"@context": {
72-
"@vocab": "http://example.com/apidoc#",
72+
"@vocab": "http://example.com/apidoc.jsonld#",
7373
"hydra": "http://www.w3.org/ns/hydra/core#",
7474
"paris": "#RelationEmbedder/paris",
7575
"krondstadt": "#RelationEmbedder/krondstadt",

features/swagger/doc.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Feature: Documentation support
44
I need to know Swagger specifications of objects I send and receive
55

66
Scenario: Retrieve the API vocabulary
7-
Given I send a "GET" request to "/swagger"
7+
Given I send a "GET" request to "/apidoc.json"
88
Then the response status code should be 200
99
And the response should be in JSON
1010
And the header "Content-Type" should be equal to "application/json"

src/Action/EntrypointAction.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
namespace ApiPlatform\Core\Action;
1313

14+
use ApiPlatform\Core\Api\Entrypoint;
1415
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
15-
use ApiPlatform\Core\Metadata\Resource\ResourceNameCollection;
1616

1717
/**
1818
* Generates the API entrypoint.
@@ -23,13 +23,13 @@ final class EntrypointAction
2323
{
2424
private $resourceNameCollectionFactory;
2525

26-
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollection)
26+
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory)
2727
{
28-
$this->resourceNameCollectionFactory = $resourceNameCollection;
28+
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
2929
}
3030

31-
public function __invoke() : ResourceNameCollection
31+
public function __invoke() : Entrypoint
3232
{
33-
return $this->resourceNameCollectionFactory->create();
33+
return new Entrypoint($this->resourceNameCollectionFactory->create());
3434
}
3535
}

src/Api/Entrypoint.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ApiPlatform\Core\Api;
13+
14+
use ApiPlatform\Core\Metadata\Resource\ResourceNameCollection;
15+
16+
/**
17+
* The first path you will see in the API.
18+
*
19+
* @author Amrouche Hamza <[email protected]>
20+
*/
21+
final class Entrypoint
22+
{
23+
private $resourceNameCollection;
24+
25+
public function __construct(ResourceNameCollection $resourceNameCollection)
26+
{
27+
$this->resourceNameCollection = $resourceNameCollection;
28+
}
29+
30+
public function getResourceNameCollection(): ResourceNameCollection
31+
{
32+
return $this->resourceNameCollection;
33+
}
34+
}

src/Bridge/NelmioApiDoc/Extractor/AnnotationsProvider/ApiPlatformProvider.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
use ApiPlatform\Core\Api\FilterCollection;
1515
use ApiPlatform\Core\Bridge\NelmioApiDoc\Parser\ApiPlatformParser;
1616
use ApiPlatform\Core\Bridge\Symfony\Routing\OperationMethodResolverInterface;
17-
use ApiPlatform\Core\Documentation\ApiDocumentationBuilderInterface;
1817
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
1918
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
2019
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
2120
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
2221
use Nelmio\ApiDocBundle\Extractor\AnnotationsProviderInterface;
2322
use Symfony\Component\HttpFoundation\Request;
23+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2424

2525
/**
2626
* Creates Nelmio ApiDoc annotations for the api platform.
@@ -31,15 +31,15 @@
3131
final class ApiPlatformProvider implements AnnotationsProviderInterface
3232
{
3333
private $resourceNameCollectionFactory;
34-
private $apiDocumentationBuilder;
34+
private $documentationNormalizer;
3535
private $resourceMetadataFactory;
3636
private $filters;
3737
private $operationMethodResolver;
3838

39-
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ApiDocumentationBuilderInterface $apiDocumentationBuilder, ResourceMetadataFactoryInterface $resourceMetadataFactory, FilterCollection $filters, OperationMethodResolverInterface $operationMethodResolver)
39+
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, NormalizerInterface $documentationNormalizer, ResourceMetadataFactoryInterface $resourceMetadataFactory, FilterCollection $filters, OperationMethodResolverInterface $operationMethodResolver)
4040
{
4141
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
42-
$this->apiDocumentationBuilder = $apiDocumentationBuilder;
42+
$this->documentationNormalizer = $documentationNormalizer;
4343
$this->resourceMetadataFactory = $resourceMetadataFactory;
4444
$this->filters = $filters;
4545
$this->operationMethodResolver = $operationMethodResolver;
@@ -51,7 +51,7 @@ public function __construct(ResourceNameCollectionFactoryInterface $resourceName
5151
public function getAnnotations() : array
5252
{
5353
$annotations = [];
54-
$hydraDoc = $this->apiDocumentationBuilder->getApiDocumentation();
54+
$hydraDoc = $this->documentationNormalizer->normalize($this->resourceNameCollectionFactory->create());
5555
$entrypointHydraDoc = $this->getResourceHydraDoc($hydraDoc, '#Entrypoint');
5656

5757
foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {

src/Bridge/Symfony/Bundle/Command/SwaggerCommand.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Command;
1313

14-
use ApiPlatform\Core\Swagger\ApiDocumentationBuilder;
14+
use ApiPlatform\Core\Documentation\Documentation;
15+
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
16+
use ApiPlatform\Core\Swagger\DocumentationNormalizer;
1517
use Symfony\Component\Console\Command\Command;
1618
use Symfony\Component\Console\Input\InputInterface;
1719
use Symfony\Component\Console\Output\OutputInterface;
@@ -23,12 +25,22 @@
2325
*/
2426
final class SwaggerCommand extends Command
2527
{
26-
private $apiDocumentationBuilder;
28+
private $documentationNormalizer;
29+
private $resourceNameCollectionFactory;
30+
private $title;
31+
private $description;
32+
private $version;
33+
private $formats;
2734

28-
public function __construct(ApiDocumentationBuilder $apiDocumentationBuilder)
35+
public function __construct(DocumentationNormalizer $documentationNormalizer, ResourceNameCollectionFactoryInterface $resourceNameCollection, string $title, string $description, string $version, array $formats)
2936
{
3037
parent::__construct();
31-
$this->apiDocumentationBuilder = $apiDocumentationBuilder;
38+
$this->documentationNormalizer = $documentationNormalizer;
39+
$this->resourceNameCollectionFactory = $resourceNameCollection;
40+
$this->title = $title;
41+
$this->description = $description;
42+
$this->version = $version;
43+
$this->formats = $formats;
3244
}
3345

3446
/**
@@ -46,7 +58,8 @@ protected function configure()
4658
*/
4759
protected function execute(InputInterface $input, OutputInterface $output)
4860
{
49-
$data = $this->apiDocumentationBuilder->getApiDocumentation();
61+
$documentation = new Documentation($this->resourceNameCollectionFactory->create(), $this->title, $this->description, $this->version, $this->formats);
62+
$data = $this->documentationNormalizer->normalize($documentation);
5063
$content = json_encode($data, JSON_PRETTY_PRINT);
5164
$output->writeln($content);
5265
}

0 commit comments

Comments
 (0)