Skip to content

Commit

Permalink
Merge pull request #2343 from alexander-schranz/reproducable/custom-f…
Browse files Browse the repository at this point in the history
…lattenexception-normalizer-2

Fix FlattenExceptionNormalizer without custom SerializeErrorRenderer
  • Loading branch information
goetas committed Nov 8, 2021
2 parents 2bb540b + 5006901 commit cb75a41
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 1 deletion.
16 changes: 15 additions & 1 deletion Serializer/Normalizer/FlattenExceptionNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ public function normalize($exception, $format = null, array $context = [])

public function supportsNormalization($data, $format = null, array $context = [])
{
return $data instanceof FlattenException && ($context[Serializer::FOS_BUNDLE_SERIALIZATION_CONTEXT] ?? false);
if (!($data instanceof FlattenException)) {
return false;
}

// we are in fos rest context
if (!empty($context[Serializer::FOS_BUNDLE_SERIALIZATION_CONTEXT])) {
return true;
}

// we are in messenger context
if (!empty($context['messenger_serialization'])) { // Serializer::MESSENGER_SERIALIZATION_CONTEXT
return false;
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Validator\Constraints\NotBlank;

class CustomArgumentException extends \Exception
{
}

/**
* Controller to test serialization of various errors and exceptions.
*
Expand All @@ -38,6 +42,11 @@ public function invalidArgumentExceptionAction()
throw new \InvalidArgumentException('Invalid argument given.');
}

public function customExceptionAction()
{
throw new CustomArgumentException('Custom exception');
}

/**
* @View
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ test_serializer_error_invalid_form:
path: /serializer-error/invalid-form.{_format}
defaults: { _controller: FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\SerializerErrorController::invalidFormAction }

test_custom_exception_serializer:
path: /serializer-error/custom-argument-exception.{_format}
defaults: { _controller: FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\SerializerErrorController::customExceptionAction }

# Must be defined before test_version
test_version2:
resource: FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\Version2Controller
Expand Down
18 changes: 18 additions & 0 deletions Tests/Functional/SerializerErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class SerializerErrorTest extends WebTestCase
{
public static function tearDownAfterClass(): void
{
self::deleteTmpDir('CustomFlattenExceptionNormalizer');
self::deleteTmpDir('FlattenExceptionHandlerLegacyFormat');
self::deleteTmpDir('FlattenExceptionHandlerRfc7807Format');
self::deleteTmpDir('FlattenExceptionNormalizerLegacyFormat');
Expand Down Expand Up @@ -121,6 +122,23 @@ public function testSerializeExceptionCodeMappedToResponseStatusCodeJsonUsingErr
$this->assertEquals(json_encode($expectedJson), $client->getResponse()->getContent());
}

public function testCustomExceptionSerialization()
{
if (!class_exists(SerializerErrorRenderer::class)) {
$this->markTestSkipped();
}

$this->iniSet('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul');

$client = $this->createClient(['test_case' => 'CustomFlattenExceptionNormalizer', 'debug' => false]);
$client->request('GET', '/serializer-error/custom-argument-exception.json');

$this->assertEquals(
'{"code":409,"message":"Conflict"}',
$client->getResponse()->getContent()
);
}

public function serializeExceptionCodeMappedToResponseStatusCodeJsonProvider(): array
{
return [
Expand Down
17 changes: 17 additions & 0 deletions Tests/Functional/app/CustomFlattenExceptionNormalizer/bundles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

return [
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \FOS\RestBundle\FOSRestBundle(),
new \JMS\SerializerBundle\JMSSerializerBundle(),
new \FOS\RestBundle\Tests\Functional\Bundle\TestBundle\TestBundle(),
];
25 changes: 25 additions & 0 deletions Tests/Functional/app/CustomFlattenExceptionNormalizer/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
imports:
- { resource: ../config/default.yml }

framework:
serializer: true

fos_rest:
exception:
codes:
'FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\CustomArgumentException': 409
enabled: true
map_exception_codes: true
exception_listener: false
serialize_exceptions: false
flatten_exception_format: 'legacy'
serializer_error_renderer: false
serializer:
serialize_null: true
body_listener:
enabled: true
routing_loader: false
view:
formats:
json: true
csv: true

0 comments on commit cb75a41

Please sign in to comment.