Skip to content

Commit 1f23344

Browse files
authored
fix(symfony): status at 200 when allowCreate is false (#5465)
1 parent 16a1a61 commit 1f23344

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

features/main/custom_put.feature

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Feature: Spec-compliant PUT support
2+
As a client software developer
3+
I need to be able to create or replace resources using the PUT HTTP method
4+
5+
@createSchema
6+
@!mongodb
7+
Scenario: Get a correct status code when updating a resource that is not allowed to read nor to create
8+
When I add "Content-Type" header equal to "application/ld+json"
9+
And I send a "PUT" request to "/custom_puts/1" with body:
10+
"""
11+
{
12+
"foo": "a",
13+
"bar": "b"
14+
}
15+
"""
16+
Then the response status code should be 200
17+
And the response status code should not be 201
18+
And the response should be in JSON
19+
And the JSON should be equal to:
20+
"""
21+
{
22+
"@context": "/contexts/CustomPut",
23+
"@id": "/custom_puts/1",
24+
"@type": "CustomPut",
25+
"id": 1,
26+
"foo": "a",
27+
"bar": "b"
28+
}
29+
"""

src/Symfony/EventListener/RespondListener.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Api\IriConverterInterface;
1717
use ApiPlatform\Api\UrlGeneratorInterface;
1818
use ApiPlatform\Metadata\HttpOperation;
19+
use ApiPlatform\Metadata\Put;
1920
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
2021
use ApiPlatform\Util\OperationRequestInitiatorTrait;
2122
use ApiPlatform\Util\RequestAttributesExtractor;
@@ -90,7 +91,7 @@ public function onKernelView(ViewEvent $event): void
9091
) {
9192
$status = 301;
9293
$headers['Location'] = $this->iriConverter->getIriFromResource($request->attributes->get('data'), UrlGeneratorInterface::ABS_PATH, $operation);
93-
} elseif (HttpOperation::METHOD_PUT === $method && !($attributes['previous_data'] ?? null) && null === $status) {
94+
} elseif (HttpOperation::METHOD_PUT === $method && !($attributes['previous_data'] ?? null) && null === $status && ($operation instanceof Put && ($operation->getAllowCreate() ?? false))) {
9495
$status = Response::HTTP_CREATED;
9596
}
9697

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\Entity;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\Get;
18+
use ApiPlatform\Metadata\Put;
19+
use Doctrine\ORM\Mapping\Column;
20+
use Doctrine\ORM\Mapping\Entity;
21+
use Doctrine\ORM\Mapping\Id;
22+
23+
/**
24+
* @author Kévin Dunglas <[email protected]>
25+
*/
26+
#[ApiResource(
27+
operations: [new Get(), new Put(read: false, allowCreate: false)],
28+
extraProperties: [
29+
'custom_put' => true,
30+
]
31+
)]
32+
#[Entity]
33+
class CustomPut
34+
{
35+
#[Id]
36+
#[Column]
37+
public ?int $id = null;
38+
39+
#[Column]
40+
public string $foo = '';
41+
42+
#[Column]
43+
public string $bar = '';
44+
}

0 commit comments

Comments
 (0)