Skip to content

Commit

Permalink
fix(laravel): docs _format and open swagger ui (#6595)
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka authored Sep 6, 2024
1 parent c9f18d4 commit 3c554a6
Show file tree
Hide file tree
Showing 15 changed files with 290 additions and 124 deletions.
1 change: 0 additions & 1 deletion src/Documentation/Action/DocumentationAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ class: OpenApi::class,
);

if ('html' === $format) {
// TODO: support laravel this bounds Documentation with Symfony so it's not perfect
$operation = $operation->withProcessor('api_platform.swagger_ui.processor')->withWrite(true);
}

Expand Down
38 changes: 25 additions & 13 deletions src/Laravel/ApiPlatformProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace ApiPlatform\Laravel;

use ApiPlatform\Documentation\Action\DocumentationAction;
use ApiPlatform\Documentation\Action\EntrypointAction;
use ApiPlatform\GraphQl\Error\ErrorHandler as GraphQlErrorHandler;
use ApiPlatform\GraphQl\Error\ErrorHandlerInterface;
use ApiPlatform\GraphQl\Executor;
Expand Down Expand Up @@ -72,6 +70,8 @@
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
use ApiPlatform\Laravel\ApiResource\Error;
use ApiPlatform\Laravel\Controller\ApiPlatformController;
use ApiPlatform\Laravel\Controller\DocumentationController;
use ApiPlatform\Laravel\Controller\EntrypointController;
use ApiPlatform\Laravel\Eloquent\Extension\FilterQueryExtension;
use ApiPlatform\Laravel\Eloquent\Extension\QueryExtensionInterface;
use ApiPlatform\Laravel\Eloquent\Filter\DateFilter;
Expand Down Expand Up @@ -321,6 +321,11 @@ public function register(): void
$this->app->singleton(ResourceMetadataCollectionFactoryInterface::class, function (Application $app) {
/** @var ConfigRepository */
$config = $app['config'];
$formats = $config->get('api-platform.formats');

if ($config->get('api-platform.swagger_ui.enabled', false) && !isset($formats['html'])) {
$formats['html'] = ['text/html'];
}

return new CacheResourceCollectionMetadataFactory(
new EloquentResourceCollectionMetadataFactory(
Expand Down Expand Up @@ -361,7 +366,7 @@ public function register(): void
)
)
),
$config->get('api-platform.formats'),
$formats,
$config->get('api-platform.patch_formats'),
)
)
Expand Down Expand Up @@ -417,7 +422,10 @@ public function register(): void
});

$this->app->singleton(SwaggerUiProvider::class, function (Application $app) {
return new SwaggerUiProvider($app->make(ReadProvider::class), $app->make(OpenApiFactoryInterface::class));
/** @var ConfigRepository */
$config = $app['config'];

return new SwaggerUiProvider($app->make(ReadProvider::class), $app->make(OpenApiFactoryInterface::class), $config->get('api-platform.swagger_ui.enabled', false));
});

$this->app->singleton(ValidateProvider::class, function (Application $app) {
Expand Down Expand Up @@ -476,9 +484,14 @@ public function register(): void

$this->app->tag([RemoveProcessor::class, PersistProcessor::class], ProcessorInterface::class);
$this->app->singleton(CallableProcessor::class, function (Application $app) {
/** @var ConfigRepository */
$config = $app['config'];
$tagged = iterator_to_array($app->tagged(ProcessorInterface::class));
// TODO: tag SwaggerUiProcessor instead?
$tagged['api_platform.swagger_ui.processor'] = $app->make(SwaggerUiProcessor::class);

if ($config->get('api-platform.swagger_ui.enabled', false)) {
// TODO: tag SwaggerUiProcessor instead?
$tagged['api_platform.swagger_ui.processor'] = $app->make(SwaggerUiProcessor::class);
}

return new CallableProcessor(new ServiceLocator($tagged));
});
Expand Down Expand Up @@ -628,18 +641,18 @@ public function register(): void
return new Options(title: $config->get('api-platform.title') ?? '');
});

