Skip to content

Commit 2121d15

Browse files
committed
fix(symfony): allow post with uri variables and no provider
1 parent ed4bca9 commit 2121d15

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

features/main/attribute_resource.feature

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,11 @@ Feature: Resource attributes
100100
And the response should be in JSON
101101
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
102102
And the JSON node "hydra:description" should be equal to 'Unable to generate an IRI for the item of type "ApiPlatform\Tests\Fixtures\TestBundle\Entity\IncompleteUriVariableConfigured"'
103+
104+
Scenario: Uri variables with Post operation
105+
When I add "Content-Type" header equal to "application/ld+json"
106+
And I send a "POST" request to "/post_with_uri_variables/{id}" with body:
107+
"""
108+
{}
109+
"""
110+
Then the response status code should be 201

src/Symfony/EventListener/ReadListener.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Api\UriVariablesConverterInterface;
1717
use ApiPlatform\Exception\InvalidIdentifierException;
1818
use ApiPlatform\Exception\InvalidUriVariableException;
19+
use ApiPlatform\Exception\RuntimeException;
1920
use ApiPlatform\Metadata\HttpOperation;
2021
use ApiPlatform\Metadata\Put;
2122
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
@@ -92,10 +93,13 @@ public function onKernelRequest(RequestEvent $event): void
9293
$data = $this->provider->provide($operation, $uriVariables, $context);
9394
} catch (InvalidIdentifierException|InvalidUriVariableException $e) {
9495
throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
96+
} catch (RuntimeException $e) {
97+
$data = null;
9598
}
9699

97100
if (
98101
null === $data
102+
&& HttpOperation::METHOD_POST !== $operation->getMethod()
99103
&& (
100104
HttpOperation::METHOD_PUT !== $operation->getMethod()
101105
|| ($operation instanceof Put && !($operation->getAllowCreate() ?? false))
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\ApiResource;
15+
16+
use ApiPlatform\Metadata\NotExposed;
17+
use ApiPlatform\Metadata\Operation;
18+
use ApiPlatform\Metadata\Post;
19+
20+
#[NotExposed(uriTemplate: '/post_with_uri_variables/{id}')]
21+
#[Post(uriTemplate: '/post_with_uri_variables/{id}', uriVariables: ['id'], processor: [PostWithUriVariables::class, 'process'])]
22+
final class PostWithUriVariables
23+
{
24+
public function __construct(public readonly ?int $id = null)
25+
{
26+
}
27+
28+
public static function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
29+
{
30+
return new self(id: 1);
31+
}
32+
}

0 commit comments

Comments
 (0)