Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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