Skip to content

[Serializer] Deprecate using datetime construct as fallback on default format mismatch #47095

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

Closed

Conversation

ogizanagi
Copy link
Contributor

@ogizanagi ogizanagi commented Jul 28, 2022

Q A
Branch? 6.3
Bug fix? no
New feature? no
Deprecations? yes
Tickets Relates to #43329
License MIT
Doc PR N/A

This PR is a follow-up to #43329 and aims to enforce the configured default format is always respected, instead of silently fallback to the \DateTime & \DateTimeImmutable constructors.
As of today, the only way to enforce this format is to set it locally through the context.

For now, the PR just adds the deprecation in case the fallback path is reached. One could argue this might be enough, since not respecting the expected format as an end user of an API might be considered as an issue in the first place.

Still:

  1. Do we need a better upgrade path?
    e.g:

     \DateTimeNormalizer::USE_DATETIME_CONSTRUCT_AS_FALLBACK => false,

    and trigger a deprec if this flag is not set. Default 6.2: true, 7.0: false

  2. Do we want to keep the ability to use the datetime constructs as fallbacks?
    e.g:

     \DateTimeNormalizer::USE_DATETIME_CONSTRUCT_AS_FALLBACK => false,

    Then, this behavior should be consistent whenever a default format or a context one is used (there is no fallback right now if set from the context)

  3. Do we want to keep the datetime constructs call whenever the default format is null?
    Then, one should be able to "unset" the default format by passing in the context:

    $this->normalizer->denormalize('now', \DateTime::class, null, [\DateTimeNormalizer::FORMAT_KEY => null]);

    (currently fallbacks to the default format) or use a special value to do so (php-datetime-string ?).

@nicolas-grekas
Copy link
Member

(rebase unlocked)

@ogizanagi ogizanagi force-pushed the serializer-deprec-dt-construct-fallback branch from 88c8ff8 to 55be0a1 Compare July 28, 2022 13:43
@@ -113,6 +113,8 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
if (false !== $object) {
return $object;
}

trigger_deprecation('symfony/serializer', '6.2', 'Relying on a datetime constructor as a fallback when using a specific default date format (`datetime_format`) for the DateTimeNormalizer is deprecated. Respect the "%s" default format.', $defaultDateTimeFormat);
Copy link
Member

Choose a reason for hiding this comment

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

What will the code look like in 7.0? Can we add a hint as a comment here?

Copy link
Contributor Author

@ogizanagi ogizanagi Jul 29, 2022

Choose a reason for hiding this comment

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

Depending on how we answer the questions in PR description, In 7.0, the fallback path will be either totally removed or disabled by default.

Suggested change
trigger_deprecation('symfony/serializer', '6.2', 'Relying on a datetime constructor as a fallback when using a specific default date format (`datetime_format`) for the DateTimeNormalizer is deprecated. Respect the "%s" default format.', $defaultDateTimeFormat);
trigger_deprecation('symfony/serializer', '6.2', 'Relying on a datetime constructor as a fallback when using a specific default date format (`datetime_format`) for the DateTimeNormalizer is deprecated and will be removed in 7.0. Respect the "%s" default format.', $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'd got with 3 when format is auto.

@nicolas-grekas nicolas-grekas modified the milestones: 6.2, 6.3 Nov 5, 2022
@ogizanagi ogizanagi force-pushed the serializer-deprec-dt-construct-fallback branch 2 times, most recently from 3752548 to 8937432 Compare December 27, 2022 09:39
@ogizanagi
Copy link
Contributor Author

PR updated to add the auto format for denormalization.

@ogizanagi ogizanagi force-pushed the serializer-deprec-dt-construct-fallback branch from 8937432 to 8bcd45b Compare December 27, 2022 09:41
@ogizanagi ogizanagi force-pushed the serializer-deprec-dt-construct-fallback branch from 8bcd45b to 41a1d56 Compare April 18, 2023 08:31
@nicolas-grekas nicolas-grekas modified the milestones: 6.3, 6.4 May 23, 2023
@nicolas-grekas
Copy link
Member

This didn't get traction so I guess this isn't much desired. On my side at least I don't see why this would be an improvement. I'd prefer closing.

@fabpot
Copy link
Member

fabpot commented Aug 1, 2023

I think I agree with @nicolas-grekas here.

@fabpot fabpot closed this Aug 1, 2023
@ogizanagi ogizanagi deleted the serializer-deprec-dt-construct-fallback branch November 17, 2023 21:54
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.

4 participants