Skip to content

Commit

Permalink
fix(state): no location header without output (#6356)
Browse files Browse the repository at this point in the history
fixes #6352
  • Loading branch information
soyuka committed May 10, 2024
1 parent 9d159f4 commit 10f24f7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
10 changes: 10 additions & 0 deletions features/jsonld/no_output.feature
@@ -0,0 +1,10 @@
Feature: Disable Id generation on anonymous resource collections

@!mongodb
Scenario: Post to an output false should not generate an IRI
When I add "Content-Type" header equal to "application/ld+json"
And I send a "POST" request to "/no_iri_messages" with body:
"""
{}
"""
Then the response status code should be 202
Expand Up @@ -65,17 +65,18 @@ private function getTransformedOperations(Operations|array $operations, ApiResou
&& \array_key_exists('class', $operation->getInput())
&& null === $operation->getInput()['class']
) {
$operation = $operation->withDeserialize(false);
$operation = $operation->withValidate(false);
$operation = $operation->withDeserialize(null === $operation->canDeserialize() ? false : $operation->canDeserialize());
$operation = $operation->withValidate(null === $operation->canValidate() ? false : $operation->canValidate());
}

if (
$operation instanceof HttpOperation
&& $operation->getOutput()
&& \array_key_exists('class', $operation->getOutput())
&& null === $operation->getOutput()['class']
&& null === $operation->getStatus()
) {
$operation = $operation->withStatus($operation->getStatus() ?? 204);
$operation = $operation->withStatus(204);
}

$operations instanceof Operations ? $operations->add($key, $operation) : $operations[$key] = $operation;
Expand Down
6 changes: 5 additions & 1 deletion src/State/Processor/RespondProcessor.php
Expand Up @@ -88,7 +88,11 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
$method = $request->getMethod();
$originalData = $context['original_data'] ?? null;

if ($hasData = ($this->resourceClassResolver && $originalData && \is_object($originalData) && $this->resourceClassResolver->isResourceClass($this->getObjectClass($originalData))) && $this->iriConverter) {
$outputMetadata = $operation->getOutput() ?? ['class' => $operation->getClass()];
$hasOutput = \is_array($outputMetadata) && \array_key_exists('class', $outputMetadata) && null !== $outputMetadata['class'];
$hasData = !$hasOutput ? false : ($this->resourceClassResolver && $originalData && \is_object($originalData) && $this->resourceClassResolver->isResourceClass($this->getObjectClass($originalData)));

if ($hasData && $this->iriConverter) {
if (
!isset($headers['Location'])
&& 300 <= $status && $status < 400
Expand Down
27 changes: 27 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/Issue6352/NoIriMessage.php
@@ -0,0 +1,27 @@
<?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\Issue6352;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\NotExposed;
use ApiPlatform\Metadata\Post;

#[ApiResource(operations: [
new NotExposed(),
new Post(status: 202, output: false),
])]
class NoIriMessage
{
public ?int $id = null;
}
3 changes: 3 additions & 0 deletions tests/State/RespondProcessorTest.php
Expand Up @@ -35,19 +35,22 @@ public function testRedirectToOperation(): void
{
$canonicalUriTemplateRedirectingOperation = new Get(
status: 302,
class: Employee::class,
extraProperties: [
'canonical_uri_template' => '/canonical',
]
);

$alternateRedirectingResourceOperation = new Get(
status: 308,
class: Employee::class,
extraProperties: [
'is_alternate_resource_metadata' => true,
]
);

$alternateResourceOperation = new Get(
class: Employee::class,
extraProperties: [
'is_alternate_resource_metadata' => true,
]
Expand Down

0 comments on commit 10f24f7

Please sign in to comment.