-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[Serializer] Allow forcing timezone in DateTimeNormalizer during denormalization
#60153
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
Conversation
|
Hey! I see that this is your first PR. That is great! Welcome! Symfony has a contribution guide which I suggest you to read. In short:
Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change. When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor! I am going to sit back now and wait for the reviews. Cheers! Carsonbot |
|
Possibility of lowering complexity: There are currently 2 similar blocks of code for I was hesistant to do this immediately because i have no idea why there is different error handling for the context $dateTimeFormat. |
DateTimeNormalizer during denormalization
src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php
Outdated
Show resolved
Hide resolved
|
Is there anything I can do to help this being merged in Symfony 7.4? |
551c4f8 to
8b9a4f9
Compare
src/Symfony/Component/Serializer/Context/Normalizer/DateTimeNormalizerContextBuilder.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php
Outdated
Show resolved
Hide resolved
e95ddf0 to
dacfe45
Compare
DateTimeNormalizer during denormalizationDateTimeNormalizer during denormalization
|
@nicolas-grekas Thanks for the feedback. The branch is now up-to-date again with 7.4. There are 3 failings tests but I don't see the relation to my changes though. |
nicolas-grekas
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I almost forgot: Mapping/Loader/schema/serialization.schema.json needs also an update
| * @throws InvalidArgumentException | ||
| */ | ||
| public function withTimezone(\DateTimeZone|string|null $timezone): static | ||
| public function withTimezone(\DateTimeZone|string|null $timezone, ?bool $force = null): static |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| public function withTimezone(\DateTimeZone|string|null $timezone, ?bool $force = null): static | |
| public function withTimezone(\DateTimeZone|string|null $timezone, bool $force = false): static |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about setting it to true|false but in the implementation there's a "default context" and given context. Basicly you could set default context to ->withTimezone(null, true), and in your custom context ->withTimezone('UTC, null). In this way for every context the timezone will be forced, but per serializer context you can enforce different timezones.
src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php
Outdated
Show resolved
Hide resolved
nicolas-grekas
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 nits and GTM, thanks
src/Symfony/Component/Serializer/Context/Normalizer/DateTimeNormalizerContextBuilder.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/Mapping/Loader/schema/serialization.schema.json
Outdated
Show resolved
Hide resolved
be9608e to
bd1a4d0
Compare
|
Thank you @frankdekker. |
By default the
DateTimeNormalizerwill denormalize timestamps and date strings that include a timezone toDateTimeobjects with respective timezone UTC or the timezone from the date string. Even with the context keyTIMEZONE_KEYset, the timezone from the input superceeds the context timezone.This PR allows to set
PRESERVE_CONTEXT_TIMEZONE_KEYcontext totrue(default:false), which in combination withTIMEZONE_KEYwill set the Timezone of the denormalized DateTimes to the given timezone. The timezone from the input will be overwritten.Documentation
symfony/symfony-docs#20860