8000 [Serializer] Fix decoding attribute that contains many digits by vlastv · Pull Request #22333 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Fix decoding attribute that contains many digits #22333

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
Closed

[Serializer] Fix decoding attribute that contains many digits #22333

wants to merge 3 commits into from

Conversation

vlastv
Copy link
Contributor
@vlastv vlastv commented Apr 7, 2017
Q A
Branch? 2.7
Bug fix? yes
New feature? no
BC breaks? yes
Deprecations? no
Tests pass? yes
Fixed tickets #22329
License MIT

@nicolas-grekas nicolas-grekas added this to the 2.7 milestone Apr 7, 2017
} else {
$data['@'.$attr->nodeName] = $attr->nodeValue;
}
$data['@'.$attr->nodeName] = $attr->nodeValue;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a BC break in case someone relies on the type of the decoded data (this could happen easily with PHP7 et strict types).

Maybe you could check if the cast to int loses information and keep a string only in this 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.

@jvasseur I changed in PR description BC break flag.
If checking possibility cast, then only positive integer will return as int and all others as string.
I think this behavior is wrong.

Copy link
Contributor

Choose a reason for hiding this comment

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

https://3v4l.org/fRfCH

'-321312' is properly converted to -321312.

Copy link
Contributor Author
@vlastv vlastv Apr 7, 2017

Choose a reason for hiding this comment

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

@sstok this is a BC break, earlier would return as a string ctype_digit('-321312') === false

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My comment about positive integer actual only if need save BC.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

on different data in attribute you get different type, in PHP 7 with strict_types=1 this can create exception;

Copy link
Contributor

Choose a reason for hiding this comment

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

-321312 would have been converted to a string. Which technically is a bug, the double cast checking ensures it's converted to an integer ONLY when the actual result would not change.

(string) ((int)'-321312') === '-321312'.
https://3v4l.org/mvlQN

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sstok method must return only one data type

Copy link
Contributor

Choose a reason for hiding this comment

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

This method always returns an array 😛 the values of the array vary.

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 propose to open a second PR with your implementation.
You will also have a break BC

@vlastv vlastv changed the title Fix decoding attribute that contains many digits [Serializer] Fix decoding attribute that contains many digits Apr 7, 2017
@dunglas
Copy link
Member
dunglas commented Apr 19, 2017

I suggest to return a float instead of an int only for large numbers. It will be consistent with the behavior of the JSON encoder behavior: https://3v4l.org/O5gST

@dunglas
Copy link
Member
dunglas commented Apr 19, 2017

By the way the current handling of negative integers is faulty and should be considered as a bug.

@vlastv
Copy link
Contributor Author
vlastv commented Apr 19, 2017

@dunglas we can not know whether a string is a number in fact, or it is a random circumstance.
Suppose that as a result of base64_encode, a line containing only numbers ...

fabpot added a commit that referenced this pull request Apr 23, 2017
…s handling (dunglas)

This PR was merged into the 2.7 branch.

Discussion
----------

[Serializer] XmlEncoder: fix negative int and large numbers handling

| Q             | A
| ------------- | ---
| Branch?       | 2.7 <!-- see comment below -->
| Bug fix?      | yes
| New feature?  | no <!-- don't forget updating src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget updating UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | #22329, #22333 <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | n/a

Alternative to #22333.

* Negative integers are now handled
* Float are now handled
* Large numbers are converted to float (as the `JsonEncoder` and native PHP functions like `ceil` do)

@vlastv, I've adapted your test. Can you check if it fixes your problem?

Commits
-------

1eeadb0 [Serializer] XmlEncoder: fix negative int and large numbers handling
@fabpot fabpot closed this Apr 23, 2017
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.

7 participants
0