Skip to content

Commit

Permalink
fix(symfony): allow post with uri variables and no provider
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed May 5, 2023
1 parent ed4bca9 commit 2121d15
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
8 changes: 8 additions & 0 deletions features/main/attribute_resource.feature
Expand Up @@ -100,3 +100,11 @@ Feature: Resource attributes
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
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"'

Scenario: Uri variables with Post operation
When I add "Content-Type" header equal to "application/ld+json"
And I send a "POST" request to "/post_with_uri_variables/{id}" with body:
"""
{}
"""
Then the response status code should be 201
4 changes: 4 additions & 0 deletions src/Symfony/EventListener/ReadListener.php
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Api\UriVariablesConverterInterface;
use ApiPlatform\Exception\InvalidIdentifierException;
use ApiPlatform\Exception\InvalidUriVariableException;
use ApiPlatform\Exception\RuntimeException;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
Expand Down Expand Up @@ -92,10 +93,13 @@ public function onKernelRequest(RequestEvent $event): void
$data = $this->provider->provide($operation, $uriVariables, $context);
} catch (InvalidIdentifierException|InvalidUriVariableException $e) {
throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
} catch (RuntimeException $e) {
$data = null;
}

if (
null === $data
&& HttpOperation::METHOD_POST !== $operation->getMethod()
&& (
HttpOperation::METHOD_PUT !== $operation->getMethod()
|| ($operation instanceof Put && !($operation->getAllowCreate() ?? false))
Expand Down
32 changes: 32 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/PostWithUriVariables.php
@@ -0,0 +1,32 @@
<?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;

use ApiPlatform\Metadata\NotExposed;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Post;

#[NotExposed(uriTemplate: '/post_with_uri_variables/{id}')]
#[Post(uriTemplate: '/post_with_uri_variables/{id}', uriVariables: ['id'], processor: [PostWithUriVariables::class, 'process'])]
final class PostWithUriVariables
{
public function __construct(public readonly ?int $id = null)
{
}

public static function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
{
return new self(id: 1);
}
}

0 comments on commit 2121d15

Please sign in to comment.