Skip to content

Commit 6212e86

Browse files
author
allison guilhem
committed
[Serializer] Return null and handle as InvalidArgumentException instead of ValueError if the data doesn't belong to a backed enum.
1 parent f7a2c37 commit 6212e86

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/Symfony/Component/Serializer/Normalizer/BackedEnumNormalizer.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ public function denormalize($data, $type, $format = null, array $context = [])
6161
throw NotNormalizableValueException::createForUnexpectedDataType('The data is neither an integer nor a string, you should pass an integer or a string that can be parsed as an enumeration case of type '.$type.'.', $data, [Type::BUILTIN_TYPE_INT, Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, true);
6262
}
6363

64-
try {
65-
return $type::from($data);
66-
} catch (\ValueError $e) {
67-
throw NotNormalizableValueException::createForUnexpectedDataType($e->getMessage(), $data, [Type::BUILTIN_TYPE_INT, Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, true, $e->getCode(), $e);
64+
$matched = $type::tryfrom($data);
65+
if (!$matched) {
66+
throw new InvalidArgumentException('The data must belong to a backed enumeration.');
6867
}
68+
69+
return $matched;
6970
}
7071

7172
/**

src/Symfony/Component/Serializer/Tests/Normalizer/BackedEnumNormalizerTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ 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);
120119
$this->normalizer->denormalize('POST', StringBackedEnumDummy::class);
121120
}
122121

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy