-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Improve denormalization of backed enums #50201
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
base: 7.4
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,6 @@ | |
|
||
namespace Symfony\Component\Serializer\Normalizer; | ||
|
||
use Symfony\Component\PropertyInfo\Type; | ||
use Symfony\Component\Serializer\Exception\InvalidArgumentException; | ||
use Symfony\Component\Serializer\Exception\NotNormalizableValueException; | ||
|
||
|
@@ -69,14 +68,17 @@ public function denormalize(mixed $data, string $type, string $format = null, ar | |
} | ||
} | ||
|
||
if (!\is_int($data) && !\is_string($data)) { | ||
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); | ||
$backingType = (new \ReflectionEnum($type))->getBackingType()->getName(); | ||
$givenType = \get_debug_type($data); | ||
|
||
if ($givenType !== $backingType) { | ||
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Data expected to be "%s", "%s" given. You should pass a value that can be parsed as an enumeration case of type %s.', $backingType, $givenType, $type), $data, [$backingType], $context['deserialization_path'] ?? null, true); | ||
} | ||
|
||
try { | ||
return $type::from($data); | ||
} catch (\ValueError $e) { | ||
throw new InvalidArgumentException('The data must belong to a backed enumeration of type '.$type); | ||
throw NotNormalizableValueException::createForUnexpectedDataType('The data must belong to a backed enumeration of type '.$type, $data, [$backingType], $context['deserialization_path'] ?? null, true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed with @nicolas-grekas here, it seems to revert the previous work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Throwing I first viewed #43047 (comment) The whole point of having it as an |
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.