Skip to content

Commit 1bcca09

Browse files
authored
fix(symfony): provider can throw validation exception (#5586)
fixes #5585
1 parent 1fc611e commit 1bcca09

File tree

5 files changed

+44
-7
lines changed

5 files changed

+44
-7
lines changed

features/main/attribute_resource.feature

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,17 @@ Feature: Resource attributes
103103

104104
Scenario: Uri variables with Post operation
105105
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:
106+
And I send a "POST" request to "/post_with_uri_variables_and_no_provider/{id}" with body:
107107
"""
108108
{}
109109
"""
110110
Then the response status code should be 201
111+
112+
Scenario: Throw validation exception in a provider
113+
When I add "Content-Type" header equal to "application/ld+json"
114+
And I send a "POST" request to "/post_with_uri_variables/{id}" with body:
115+
"""
116+
{}
117+
"""
118+
Then the response status code should be 422
119+

src/State/CallableProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
namespace ApiPlatform\State;
1515

16-
use ApiPlatform\Exception\RuntimeException;
1716
use ApiPlatform\Metadata\Operation;
17+
use ApiPlatform\State\Exception\ProviderNotFoundException;
1818
use Psr\Container\ContainerInterface;
1919

2020
final class CallableProvider implements ProviderInterface
@@ -34,7 +34,7 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
3434

3535
if (\is_string($provider)) {
3636
if (!$this->locator->has($provider)) {
37-
throw new RuntimeException(sprintf('Provider "%s" not found on operation "%s"', $provider, $operation->getName()));
37+
throw new ProviderNotFoundException(sprintf('Provider "%s" not found on operation "%s"', $provider, $operation->getName()));
3838
}
3939

4040
/** @var ProviderInterface $providerInstance */
@@ -43,6 +43,6 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
4343
return $providerInstance->provide($operation, $uriVariables, $context);
4444
}
4545

46-
throw new RuntimeException(sprintf('Provider not found on operation "%s"', $operation->getName()));
46+
throw new ProviderNotFoundException(sprintf('Provider not found on operation "%s"', $operation->getName()));
4747
}
4848
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
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\State\Exception;
15+
16+
use ApiPlatform\Metadata\Exception\RuntimeException;
17+
18+
final class ProviderNotFoundException extends RuntimeException
19+
{
20+
}

src/Symfony/EventListener/ReadListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
use ApiPlatform\Api\UriVariablesConverterInterface;
1717
use ApiPlatform\Exception\InvalidIdentifierException;
1818
use ApiPlatform\Exception\InvalidUriVariableException;
19-
use ApiPlatform\Exception\RuntimeException;
2019
use ApiPlatform\Metadata\HttpOperation;
2120
use ApiPlatform\Metadata\Put;
2221
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
2322
use ApiPlatform\Serializer\SerializerContextBuilderInterface;
23+
use ApiPlatform\State\Exception\ProviderNotFoundException;
2424
use ApiPlatform\State\ProviderInterface;
2525
use ApiPlatform\State\UriVariablesResolverTrait;
2626
use ApiPlatform\Util\CloneTrait;
@@ -93,7 +93,7 @@ public function onKernelRequest(RequestEvent $event): void
9393
$data = $this->provider->provide($operation, $uriVariables, $context);
9494
} catch (InvalidIdentifierException|InvalidUriVariableException $e) {
9595
throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
96-
} catch (RuntimeException $e) {
96+
} catch (ProviderNotFoundException $e) {
9797
$data = null;
9898
}
9999

tests/Fixtures/TestBundle/ApiResource/PostWithUriVariables.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
use ApiPlatform\Metadata\NotExposed;
1717
use ApiPlatform\Metadata\Operation;
1818
use ApiPlatform\Metadata\Post;
19+
use ApiPlatform\Symfony\Validator\Exception\ValidationException as ExceptionValidationException;
20+
use Symfony\Component\Validator\ConstraintViolationList;
1921

2022
#[NotExposed(uriTemplate: '/post_with_uri_variables/{id}')]
21-
#[Post(uriTemplate: '/post_with_uri_variables/{id}', uriVariables: ['id'], processor: [PostWithUriVariables::class, 'process'])]
23+
#[Post(uriTemplate: '/post_with_uri_variables_and_no_provider/{id}', uriVariables: ['id'], processor: [PostWithUriVariables::class, 'process'])]
24+
#[Post(uriTemplate: '/post_with_uri_variables/{id}', uriVariables: ['id'], provider: [PostWithUriVariables::class, 'provide'])]
2225
final class PostWithUriVariables
2326
{
2427
public function __construct(public readonly ?int $id = null)
@@ -29,4 +32,9 @@ public static function process(mixed $data, Operation $operation, array $uriVari
2932
{
3033
return new self(id: 1);
3134
}
35+
36+
public static function provide(Operation $operation, array $uriVariables = [], array $context = []): void
37+
{
38+
throw new ExceptionValidationException(new ConstraintViolationList());
39+
}
3240
}

0 commit comments

Comments
 (0)