8000 [Serializer] Unexpected value should throw UnexpectedValueException · symfony/symfony@a37127f · GitHub
[go: up one dir, main page]

Skip to content

Commit a37127f

Browse files
committed
[Serializer] Unexpected value should throw UnexpectedValueException
1 parent 51b859f commit a37127f

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ public function denormalize($data, string $type, string $format = null, array $c
9191
$dateTimeFormat = $context[self::FORMAT_KEY] ?? null;
9292
$timezone = $this->getTimezone($context);
9393

94-
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);
9696
}
9797

9898
if (null !== $dateTimeFormat) {
@@ -110,10 +110,14 @@ public function denormalize($data, string $type, string $format = null, array $c
110110
$defaultDateTimeFormat = $this->defaultContext[self::FORMAT_KEY] ?? null;
111111

112112
if (null !== $defaultDateTimeFormat) {
113-
$object = \DateTime::class === $type ? \DateTime::createFromFormat($defaultDateTimeFormat, $data, $timezone) : \DateTimeImmutable::createFromFormat($defaultDateTimeFormat, $data, $timezone);
114-
115-
if (false !== $object) {
116-
return $object;
113+
try {
114+
$object = \DateTime::class === $type ? \DateTime::createFromFormat($defaultDateTimeFormat, $data, $timezone) : \DateTimeImmutable::createFromFormat($defaultDateTimeFormat, $data, $timezone);
115+
116+
if (false !== $object) {
117+
return $object;
118+
}
119+
} catch (\Exception $e) {
120+
throw NotNormalizableValueException::createForUnexpectedDataType($e->getMessage(), $data, [Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, false, $e->getCode(), $e);
117121
}
118122
}
119123

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,24 +243,31 @@ public function testDenormalizeInvalidDataThrowsException()
243243
$this->normalizer->denormalize('invalid date', \DateTimeInterface::class);
244244
}
245245

246+
public function testDenormalizeWrongTypeThrowsException()
247+
{
248+
$this->expectException(UnexpectedValueException::class);
249+
$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.');
250+
$this->normalizer->denormalize(['date' => '2023-03-03 00:00:00.000000', 'timezone_type' => 1, 'timezone' => '+01:00'], \DateTimeInterface::class);
251+
}
252+
246253
public function testDenormalizeNullThrowsException()
247254
{
248255
$this->expectException(UnexpectedValueException::class);
249-
$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.');
250257
$this->normalizer->denormalize(null, \DateTimeInterface::class);
251258
}
252259

253260
public function testDenormalizeEmptyStringThrowsException()
254261
{
255262
$this->expectException(UnexpectedValueException::class);
256-
$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.');
257264
$this->normalizer->denormalize('', \DateTimeInterface::class);
258265
}
259266

260267
public function testDenormalizeStringWithSpacesOnlyThrowsAnException()
261268
{
262269
$this->expectException(UnexpectedValueException::class);
263-
$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.');
264271
$this->normalizer->denormalize(' ', \DateTimeInterface::class);
265272
}
266273

0 commit comments

Comments
 (0)
0