$this->app->singleton(DocumentationAction::class, function (Application $app) {
$this->app->singleton(DocumentationController::class, function (Application $app) {
/** @var ConfigRepository */
$config = $app['config'];

return new DocumentationAction($app->make(ResourceNameCollectionFactoryInterface::class), $config->get('api-platform.title') ?? '', $config->get('api-platform.description') ?? '', $config->get('api-platform.version') ?? '', $app->make(OpenApiFactoryInterface::class), $app->make(ProviderInterface::class), $app->make(ProcessorInterface::class), $app->make(Negotiator::class), $config->get('api-platform.docs_formats'));
return new DocumentationController($app->make(ResourceNameCollectionFactoryInterface::class), $config->get('api-platform.title') ?? '', $config->get('api-platform.description') ?? '', $config->get('api-platform.version') ?? '', $app->make(OpenApiFactoryInterface::class), $app->make(ProviderInterface::class), $app->make(ProcessorInterface::class), $app->make(Negotiator::class), $config->get('api-platform.docs_formats'), $config->get('api-platform.swagger_ui.enabled', false));
});

$this->app->singleton(EntrypointAction::class, function (Application $app) {
$this->app->singleton(EntrypointController::class, function (Application $app) {
/** @var ConfigRepository */
$config = $app['config'];

return new EntrypointAction($app->make(ResourceNameCollectionFactoryInterface::class), $app->make(ProviderInterface::class), $app->make(ProcessorInterface::class), $config->get('api-platform.docs_formats'));
return new EntrypointController($app->make(ResourceNameCollectionFactoryInterface::class), $app->make(ProviderInterface::class), $app->make(ProcessorInterface::class), $config->get('api-platform.docs_formats'));
});

$this->app->singleton(Pagination::class, function (Application $app) {
Expand Down Expand Up @@ -1144,17 +1157,16 @@ public function boot(ResourceNameCollectionFactoryInterface $resourceNameCollect
$route = new Route(['GET'], $prefix.'/contexts/{shortName?}{_format?}', [ContextAction::class, '__invoke']);
$route->name('api_jsonld_context')->middleware(ApiPlatformMiddleware::class);
$routeCollection->add($route);
// Maybe that we can alias Symfony Request to Laravel Request within the provider ?
$route = new Route(['GET'], $prefix.'/docs{_format?}', function (Request $request, Application $app) {
$documentationAction = $app->make(DocumentationAction::class);
$documentationAction = $app->make(DocumentationController::class);

return $documentationAction->__invoke($request);
});
$route->name('api_doc')->middleware(ApiPlatformMiddleware::class);
$routeCollection->add($route);

$route = new Route(['GET'], $prefix.'/{index?}{_format?}', function (Request $request, Application $app) {
$entrypointAction = $app->make(EntrypointAction::class);
$entrypointAction = $app->make(EntrypointController::class);

return $entrypointAction->__invoke($request);
});
Expand Down
122 changes: 122 additions & 0 deletions src/Laravel/Controller/DocumentationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?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\Laravel\Controller;

use ApiPlatform\Documentation\Documentation;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Util\ContentNegotiationTrait;
use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
use ApiPlatform\OpenApi\OpenApi;
use ApiPlatform\OpenApi\Serializer\ApiGatewayNormalizer;
use ApiPlatform\OpenApi\Serializer\LegacyOpenApiNormalizer;
use ApiPlatform\OpenApi\Serializer\OpenApiNormalizer;
use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\State\ProviderInterface;
use Negotiation\Negotiator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* Generates the API documentation.
*
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
*/
final class DocumentationController
{
use ContentNegotiationTrait;

/**
* @param array<string, string[]> $documentationFormats
* @param ProviderInterface<object> $provider
* @param ProcessorInterface<mixed, Response> $processor
*/
public function __construct(
private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory,
private readonly string $title = '',
private readonly string $description = '',
private readonly string $version = '',
private readonly ?OpenApiFactoryInterface $openApiFactory = null,
private readonly ?ProviderInterface $provider = null,
private readonly ?ProcessorInterface $processor = null,
?Negotiator $negotiator = null,
private readonly array $documentationFormats = [OpenApiNormalizer::JSON_FORMAT => ['application/vnd.openapi+json'], OpenApiNormalizer::FORMAT => ['application/json']],
private readonly bool $swaggerUiEnabled = true,
) {
$this->negotiator = $negotiator ?? new Negotiator();
}

public function __invoke(Request $request): Response
{
$context = [
'api_gateway' => $request->query->getBoolean(ApiGatewayNormalizer::API_GATEWAY),
'base_url' => $request->getBaseUrl(),
'spec_version' => (string) $request->query->get(LegacyOpenApiNormalizer::SPEC_VERSION),
];
$request->attributes->set('_api_normalization_context', $request->attributes->get('_api_normalization_context', []) + $context);
// We want to find the format early on, this code is also executed later on by the ContentNegotiationProvider.
$this->addRequestFormats($request, $this->documentationFormats);
$format = $this->getRequestFormat($request, $this->documentationFormats);

if ('html' === $format || OpenApiNormalizer::FORMAT === $format || OpenApiNormalizer::JSON_FORMAT === $format || OpenApiNormalizer::YAML_FORMAT === $format) {
return $this->getOpenApiDocumentation($context, $format, $request);
}

return $this->getHydraDocumentation($context, $request);
}

/**
* @param array<string,mixed> $context
*/
private function getOpenApiDocumentation(array $context, string $format, Request $request): Response
{
$context['request'] = $request;
$operation = new Get(
class: OpenApi::class,
read: true,
serialize: true,
provider: fn () => $this->openApiFactory->__invoke($context),
normalizationContext: [
ApiGatewayNormalizer::API_GATEWAY => $context['api_gateway'] ?? null,
LegacyOpenApiNormalizer::SPEC_VERSION => $context['spec_version'] ?? null,
],
outputFormats: $this->documentationFormats
);

if ('html' === $format && $this->swaggerUiEnabled) {
$operation = $operation->withProcessor('api_platform.swagger_ui.processor')->withWrite(true);
}

return $this->processor->process($this->provider->provide($operation, [], $context), $operation, [], $context);
}

/**
* TODO: the logic behind the Hydra Documentation is done in a ApiPlatform\Hydra\Serializer\DocumentationNormalizer.
* We should transform this to a provider, it'd improve performances also by a bit.
*
* @param array<string,mixed> $context
*/
private function getHydraDocumentation(array $context, Request $request): Response
{
$context['request'] = $request;
$operation = new Get(
class: Documentation::class,
read: true,
serialize: true,
provider: fn () => new Documentation($this->resourceNameCollectionFactory->create(), $this->title, $this->description, $this->version)
);

return $this->processor->process($this->provider->provide($operation, [], $context), $operation, [], $context);
}
}
74 changes: 74 additions & 0 deletions src/Laravel/Controller/EntrypointController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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\Laravel\Controller;

use ApiPlatform\Documentation\Entrypoint;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\ResourceNameCollection;
use ApiPlatform\OpenApi\Serializer\LegacyOpenApiNormalizer;
use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\State\ProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* Generates the API entrypoint.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
final class EntrypointController
{
private static ResourceNameCollection $resourceNameCollection;

/**
* @param array<string, string[]> $documentationFormats
* @param ProviderInterface<object> $provider
* @param ProcessorInterface<mixed, Response> $processor
*/
public function __construct(
private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory,
private readonly ProviderInterface $provider,
private readonly ProcessorInterface $processor,
private readonly array $documentationFormats = [],
) {
}

public function __invoke(Request $request): Response
{
self::$resourceNameCollection = $this->resourceNameCollectionFactory->create();
$context = [
'request' => $request,
'spec_version' => (string) $request->query->get(LegacyOpenApiNormalizer::SPEC_VERSION),
];
$request->attributes->set('_api_platform_disable_listeners', true);
$operation = new Get(
outputFormats: $this->documentationFormats,
read: true,
serialize: true,
class: Entrypoint::class,
provider: [self::class, 'provide']
);
$request->attributes->set('_api_operation', $operation);
$body = $this->provider->provide($operation, [], $context);
$operation = $request->attributes->get('_api_operation');

return $this->processor->process($body, $operation, [], $context);
}

public static function provide(): Entrypoint
{
return new Entrypoint(self::$resourceNameCollection);
}
}
2 changes: 2 additions & 0 deletions src/Laravel/State/SwaggerUiProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ final class SwaggerUiProvider implements ProviderInterface
public function __construct(
private readonly ProviderInterface $decorated,
private readonly OpenApiFactoryInterface $openApiFactory,
private readonly bool $swaggerUiEnabled = true,
) {
}

Expand All @@ -51,6 +52,7 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
!($operation instanceof HttpOperation)
|| !($request = $context['request'] ?? null)
|| 'html' !== $request->getRequestFormat()
|| !$this->swaggerUiEnabled
) {
return $this->decorated->provide($operation, $uriVariables, $context);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Laravel/Tests/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testAuthenticatedPolicy(): void
{
$response = $this->post('/tokens/create');
$token = $response->json()['token'];
$response = $this->post('/api/vaults', [], ['content-type' => ['application/ld+json'], 'authorization' => 'Bearer '.$token]);
$response = $this->post('/api/vaults', [], ['accept' => ['application/ld+json'], 'content-type' => ['application/ld+json'], 'authorization' => 'Bearer '.$token]);
$response->assertStatus(403);
}
}
52 changes: 52 additions & 0 deletions src/Laravel/Tests/DocsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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\Laravel\Tests;

use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;

class DocsTest extends TestCase
{
use ApiTestAssertionsTrait;
use WithWorkbench;

public function testOpenApi(): void
{
$res = $this->get('/api/docs.jsonopenapi');
$this->assertArrayHasKey('openapi', $res->json());
$this->assertSame('application/vnd.openapi+json; charset=utf-8', $res->headers->get('content-type'));
}

public function testOpenApiAccept(): void
{
$res = $this->get('/api/docs', headers: ['accept' => 'application/vnd.openapi+json']);
$this->assertArrayHasKey('openapi', $res->json());
$this->assertSame('application/vnd.openapi+json; charset=utf-8', $res->headers->get('content-type'));
}

public function testJsonLd(): void
{
$res = $this->get('/api/docs.jsonld');
$this->assertArrayHasKey('@context', $res->json());
$this->assertSame('application/ld+json; charset=utf-8', $res->headers->get('content-type'));
}

public function testJsonLdAccept(): void
{
$res = $this->get('/api/docs', headers: ['accept' => 'application/ld+json']);
$this->assertArrayHasKey('@context', $res->json());
$this->assertSame('application/ld+json; charset=utf-8', $res->headers->get('content-type'));
}
}
Loading

0 comments on commit 3c554a6

Please sign in to comment.