Skip to content

Commit 10f24f7

Browse files
authored
fix(state): no location header without output (#6356)
fixes #6352
1 parent 9d159f4 commit 10f24f7

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed

features/jsonld/no_output.feature

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Feature: Disable Id generation on anonymous resource collections
2+
3+
@!mongodb
4+
Scenario: Post to an output false should not generate an IRI
5+
When I add "Content-Type" header equal to "application/ld+json"
6+
And I send a "POST" request to "/no_iri_messages" with body:
7+
"""
8+
{}
9+
"""
10+
Then the response status code should be 202

src/Metadata/Resource/Factory/InputOutputResourceMetadataCollectionFactory.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,18 @@ private function getTransformedOperations(Operations|array $operations, ApiResou
6565
&& \array_key_exists('class', $operation->getInput())
6666
&& null === $operation->getInput()['class']
6767
) {
68-
$operation = $operation->withDeserialize(false);
69-
$operation = $operation->withValidate(false);
68+
$operation = $operation->withDeserialize(null === $operation->canDeserialize() ? false : $operation->canDeserialize());
69+
$operation = $operation->withValidate(null === $operation->canValidate() ? false : $operation->canValidate());
7070
}
7171

7272
if (
7373
$operation instanceof HttpOperation
7474
&& $operation->getOutput()
7575
&& \array_key_exists('class', $operation->getOutput())
7676
&& null === $operation->getOutput()['class']
77+
&& null === $operation->getStatus()
7778
) {
78-
$operation = $operation->withStatus($operation->getStatus() ?? 204);
79+
$operation = $operation->withStatus(204);
7980
}
8081

8182
$operations instanceof Operations ? $operations->add($key, $operation) : $operations[$key] = $operation;

src/State/Processor/RespondProcessor.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
8888
$method = $request->getMethod();
8989
$originalData = $context['original_data'] ?? null;
9090

91-
if ($hasData = ($this->resourceClassResolver && $originalData && \is_object($originalData) && $this->resourceClassResolver->isResourceClass($this->getObjectClass($originalData))) && $this->iriConverter) {
91+
$outputMetadata = $operation->getOutput() ?? ['class' => $operation->getClass()];
92+
$hasOutput = \is_array($outputMetadata) && \array_key_exists('class', $outputMetadata) && null !== $outputMetadata['class'];
93+
$hasData = !$hasOutput ? false : ($this->resourceClassResolver && $originalData && \is_object($originalData) && $this->resourceClassResolver->isResourceClass($this->getObjectClass($originalData)));
94+
95+
if ($hasData && $this->iriConverter) {
9296
if (
9397
!isset($headers['Location'])
9498
&& 300 <= $status && $status < 400
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6352;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\NotExposed;
18+
use ApiPlatform\Metadata\Post;
19+
20+
#[ApiResource(operations: [
21+
new NotExposed(),
22+
new Post(status: 202, output: false),
23+
])]
24+
class NoIriMessage
25+
{
26+
public ?int $id = null;
27+
}

tests/State/RespondProcessorTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,22 @@ public function testRedirectToOperation(): void
3535
{
3636
$canonicalUriTemplateRedirectingOperation = new Get(
3737
status: 302,
38+
class: Employee::class,
3839
extraProperties: [
3940
'canonical_uri_template' => '/canonical',
4041
]
4142
);
4243

4344
$alternateRedirectingResourceOperation = new Get(
4445
status: 308,
46+
class: Employee::class,
4547
extraProperties: [
4648
'is_alternate_resource_metadata' => true,
4749
]
4850
);
4951

5052
$alternateResourceOperation = new Get(
53+
class: Employee::class,
5154
extraProperties: [
5255
'is_alternate_resource_metadata' => true,
5356
]

0 commit comments

Comments
 (0)