Skip to content

Commit

Permalink
fix(state): no location header without output
Browse files Browse the repository at this point in the history
fixes #6352
  • Loading branch information
soyuka committed May 7, 2024
1 parent 6fa4bb7 commit 3548bb4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
10 changes: 10 additions & 0 deletions features/jsonld/no_output.feature
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion src/State/Processor/RespondProcessor.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 3548bb4

Please sign in to comment.