symfony icon indicating copy to clipboard operation
symfony copied to clipboard

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

Open ogizanagi opened this issue 3 years ago • 2 comments

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 https://github.com/symfony/symfony/pull/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 ?).

ogizanagi avatar Jul 28 '22 10:07 ogizanagi

(rebase unlocked)

nicolas-grekas avatar Jul 28 '22 12:07 nicolas-grekas

PR updated to add the auto format for denormalization.

ogizanagi avatar Dec 27 '22 09:12 ogizanagi

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.

nicolas-grekas avatar Aug 01 '23 13:08 nicolas-grekas

I think I agree with @nicolas-grekas here.

fabpot avatar Aug 01 '23 15:08 fabpot