Skip to content

Commit b67cf57

Browse files
committed
Merge branch '5.4' into 6.0
* 5.4: [HttpFoundation] Prevent accepted rate limits with no remaining token to be preferred over denied ones Email image parts: regex for single closing quote [Serializer] Throw InvalidArgumentException if the data needed in the constructor doesn't belong to a backedEnum
2 parents 348bea6 + 3fc9afe commit b67cf57

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

Normalizer/BackedEnumNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
6060
try {
6161
return $type::from($data);
6262
} catch (\ValueError $e) {
63-
throw NotNormalizableValueException::createForUnexpectedDataType($e->getMessage(), $data, [Type::BUILTIN_TYPE_INT, Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, true, $e->getCode(), $e);
63+
throw new InvalidArgumentException('The data must belong to a backed enumeration of type '.$type);
6464
}
6565
}
6666

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Symfony\Component\Serializer\Tests\Fixtures;
4+
5+
use Symfony\Component\Serializer\Tests\Fixtures\StringBackedEnumDummy;
6+
7+
class DummyObjectWithEnumConstructor
8+
{
9+
public function __construct(public StringBackedEnumDummy $get)
10+
{
11+
}
12+
}

Tests/Normalizer/BackedEnumNormalizerTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,9 @@ public function testDenormalizeObjectThrowsException()
115115
*/
116116
public function testDenormalizeBadBackingValueThrowsException()
117117
{
118-
$this->expectException(NotNormalizableValueException::class);
119-
$this->expectExceptionMessage('"POST" is not a valid backing value for enum "'.StringBackedEnumDummy::class.'"');
118+
$this->expectException(InvalidArgumentException::class);
119+
$this->expectExceptionMessage('The data must belong to a backed enumeration of type '.StringBackedEnumDummy::class);
120+
120121
$this->normalizer->denormalize('POST', StringBackedEnumDummy::class);
121122
}
122123

Tests/SerializerTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
3737
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
3838
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
39+
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
3940
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
4041
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer;
4142
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
@@ -58,6 +59,7 @@
5859
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface;
5960
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberOne;
6061
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberTwo;
62+
use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumConstructor;
6163
use Symfony\Component\Serializer\Tests\Fixtures\FalseBuiltInDummy;
6264
use Symfony\Component\Serializer\Tests\Fixtures\NormalizableTraversableDummy;
6365
use Symfony\Component\Serializer\Tests\Fixtures\Php74Full;
@@ -1167,6 +1169,69 @@ public function testCollectDenormalizationErrorsWithConstructor(?ClassMetadataFa
11671169
$this->assertSame($expected, $exceptionsAsArray);
11681170
}
11691171

1172+
/**
1173+
* @requires PHP 8.1
1174+
*/
1175+
public function testCollectDenormalizationErrorsWithEnumConstructor()
1176+
{
1177+
$serializer = new Serializer(
1178+
[
1179+
new BackedEnumNormalizer(),
1180+
new ObjectNormalizer(),
1181+
],
1182+
['json' => new JsonEncoder()]
1183+
);
1184+
1185+
try {
1186+
$serializer->deserialize('{"invalid": "GET"}', DummyObjectWithEnumConstructor::class, 'json', [
1187+
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
1188+
]);
1189+
} catch (\Throwable $th) {
1190+
$this->assertInstanceOf(PartialDenormalizationException::class, $th);
1191+
}
1192+
1193+
$exceptionsAsArray = array_map(function (NotNormalizableValueException $e): array {
1194+
return [
1195+
'currentType' => $e->getCurrentType(),
1196+
'useMessageForUser' => $e->canUseMessageForUser(),
1197+
'message' => $e->getMessage(),
1198+
];
1199+
}, $th->getErrors());
1200+
1201+
$expected = [
1202+
[
1203+
'currentType' => 'array',
1204+
'useMessageForUser' => true,
1205+
'message' => 'Failed to create object because the class misses the "get" property.',
1206+
],
1207+
];
1208+
1209+
$this->assertSame($expected, $exceptionsAsArray);
1210+
}
1211+
1212+
/**
1213+
* @requires PHP 8.1
1214+
*/
1215+
public function testNoCollectDenormalizationErrorsWithWrongEnum()
1216+
{
1217+
$serializer = new Serializer(
1218+
[
1219+
new BackedEnumNormalizer(),
1220+
new ObjectNormalizer(),
1221+
],
1222+
['json' => new JsonEncoder()]
1223+
);
1224+
1225+
try {
1226+
$serializer->deserialize('{"get": "invalid"}', DummyObjectWithEnumConstructor::class, 'json', [
1227+
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
1228+
]);
1229+
} catch (\Throwable $th) {
1230+
$this->assertNotInstanceOf(PartialDenormalizationException::class, $th);
1231+
$this->assertInstanceOf(InvalidArgumentException::class, $th);
1232+
}
1233+
}
1234+
11701235
public function provideCollectDenormalizationErrors()
11711236
{
11721237
return [

0 commit comments

Comments
 (0)