Skip to content

cast integer to string in case date time format is „U“ #50272

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
wants to merge 3 commits into from

Conversation

PercyDwn
Copy link

@PercyDwn PercyDwn commented May 9, 2023

In case the date time format is „U“, the data comes as a integer (timestamp in seconds). To keep the „data is string“-condition, the integer is casted to a string.

Q A
Branch? 6.3
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #50236
License MIT
Doc PR

Fixes DateTimeNormalizer::denormalize throwing a NotNormalizableValueException when DateTimeFormat is "U" and data is passed as an integer.
The Exception should not be thrown, as data being an integer is valid for a timestamp.
My solution is to check if $dateTImeFormat is "U" and $data is an integer. In that case the integer is casted to a string.
This way, the condition that $data is a string does not need to be changed.

In case the date time format is „U“, the data comes as a integer (timestamp in seconds).
To keep the „data is string“-condition, the integer is casted to a string.
@PercyDwn PercyDwn requested a review from dunglas as a code owner May 9, 2023 08:37
@carsonbot carsonbot added this to the 6.3 milestone May 9, 2023
@carsonbot
Copy link

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:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.3 branch.

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!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@carsonbot
Copy link

Hey!

Thanks for your PR. You are targeting branch "6.3" but it seems your PR description refers to branch "6.3 (?)".
Could you update the PR description or change target branch? This helps core maintainers a lot.

Cheers!

Carsonbot

@PercyDwn PercyDwn marked this pull request as draft May 9, 2023 08:42
@stof
Copy link
Member

stof commented May 9, 2023

I'd rather change the type check to also allow integers when using U instead of casting the value before the type check.

@stof
Copy link
Member

stof commented May 9, 2023

And this needs to have tests covering it to prevent regressions

@nicolas-grekas
Copy link
Member

We might want to account for U.u also.
Please use is_int instead of gettype while updating :)

@PercyDwn
Copy link
Author

PercyDwn commented May 9, 2023

Thank you for the feedback, @stof and @nicolas-grekas.

Is it the better solution to allow integers and floats for U and U.u?
In the PHP documentation the datetime for createFromFormat is typed as a string:

public static DateTimeImmutable::createFromFormat(string $format, string $datetime, ?[DateTimeZone] $timezone = null): [DateTimeImmutable]|false

With PHP 8.1 following code works just fine (does not return false)

$object = DateTimeImmutable::createFromFormat("U", 1683635480);
$object = DateTimeImmutable::createFromFormat("U.u", 1683635480.111111);

but could this behavior change in the future?

@stof
Copy link
Member

stof commented May 9, 2023

@PercyDwn My guess is that this code would not work fine with strict types.

@stof
Copy link
Member

stof commented May 9, 2023

Is it the better solution to allow integers and floats for U and U.u?

IMO, yes

PercyDwn added 2 commits May 9, 2023 16:14
$data can come as an integer, when date time format is „U“. Updated type check, so in that case no exception is thrown.
@PercyDwn
Copy link
Author

PercyDwn commented May 9, 2023

Okay so I updated the type check to allow integers when dateTimeFormat is "U".
But I ignored "U.u" as writing a test for it was a total mess. I tried allowing floats with "U.u", but i have the feeling that in such a case the data would come as a string, not as a float. I was not able to write a working test where the time (including milliseconds) came as a float and was identical with the resulting denormalized date.

Some of the github tests are failing, but I am not quite sure why.

@nicolas-grekas
Copy link
Member

Fixed by #50251

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.

[Serializer] DateTimeNormalizer throws NotNormalizableValueException if using datetime_format U
4 participants