Skip to content

[Serializer] Throw InvalidArgumentException if the data needed in the constructor doesn't belong to a backedEnum #47128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Serializer] Throw InvalidArgumentException if the data needed in the…
… constructor doesn't belong to a backedEnum
  • Loading branch information
allison guilhem authored and alli83 committed Aug 18, 2022
commit c30f057db29a8fcc819b71adf390532085b10c9f
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function denormalize($data, string $type, string $format = null, array $c
try {
return $type::from($data);
} catch (\ValueError $e) {
throw NotNormalizableValueException::createForUnexpectedDataType($e->getMessage(), $data, [Type::BUILTIN_TYPE_INT, Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, true, $e->getCode(), $e);
throw new InvalidArgumentException('The data must belong to a backed enumeration of type '.$type);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Symfony\Component\Serializer\Tests\Fixtures;

use Symfony\Component\Serializer\Tests\Fixtures\StringBackedEnumDummy;

class DummyObjectWithEnumConstructor
{
public function __construct(public StringBackedEnumDummy $get)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ public function testDenormalizeObjectThrowsException()
*/
public function testDenormalizeBadBackingValueThrowsException()
{
$this->expectException(NotNormalizableValueException::class);
$this->expectExceptionMessage('"POST" is not a valid backing value for enum "'.StringBackedEnumDummy::class.'"');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The data must belong to a backed enumeration of type '.StringBackedEnumDummy::class);

$this->normalizer->denormalize('POST', StringBackedEnumDummy::class);
}

Expand Down
65 changes: 65 additions & 0 deletions src/Symfony/Component/Serializer/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
Expand All @@ -58,6 +59,7 @@
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface;
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberOne;
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberTwo;
use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumConstructor;
use Symfony\Component\Serializer\Tests\Fixtures\FalseBuiltInDummy;
use Symfony\Component\Serializer\Tests\Fixtures\NormalizableTraversableDummy;
use Symfony\Component\Serializer\Tests\Fixtures\Php74Full;
Expand Down Expand Up @@ -1173,6 +1175,69 @@ public function testCollectDenormalizationErrorsWithConstructor(?ClassMetadataFa
$this->assertSame($expected, $exceptionsAsArray);
}

/**
* @requires PHP 8.1
*/
public function testCollectDenormalizationErrorsWithEnumConstructor()
{
$serializer = new Serializer(
[
new BackedEnumNormalizer(),
new ObjectNormalizer(),
],
['json' => new JsonEncoder()]
);

try {
$serializer->deserialize('{"invalid": "GET"}', DummyObjectWithEnumConstructor::class, 'json', [
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
]);
} catch (\Throwable $th) {
$this->assertInstanceOf(PartialDenormalizationException::class, $th);
}

$exceptionsAsArray = array_map(function (NotNormalizableValueException $e): array {
return [
'currentType' => $e->getCurrentType(),
'useMessageForUser' => $e->canUseMessageForUser(),
'message' => $e->getMessage(),
];
}, $th->getErrors());

$expected = [
[
'currentType' => 'array',
'useMessageForUser' => true,
'message' => 'Failed to create object because the class misses the "get" property.',
],
];

$this->assertSame($expected, $exceptionsAsArray);
}

/**
* @requires PHP 8.1
*/
public function testNoCollectDenormalizationErrorsWithWrongEnum()
{
$serializer = new Serializer(
[
new BackedEnumNormalizer(),
new ObjectNormalizer(),
],
['json' => new JsonEncoder()]
);

try {
$serializer->deserialize('{"get": "invalid"}', DummyObjectWithEnumConstructor::class, 'json', [
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
]);
} catch (\Throwable $th) {
$this->assertNotInstanceOf(PartialDenormalizationException::class, $th);
$this->assertInstanceOf(InvalidArgumentException::class, $th);
}
}

public function provideCollectDenormalizationErrors()
{
return [
Expand Down