Skip to content
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

fix(state): no location header without output #6356

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number Diff line number Diff line change
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
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;
}
3 changes: 3 additions & 0 deletions tests/State/RespondProcessorTest.php
Original file line number Diff line number Diff line change
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