You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
bug #49537 [Serializer] Unexpected value should throw UnexpectedValueException (ThomasTr)
This PR was merged into the 5.4 branch.
Discussion
----------
[Serializer] Unexpected value should throw UnexpectedValueException
| Q | A
| ------------- | ---
| Branch? | 5.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets | See my recently started discussion here: #49535
| License | MIT
| Doc PR | NA
If a DateTime field to be serialised has a data type other than string (e.g. a serialised DateTime as array), a TypeException is thrown. This exception cannot be caught by a try/catch around the serializer.
This fix throws a NotNormalizableValueException that appears cleanly at the corresponding field.
This is my first pull request, please be kind to me :)
Commits
-------
af34c52 [Serializer] Unexpected value should throw UnexpectedValueException
if (null === $data || (\is_string($data) &&'' === trim($data))) {
95
-
throw NotNormalizableValueException::createForUnexpectedDataType('The data is either an empty string or null, you should pass a string that can be parsed with the passed format or a valid DateTime string.', $data, [Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, true);
94
+
if (null === $data || !\is_string($data) ||'' === trim($data)) {
95
+
throw NotNormalizableValueException::createForUnexpectedDataType('The data is either not an string, an empty string, or null; you should pass a string that can be parsed with the passed format or a valid DateTime string.', $data, [Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, true);
$this->expectExceptionMessage('The data is either not an string, an empty string, or null; you should pass a string that can be parsed with the passed format or a valid DateTime string.');
$this->expectExceptionMessage('The data is either an empty string or null, you should pass a string that can be parsed with the passed format or a valid DateTime string.');
256
+
$this->expectExceptionMessage('The data is either not an string, an empty string, or null; you should pass a string that can be parsed with the passed format or a valid DateTime string.');
$this->expectExceptionMessage('The data is either an empty string or null, you should pass a string that can be parsed with the passed format or a valid DateTime string.');
263
+
$this->expectExceptionMessage('The data is either not an string, an empty string, or null; you should pass a string that can be parsed with the passed format or a valid DateTime string.');
$this->expectExceptionMessage('The data is either an empty string or null, you should pass a string that can be parsed with the passed format or a valid DateTime string.');
270
+
$this->expectExceptionMessage('The data is either not an string, an empty string, or null; you should pass a string that can be parsed with the passed format or a valid DateTime string.');
Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/SerializerTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -943,7 +943,7 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet
943
943
],
944
944
'pat
8EE5
h' => 'dateTime',
945
945
'useMessageForUser' => true,
946
-
'message' => 'The data is either an empty string or null, you should pass a string that can be parsed with the passed format or a valid DateTime string.',
946
+
'message' => 'The data is either not an string, an empty string, or null; you should pass a string that can be parsed with the passed format or a valid DateTime string.',
947
947
],
948
948
[
949
949
'currentType' => 'null',
@@ -952,7 +952,7 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet
952
952
],
953
953
'path' => 'dateTimeImmutable',
954
954
'useMessageForUser' => true,
955
-
'message' => 'The data is either an empty string or null, you should pass a string that can be parsed with the passed format or a valid DateTime string.',
955
+
'message' => 'The data is either not an string, an empty string, or null; you should pass a string that can be parsed with the passed format or a valid DateTime string.',
0 commit comments