Skip to content

[Serializer] Respect default context in DateTimeNormalizer::denormalize #43329

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

Merged
merged 1 commit into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ public function denormalize($data, $type, $format = null, array $context = [])
throw new NotNormalizableValueException(sprintf('Parsing datetime string "%s" using format "%s" resulted in %d errors: ', $data, $dateTimeFormat, $dateTimeErrors['error_count'])."\n".implode("\n", $this->formatDateTimeErrors($dateTimeErrors['errors'])));
}

$defaultDateTimeFormat = $this->defaultContext[self::FORMAT_KEY] ?? null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't you patch L97 to this instead?
$dateTimeFormat = $context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY] ?? null;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we allow createFromFormat from defaultContext[FORMAT_KEY] to fail due BC, but generally we can't assume the default format fits all payloads

Copy link
Contributor

@ogizanagi ogizanagi Feb 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we trigger a deprecation in case the fallback path is triggered, so we can remove it in Symfony 7+ to enforce the defaultContext[FORMAT_KEY] is honored?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Symfony 7 is released. Is it planned to remove it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet as far as I can see. The first step to remove something in the next major release would be to deprecate it in a minor release before.


if (null !== $defaultDateTimeFormat) {
$object = \DateTime::class === $type ? \DateTime::createFromFormat($defaultDateTimeFormat, $data, $timezone) : \DateTimeImmutable::createFromFormat($defaultDateTimeFormat, $data, $timezone);

if (false !== $object) {
return $object;
}
}

try {
return \DateTime::class === $type ? new \DateTime($data, $timezone) : new \DateTimeImmutable($data, $timezone);
} catch (\Exception $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,28 @@ public function testDenormalizeDateTimeStringWithSpacesUsingFormatPassedInContex
$this->normalizer->denormalize(' 2016.01.01 ', \DateTime::class, null, [DateTimeNormalizer::FORMAT_KEY => 'Y.m.d|']);
}

public function testDenormalizeDateTimeStringWithDefaultContextFormat()
{
$format = 'd/m/Y';
$string = '01/10/2018';

$normalizer = new DateTimeNormalizer([DateTimeNormalizer::FORMAT_KEY => $format]);
$denormalizedDate = $normalizer->denormalize($string, \DateTimeInterface::class);

$this->assertSame('01/10/2018', $denormalizedDate->format($format));
}

public function testDenormalizeDateTimeStringWithDefaultContextAllowsErrorFormat()
{
$format = 'd/m/Y'; // the default format
$string = '2020-01-01'; // the value which is in the wrong format, but is accepted because of `new \DateTime` in DateTimeNormalizer::denormalize

$normalizer = new DateTimeNormalizer([DateTimeNormalizer::FORMAT_KEY => $format]);
$denormalizedDate = $normalizer->denormalize($string, \DateTimeInterface::class);

$this->assertSame('2020-01-01', $denormalizedDate->format('Y-m-d'));
}

public function testDenormalizeFormatMismatchThrowsException()
{
$this->expectException(UnexpectedValueException::class);
Expand Down