Skip to content

[Serializer] Trigger deprecation when could not parse date with default format #58966

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

Open
wants to merge 2 commits into
base: 7.4
Choose a base branch
from

Conversation

alexndlm
Copy link
Contributor

@alexndlm alexndlm commented Nov 22, 2024

Q A
Branch? 7.4
Bug fix? no
New feature? no
Deprecations? yes
Issues
License MIT

Relates to #43329 (comment)

@alexndlm
Copy link
Contributor Author

@nicolas-grekas Could you have a look?

Copy link
Contributor

@mtarld mtarld left a comment

Choose a reason for hiding this comment

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

Could you update the Serializer's CHANGELOG.md file and the UPGRADE-7.3.md file as well?

@mtarld
Copy link
Contributor

mtarld commented Jan 13, 2025

Looks like the tests must be updated to not trigger the deprecation.

@@ -126,6 +126,8 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
if (false !== $object = $type::createFromFormat($defaultDateTimeFormat, $data, $timezone)) {
return $object;
}

trigger_deprecation('symfony/serializer', '7.3', \sprintf('A "%s" will be thrown when a date could not be parsed using the default format "%s".', NotNormalizableValueException::class, $defaultDateTimeFormat));
Copy link
Member

Choose a reason for hiding this comment

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

I think we should trigger a deprecation here only if the fallback manages to instantiate it. The case where the fallback throws an exception (already turned into a NotNormalizableValueException below) should not trigger a deprecation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I understand, in 8.0, the fallback should be removed, right?

Copy link
Member

Choose a reason for hiding this comment

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

yes. And that's my point: if the fallback also fails, you already have the same behavior than what would happen in 8.0 and we should not trigger a deprecation in that case as the behavior will not change. Try submitting foobar as value to reproduce that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have rechecked and now realise that fallback should not be removed in 8.0. Fallback should still be used when datetime_format is set to null.

Copy link
Member

Choose a reason for hiding this comment

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

My point is still not solved. You still trigger a deprecation for not normalizable values (passing abcdefgh as date for instance), while there is no behavior change for that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After removing return new $type($data, $timezone); (line 133/131), it will throw NotNormalizableValueException when we pass a valid date but the format does not match $defaultDateTimeFormat.

For example, if $defaultDateTimeFormat = 'Y-m-d H:i:s' and we pass 2025-03-10 it will work.

In Symfony 8, it should throw an exception.

As I understand, the behaviour will change.

Copy link
Member

@stof stof Jun 18, 2025

Choose a reason for hiding this comment

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

I'm not talking about the case of passing a valid date not matching the format.

Your current implementation is also triggering the deprecation when passing a totally invalid date. That's because you trigger the deprecation before trying to use the fallback, without considering whether that fallback succeeds or fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I finally understood you. Look again, please.

@alexndlm alexndlm force-pushed the feature/serializer-trigger-deprecation-when-could-not-parse-date-with-default-format branch from 66a1ede to 663eec5 Compare January 13, 2025 17:26
@alexndlm
Copy link
Contributor Author

@mtarld I have done with your comments.

@alexndlm alexndlm requested review from mtarld and stof January 13, 2025 17:43
@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
@alexndlm alexndlm force-pushed the feature/serializer-trigger-deprecation-when-could-not-parse-date-with-default-format branch from 6ef37ea to b6f425d Compare June 18, 2025 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants