Skip to content

Commit cb75a41

Browse files
authored
Merge pull request #2343 from alexander-schranz/reproducable/custom-flattenexception-normalizer-2
Fix FlattenExceptionNormalizer without custom SerializeErrorRenderer
2 parents 2bb540b + 5006901 commit cb75a41

File tree

6 files changed

+88
-1
lines changed

6 files changed

+88
-1
lines changed

Serializer/Normalizer/FlattenExceptionNormalizer.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ public function normalize($exception, $format = null, array $context = [])
7676

7777
public function supportsNormalization($data, $format = null, array $context = [])
7878
{
79-
return $data instanceof FlattenException && ($context[Serializer::FOS_BUNDLE_SERIALIZATION_CONTEXT] ?? false);
79+
if (!($data instanceof FlattenException)) {
80+
return false;
81+
}
82+
83+
// we are in fos rest context
84+
if (!empty($context[Serializer::FOS_BUNDLE_SERIALIZATION_CONTEXT])) {
85+
return true;
86+
}
87+
88+
// we are in messenger context
89+
if (!empty($context['messenger_serialization'])) { // Serializer::MESSENGER_SERIALIZATION_CONTEXT
90+
return false;
91+
}
92+
93+
return true;
8094
}
8195
}

Tests/Functional/Bundle/TestBundle/Controller/SerializerErrorController.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
use Symfony\Component\Form\Extension\Core\Type\TextType;
1717
use Symfony\Component\Validator\Constraints\NotBlank;
1818

19+
class CustomArgumentException extends \Exception
20+
{
21+
}
22+
1923
/**
2024
* Controller to test serialization of various errors and exceptions.
2125
*
@@ -38,6 +42,11 @@ public function invalidArgumentExceptionAction()
3842
throw new \InvalidArgumentException('Invalid argument given.');
3943
}
4044

45+
public function customExceptionAction()
46+
{
47+
throw new CustomArgumentException('Custom exception');
48+
}
49+
4150
/**
4251
* @View
4352
*/

Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ test_serializer_error_invalid_form:
1818
path: /serializer-error/invalid-form.{_format}
1919
defaults: { _controller: FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\SerializerErrorController::invalidFormAction }
2020

21+
test_custom_exception_serializer:
22+
path: /serializer-error/custom-argument-exception.{_format}
23+
defaults: { _controller: FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\SerializerErrorController::customExceptionAction }
24+
2125
# Must be defined before test_version
2226
test_version2:
2327
resource: FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\Version2Controller

Tests/Functional/SerializerErrorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class SerializerErrorTest extends WebTestCase
2222
{
2323
public static function tearDownAfterClass(): void
2424
{
25+
self::deleteTmpDir('CustomFlattenExceptionNormalizer');
2526
self::deleteTmpDir('FlattenExceptionHandlerLegacyFormat');
2627
self::deleteTmpDir('FlattenExceptionHandlerRfc7807Format');
2728
self::deleteTmpDir('FlattenExceptionNormalizerLegacyFormat');
@@ -121,6 +122,23 @@ public function testSerializeExceptionCodeMappedToResponseStatusCodeJsonUsingErr
121122
$this->assertEquals(json_encode($expectedJson), $client->getResponse()->getContent());
122123
}
123124

125+
public function testCustomExceptionSerialization()
126+
{
127+
if (!class_exists(SerializerErrorRenderer::class)) {
128+
$this->markTestSkipped();
129+
}
130+
131+
$this->iniSet('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul');
132+
133+
$client = $this->createClient(['test_case' => 'CustomFlattenExceptionNormalizer', 'debug' => false]);
134+
$client->request('GET', '/serializer-error/custom-argument-exception.json');
135+
136+
$this->assertEquals(
137+
'{"code":409,"message":"Conflict"}',
138+
$client->getResponse()->getContent()
139+
);
140+
}
141+
124142
public function serializeExceptionCodeMappedToResponseStatusCodeJsonProvider(): array
125143
{
126144
return [
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.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+
return [
13+
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
14+
new \FOS\RestBundle\FOSRestBundle(),
15+
new \JMS\SerializerBundle\JMSSerializerBundle(),
16+
new \FOS\RestBundle\Tests\Functional\Bundle\TestBundle\TestBundle(),
17+
];
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
4+
framework:
5+
serializer: true
6+
7+
fos_rest:
8+
exception:
9+
codes:
10+
'FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\CustomArgumentException': 409
11+
enabled: true
12+
map_exception_codes: true
13+
exception_listener: false
14+
serialize_exceptions: false
15+
flatten_exception_format: 'legacy'
16+
serializer_error_renderer: false
17+
serializer:
18+
serialize_null: true
19+
body_listener:
20+
enabled: true
21+
routing_loader: false
22+
view:
23+
formats:
24+
json: true
25+
csv: true

0 commit comments

Comments
 (0)