-
-
Notifications
You must be signed in to change notification settings - Fork 933
feat(state): add headers to comply with LDP specification #6917
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
Draft
LaurentHuzard
wants to merge
1
commit into
api-platform:main
Choose a base branch
from
LaurentHuzard:feat/header-for-ldp-spec
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,78 @@ | ||
<?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\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_METHODS = ['OPTIONS', 'HEAD']; | ||
|
||
/** | ||
* @param ProcessorInterface<T1, T2> $decorated | ||
*/ | ||
public function __construct( | ||
private readonly ProcessorInterface $decorated, | ||
private readonly ResourceClassResolverInterface $resourceClassResolver, | ||
private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, | ||
) { | ||
} | ||
|
||
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 | ||
|| !$operation->getUriTemplate() | ||
|| !$this->resourceClassResolver->isResourceClass($operation->getClass()) | ||
) { | ||
return $response; | ||
} | ||
|
||
$acceptPost = null; | ||
$allowedMethods = self::DEFAULT_ALLOWED_METHODS; | ||
$resourceCollection = $this->resourceMetadataCollectionFactory->create($operation->getClass()); | ||
foreach ($resourceCollection as $resource) { | ||
foreach ($resource->getOperations() as $op) { | ||
if ($op->getUriTemplate() === $operation->getUriTemplate()) { | ||
$allowedMethods[] = $method = $op->getMethod(); | ||
if ('POST' === $method && \is_array($outputFormats = $op->getOutputFormats())) { | ||
$acceptPost = implode(', ', array_merge(...array_values($outputFormats))); | ||
} | ||
} | ||
} | ||
} | ||
if ($acceptPost) { | ||
$response->headers->set('Accept-Post', $acceptPost); | ||
} | ||
|
||
$response->headers->set('Allow', implode(', ', $allowedMethods)); | ||
|
||
return $response; | ||
} | ||
} |
This file contains hidden or 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,22 @@ | ||
<?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\State\Tests\Fixtures\ApiResource; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
|
||
#[ApiResource()] | ||
class Dummy | ||
{ | ||
public int $id; | ||
} |
161 changes: 161 additions & 0 deletions
161
src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php
This file contains hidden or 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,161 @@ | ||
<?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\State\Tests\Processor; | ||
|
||
use ApiPlatform\Hal\Tests\Fixtures\Dummy; | ||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Delete; | ||
use ApiPlatform\Metadata\Error; | ||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Metadata\GetCollection; | ||
use ApiPlatform\Metadata\Post; | ||
use ApiPlatform\Metadata\Put; | ||
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; | ||
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; | ||
use ApiPlatform\Metadata\ResourceClassResolverInterface; | ||
use ApiPlatform\State\Processor\LinkedDataPlatformProcessor; | ||
use ApiPlatform\State\ProcessorInterface; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class LinkedDataPlatformProcessorTest extends TestCase | ||
{ | ||
private ResourceMetadataCollectionFactoryInterface&MockObject $resourceMetadataCollectionFactory; | ||
|
||
private ResourceClassResolverInterface&MockObject $resourceClassResolver; | ||
|
||
private ProcessorInterface&MockObject $decorated; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->resourceClassResolver = $this->createMock(ResourceClassResolverInterface::class); | ||
$this->resourceClassResolver | ||
->method('isResourceClass') | ||
->willReturn(true); | ||
|
||
$this->resourceMetadataCollectionFactory = $this->createMock(ResourceMetadataCollectionFactoryInterface::class); | ||
$this->resourceMetadataCollectionFactory | ||
->method('create') | ||
->willReturn( | ||
new ResourceMetadataCollection(Dummy::class, [ | ||
new ApiResource(operations: [ | ||
new Get(uriTemplate: '/dummy/{dummyResourceId}{._format}', class: Dummy::class, name: 'get'), | ||
new GetCollection(uriTemplate: '/dummy{._format}', class: Dummy::class, name: 'get_collections'), | ||
new Post(uriTemplate: '/dummy{._format}', outputFormats: ['jsonld' => ['application/ld+json'], 'text/turtle' => ['text/turtle']], class: Dummy::class, name: 'post'), | ||
new Delete(uriTemplate: '/dummy/{dummyResourceId}{._format}', class: Dummy::class, name: 'delete'), | ||
new Put(uriTemplate: '/dummy/{dummyResourceId}{._format}', class: Dummy::class, name: 'put'), | ||
]), | ||
]) | ||
); | ||
|
||
$this->decorated = $this->createMock(ProcessorInterface::class); | ||
$this->decorated->method('process')->willReturn(new Response()); | ||
} | ||
|
||
public function testHeadersAcceptPostIsReturnWhenPostAllowed(): void | ||
{ | ||
$operation = new Get('/dummy{._format}', class: Dummy::class); | ||
|
||
$context = $this->getContext(); | ||
|
||
$processor = new LinkedDataPlatformProcessor( | ||
$this->decorated, | ||
$this->resourceClassResolver, | ||
$this->resourceMetadataCollectionFactory | ||
); | ||
/** @var Response $response */ | ||
$response = $processor->process(null, $operation, [], $context); | ||
|
||
$this->assertSame('application/ld+json, text/turtle', $response->headers->get('Accept-Post')); | ||
} | ||
|
||
public function testHeadersAcceptPostIsNotSetWhenPostIsNotAllowed(): void | ||
{ | ||
$operation = new Get('/dummy/{dummyResourceId}{._format}', class: Dummy::class); | ||
$context = $this->getContext(); | ||
|
||
$processor = new LinkedDataPlatformProcessor( | ||
$this->decorated, | ||
$this->resourceClassResolver, | ||
$this->resourceMetadataCollectionFactory | ||
); | ||
/** @var Response $response */ | ||
$response = $processor->process(null, $operation, [], $context); | ||
|
||
$this->assertNull($response->headers->get('Accept-Post')); | ||
} | ||
|
||
public function testHeaderAllowReflectsResourceAllowedMethods(): void | ||
{ | ||
$operation = new Get('/dummy{._format}', class: Dummy::class); | ||
$context = $this->getContext(); | ||
|
||
$processor = new LinkedDataPlatformProcessor( | ||
$this->decorated, | ||
$this->resourceClassResolver, | ||
$this->resourceMetadataCollectionFactory | ||
); | ||
/** @var Response $response */ | ||
$response = $processor->process(null, $operation, [], $context); | ||
$allowHeader = $response->headers->get('Allow'); | ||
$this->assertStringContainsString('OPTIONS', $allowHeader); | ||
$this->assertStringContainsString('HEAD', $allowHeader); | ||
$this->assertStringContainsString('GET', $allowHeader); | ||
$this->assertStringContainsString('POST', $allowHeader); | ||
$operation = new Get('/dummy/{dummyResourceId}{._format}', class: Dummy::class); | ||
|
||
/** @var Response $response */ | ||
$processor = new LinkedDataPlatformProcessor( | ||
$this->decorated, | ||
$this->resourceClassResolver, | ||
$this->resourceMetadataCollectionFactory | ||
); | ||
/** @var Response $response */ | ||
$response = $processor->process('data', $operation, [], $this->getContext()); | ||
$allowHeader = $response->headers->get('Allow'); | ||
$this->assertStringContainsString('OPTIONS', $allowHeader); | ||
$this->assertStringContainsString('HEAD', $allowHeader); | ||
$this->assertStringContainsString('GET', $allowHeader); | ||
$this->assertStringContainsString('PUT', $allowHeader); | ||
$this->assertStringContainsString('DELETE', $allowHeader); | ||
} | ||
|
||
public function testProcessorWithoutRequiredConditionReturnOriginalResponse(): void | ||
{ | ||
// Operation is an Error | ||
$processor = new LinkedDataPlatformProcessor($this->decorated, $this->resourceClassResolver, $this->resourceMetadataCollectionFactory); | ||
$response = $processor->process(null, new Error(), $this->getContext()); | ||
$this->assertNull($response->headers->get('Allow')); | ||
} | ||
|
||
private function createGetRequest(): Request | ||
{ | ||
$request = new Request(); | ||
$request->setMethod('GET'); | ||
$request->setRequestFormat('json'); | ||
$request->headers->set('Accept', 'application/ld+json'); | ||
|
||
return $request; | ||
} | ||
|
||
private function getContext(): array | ||
{ | ||
return [ | ||
'resource_class' => Dummy::class, | ||
'request' => $this->createGetRequest(), | ||
]; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
63 changes: 63 additions & 0 deletions
63
tests/Fixtures/TestBundle/ApiResource/DummyGetPostDeleteOperation.php
This file contains hidden or 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,63 @@ | ||
<?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\Tests\Fixtures\TestBundle\ApiResource; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Delete; | ||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Metadata\GetCollection; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\Metadata\Post; | ||
|
||
#[ApiResource(operations: [ | ||
new Get( | ||
uriTemplate: '/dummy_get_post_delete_operations/{id}', | ||
provider: [self::class, 'provideItem'], | ||
), | ||
new GetCollection( | ||
uriTemplate: '/dummy_get_post_delete_operations', | ||
provider: [self::class, 'provide'], ), | ||
new Post( | ||
uriTemplate: '/dummy_get_post_delete_operations', | ||
provider: [self::class, 'provide'], ), | ||
new Delete( | ||
uriTemplate: '/dummy_get_post_delete_operations/{id}', | ||
provider: [self::class, 'provideItem'], ), | ||
])] | ||
class DummyGetPostDeleteOperation | ||
{ | ||
public ?int $id; | ||
|
||
public ?string $name = null; | ||
|
||
public static function provide(Operation $operation, array $uriVariables = [], array $context = []): array | ||
{ | ||
$dummyResource = new self(); | ||
$dummyResource->id = 1; | ||
$dummyResource->name = 'Dummy name'; | ||
|
||
return [ | ||
$dummyResource, | ||
]; | ||
} | ||
|
||
public static function provideItem(Operation $operation, array $uriVariables = [], array $context = []): self | ||
{ | ||
$dummyResource = new self(); | ||
$dummyResource->id = $uriVariables['id']; | ||
$dummyResource->name = 'Dummy name'; | ||
|
||
return $dummyResource; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is interesting, we've never had the use case before where we want all "POST" operation that belong to the same URI. Note to self that it's here if I need it elsewhere :D.