-
-
Notifications
You must be signed in to change notification settings - Fork 841
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(symfony): exception_status bad merge (#4981)
Co-authored-by: ArnoudThibaut <thibaut.arnoud@gmail.com>
- Loading branch information
1 parent
530ef37
commit 56875b3
Showing
11 changed files
with
259 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
Feature: Using exception_to_status config | ||
As an API developer | ||
I can customize the status code returned if the application throws an exception | ||
|
||
@createSchema | ||
@!mongodb | ||
Scenario: Configure status code via the operation exceptionToStatus to map custom NotFound error to 404 | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "GET" request to "/dummy_exception_to_statuses/123" | ||
Then the response status code should be 404 | ||
And the response should be in JSON | ||
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8" | ||
|
||
@!mongodb | ||
Scenario: Configure status code via the resource exceptionToStatus to map custom NotFound error to 400 | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "PUT" request to "/dummy_exception_to_statuses/123" with body: | ||
""" | ||
{ | ||
"name": "black" | ||
} | ||
""" | ||
Then the response status code should be 400 | ||
And the response should be in JSON | ||
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8" | ||
|
||
@!mongodb | ||
Scenario: Configure status code via the config file to map FilterValidationException to 400 | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "GET" request to "/dummy_exception_to_statuses" | ||
Then the response status code should be 400 | ||
And the response should be in JSON | ||
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?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\Symfony\EventListener; | ||
|
||
use ApiPlatform\Action\ExceptionAction; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\EventListener\ErrorListener as SymfonyErrorListener; | ||
|
||
/** | ||
* This error listener extends the Symfony one in order to add | ||
* the `_api_operation` attribute when the request is duplicated. | ||
* It will later be used to retrieve the exceptionToStatus from the operation ({@see ExceptionAction}). | ||
*/ | ||
final class ErrorListener extends SymfonyErrorListener | ||
{ | ||
protected function duplicateRequest(\Throwable $exception, Request $request): Request | ||
{ | ||
$dup = parent::duplicateRequest($exception, $request); | ||
|
||
if ($request->attributes->has('_api_operation')) { | ||
$dup->attributes->set('_api_operation', $request->attributes->get('_api_operation')); | ||
} | ||
|
||
// TODO: remove legacy layer in 3.0 | ||
if ($request->attributes->has('_api_exception_to_status')) { | ||
$dup->attributes->set('_api_exception_to_status', $request->attributes->get('_api_exception_to_status')); | ||
} | ||
|
||
return $dup; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
tests/Fixtures/TestBundle/Entity/DummyExceptionToStatus.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?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\Entity; | ||
|
||
use ApiPlatform\Metadata\ApiFilter; | ||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Metadata\GetCollection; | ||
use ApiPlatform\Metadata\Put; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Exception\NotFoundException; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Filter\RequiredFilter; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
#[ApiResource( | ||
exceptionToStatus: [NotFoundHttpException::class => 400], | ||
operations: [ | ||
new Get(exceptionToStatus: [NotFoundException::class => 404]), | ||
new Put(), | ||
new GetCollection(), | ||
] | ||
)] | ||
#[ApiFilter(RequiredFilter::class)] | ||
#[ORM\Entity] | ||
class DummyExceptionToStatus | ||
{ | ||
/** | ||
* @var int|null The id | ||
*/ | ||
#[ORM\Column(type: 'integer')] | ||
#[ORM\Id] | ||
#[ORM\GeneratedValue(strategy: 'AUTO')] | ||
private ?int $id = null; | ||
|
||
/** | ||
* @var string|null The dummy name | ||
*/ | ||
#[ORM\Column(nullable: true)] | ||
private ?string $name = null; | ||
|
||
/** | ||
* @var string|null The dummy title | ||
*/ | ||
#[ORM\Column(nullable: true)] | ||
private ?string $title = null; | ||
|
||
/** | ||
* @var string The dummy code | ||
*/ | ||
#[ORM\Column] | ||
private string $code; | ||
|
||
public function getId(): ?int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setId(int $id): self | ||
{ | ||
$this->id = $id; | ||
|
||
return $this; | ||
} | ||
|
||
public function getName(): ?string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setName(?string $name): self | ||
{ | ||
$this->name = $name; | ||
|
||
return $this; | ||
} | ||
|
||
public function getTitle(): ?string | ||
{ | ||
return $this->title; | ||
} | ||
|
||
public function setTitle(?string $title): self | ||
{ | ||
$this->title = $title; | ||
|
||
return $this; | ||
} | ||
|
||
public function getCode(): ?string | ||
{ | ||
return $this->code; | ||
} | ||
|
||
public function setCode(string $code): self | ||
{ | ||
$this->code = $code; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?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\Exception; | ||
|
||
final class NotFoundException extends \Exception | ||
{ | ||
public function __construct(string $message = '', int $code = 0, ?\Throwable $previous = null) | ||
{ | ||
parent::__construct($message, $code, $previous); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
tests/Fixtures/TestBundle/State/DummyExceptionToStatusProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?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\State; | ||
|
||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\State\ProviderInterface; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Exception\NotFoundException; | ||
|
||
class DummyExceptionToStatusProvider implements ProviderInterface | ||
{ | ||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable|object|null | ||
{ | ||
throw new NotFoundException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -409,3 +409,4 @@ services: | |
arguments: [ '@doctrine' ] | ||
tags: | ||
- { name: 'api_platform.state_processor' } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters