-
-
Notifications
You must be signed in to change notification settings - Fork 892
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(state): improvements and fixes for LinkedDataPlatform support
- use LinkedDataPlatformProcessor instead of RespondProcessor to handle the allow and allow-post headers - add new Processor in symfony events and Laravel
- Loading branch information
1 parent
42d61d3
commit dea6352
Showing
11 changed files
with
285 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait; | ||
use Illuminate\Contracts\Config\Repository; | ||
use Illuminate\Foundation\Application; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Orchestra\Testbench\Concerns\WithWorkbench; | ||
use Orchestra\Testbench\TestCase; | ||
use Workbench\App\Models\Book; | ||
use Workbench\Database\Factories\BookFactory; | ||
|
||
class LinkedDataPlatformTest extends TestCase | ||
{ | ||
use ApiTestAssertionsTrait; | ||
use RefreshDatabase; | ||
use WithWorkbench; | ||
|
||
/** | ||
* @param Application $app | ||
*/ | ||
protected function defineEnvironment($app): void | ||
{ | ||
tap($app['config'], function (Repository $config): void { | ||
$config->set('api-platform.formats', ['jsonld' => ['application/ld+json'], 'turtle' => ['text/turtle']]); | ||
$config->set('api-platform.resources', [app_path('Models'), app_path('ApiResource')]); | ||
$config->set('app.debug', true); | ||
}); | ||
} | ||
|
||
public function testHeadersAcceptPostIsReturnWhenPostAllowed(): void | ||
{ | ||
$response = $this->get('/api/books', ['accept' => ['application/ld+json']]); | ||
$response->assertStatus(200); | ||
$response->assertHeader('accept-post', 'application/ld+json, text/turtle, text/html'); | ||
} | ||
|
||
public function testHeadersAcceptPostIsNotSetWhenPostIsNotAllowed(): void | ||
{ | ||
BookFactory::new()->createOne(); | ||
$book = Book::first(); | ||
$response = $this->get($this->getIriFromResource($book), ['accept' => ['application/ld+json']]); | ||
$response->assertStatus(200); | ||
$response->assertHeaderMissing('accept-post'); | ||
} | ||
|
||
public function testHeaderAllowReflectsResourceAllowedMethods(): void | ||
{ | ||
$response = $this->get('/api/books', ['accept' => ['application/ld+json']]); | ||
$response->assertHeader('allow', 'OPTIONS, HEAD, POST, GET'); | ||
|
||
BookFactory::new()->createOne(); | ||
$book = Book::first(); | ||
$response = $this->get($this->getIriFromResource($book), ['accept' => ['application/ld+json']]); | ||
$response->assertStatus(200); | ||
$response->assertHeader('allow', 'OPTIONS, HEAD, PUT, PATCH, GET, DELETE'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* 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\State\Processor; | ||
|
||
use ApiPlatform\Metadata\Error; | ||
use ApiPlatform\Metadata\HttpOperation; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; | ||
use ApiPlatform\Metadata\ResourceClassResolverInterface; | ||
use ApiPlatform\State\ProcessorInterface; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
/** | ||
* @template T1 | ||
* @template T2 | ||
* | ||
* @implements ProcessorInterface<T1, T2> | ||
*/ | ||
final class LinkedDataPlatformProcessor implements ProcessorInterface | ||
{ | ||
private const DEFAULT_ALLOWED_METHOD = ['OPTIONS', 'HEAD']; | ||
|
||
/** | ||
* @param ProcessorInterface<T1, T2> $decorated | ||
*/ | ||
public function __construct( | ||
private readonly ProcessorInterface $decorated, // todo is processor interface nullable | ||
private readonly ?ResourceClassResolverInterface $resourceClassResolver = null, | ||
private readonly ?ResourceMetadataCollectionFactoryInterface $resourceCollectionMetadataFactory = null, | ||
) { | ||
} | ||
|
||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed | ||
{ | ||
$response = $this->decorated->process($data, $operation, $uriVariables, $context); | ||
if ( | ||
!$response instanceof Response | ||
|| !$operation instanceof HttpOperation | ||
|| $operation instanceof Error | ||
|| null === $this->resourceCollectionMetadataFactory | ||
|| !($context['resource_class'] ?? null) | ||
|| null === $operation->getUriTemplate() | ||
|| !$this->resourceClassResolver?->isResourceClass($context['resource_class']) | ||
) { | ||
return $response; | ||
} | ||
|
||
$allowedMethods = self::DEFAULT_ALLOWED_METHOD; | ||
$resourceMetadataCollection = $this->resourceCollectionMetadataFactory->create($context['resource_class']); | ||
foreach ($resourceMetadataCollection as $resource) { | ||
foreach ($resource->getOperations() as $resourceOperation) { | ||
if ($resourceOperation->getUriTemplate() === $operation->getUriTemplate()) { | ||
$operationMethod = $resourceOperation->getMethod(); | ||
$allowedMethods[] = $operationMethod; | ||
if ('POST' === $operationMethod && \is_array($outputFormats = $operation->getOutputFormats())) { | ||
$response->headers->set('Accept-Post', implode(', ', array_merge(...array_values($outputFormats)))); | ||
} | ||
} | ||
} | ||
} | ||
|
||
$response->headers->set('Allow', implode(', ', $allowedMethods)); | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.