Skip to content

Commit 9d159f4

Browse files
authored
fix(symfony): no read shouldn't throw InvalidIdentifiers (#6357)
fixes 6355
1 parent 6fa4bb7 commit 9d159f4

File tree

5 files changed

+116
-1
lines changed

5 files changed

+116
-1
lines changed

features/main/patch.feature

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,14 @@ Feature: Sending PATCH requets
8080
"alpha": "/alphas/2"
8181
}
8282
"""
83+
84+
Scenario: Patch a non-readable resource
85+
When I add "Content-Type" header equal to "application/merge-patch+json"
86+
And I send a "PATCH" request to "/order_products/1/count" with body:
87+
"""
88+
{
89+
"id": 1,
90+
"count": 10
91+
}
92+
"""
93+
Then print last JSON response

src/Symfony/EventListener/ReadListener.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ public function onKernelRequest(RequestEvent $event): void
8484
try {
8585
$uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $operation->getClass());
8686
} catch (InvalidIdentifierException|InvalidUriVariableException $e) {
87-
throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
87+
if ($operation->canRead()) {
88+
throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
89+
}
8890
}
8991
}
9092

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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\Issue6355;
15+
16+
use ApiPlatform\Metadata\ApiProperty;
17+
use Symfony\Component\Uid\Uuid;
18+
19+
class OrderDto
20+
{
21+
#[ApiProperty(identifier: false)]
22+
public ?int $id = null;
23+
24+
#[ApiProperty(identifier: true)]
25+
public ?Uuid $uuid = null;
26+
}
Lines changed: 44 additions & 0 deletions
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\ApiResource\Issue6355;
15+
16+
use ApiPlatform\Metadata\ApiProperty;
17+
use ApiPlatform\Metadata\ApiResource;
18+
use ApiPlatform\Metadata\NotExposed;
19+
use ApiPlatform\Metadata\Patch;
20+
use ApiPlatform\Tests\Fixtures\TestBundle\Controller\Issue6355\UpdateOrderProductCountController;
21+
22+
#[ApiResource(
23+
shortName: 'OrderProduct',
24+
operations: [
25+
new NotExposed(),
26+
new Patch(
27+
uriTemplate: '/order_products/{id}/count',
28+
controller: UpdateOrderProductCountController::class,
29+
class: OrderDto::class,
30+
input: OrderProductCount::class,
31+
output: OrderDto::class,
32+
read: false,
33+
write: false,
34+
name: 'order_product_update_count',
35+
),
36+
],
37+
order: ['position' => 'ASC'],
38+
)]
39+
class OrderProductCount
40+
{
41+
#[ApiProperty(writable: false, identifier: true)]
42+
public ?int $id = null;
43+
public ?int $count = null;
44+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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\Controller\Issue6355;
15+
16+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6355\OrderDto;
17+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6355\OrderProductCount;
18+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
19+
use Symfony\Component\HttpFoundation\Request;
20+
use Symfony\Component\HttpKernel\Attribute\AsController;
21+
22+
#[AsController]
23+
class UpdateOrderProductCountController extends AbstractController
24+
{
25+
public function __invoke(OrderProductCount $data, Request $request): OrderDto
26+
{
27+
$dto = new OrderDto();
28+
$dto->id = 1;
29+
30+
return $dto;
31+
}
32+
}

0 commit comments

Comments
 (0)