Skip to content
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

Fix hydrating union types #8286

Merged
merged 6 commits into from
Apr 18, 2024

Conversation

PerryvanderMeer
Copy link
Contributor

@PerryvanderMeer PerryvanderMeer commented Apr 11, 2024

This PR is a follow-up on #8126, where some problems were introduced (see: #8126 (comment)).

Problem

Component properties cast to a union type like string|int or string|float are broken.
For example:

  • Setting (string) 'foo' to string|int $property is cast to (int) 0.
  • Setting (string) 'foo' to string|float $property is cast to (float) 0.00.

Cause

Any property (union) casted to int or float, was aggressively casted to respectively an integer or float.

Solution

We don't need to aggressively cast the passed values to an integer or float.
Casting is also already partially handled by PHP itself.
After this PR is merged, casting works as follows.

int $property

Input Output Behaviour
(null) (null) ✅ Unchanged
(int) 3 (int) 3 ✅ Unchanged
(float) 3.00 (int) 3 ✅ Unchanged
(float) 3.14 (int) 3 ✅ Unchanged
(string) '3' (int) 3 ✅ Unchanged
(string) '3.14' (int) 3 ✅ Unchanged

float $property

Input Output Behaviour
(null) (null) ✅ Unchanged
(int) 3 (float) 3.00 ✅ Unchanged
(float) 3.00 (float) 3.00 ✅ Unchanged
(float) 3.14 (float) 3.14 ✅ Unchanged
(string) '3' (float) 3.00 ✅ Unchanged
(string) '3.14' (float) 3.14 ✅ Unchanged

int|string $property

Input Output Behaviour
(null) (null) ✅ Unchanged
(int) 3 (int) 3 ✅ Unchanged
(float) 3.00 (int) 3 ✅ Unchanged
(float) 3.14 (int) 3 ✅ Unchanged
(string) '3' (int) 3 ✅ Unchanged
(string) '3.00' (int) 3 ✅ Unchanged
(string) '3.14' (string) '3.14' ⚠️ Before: (int) 3, can cause issues
(string) 'foo' (string) 'foo' ✅ Before: (int) 0, solves issue

float|string $property

Input Output Behaviour
(null) (null) ✅ Unchanged
(int) 3 (float) 3.00 ✅ Unchanged
(float) 3.00 (float) 3.00 ✅ Unchanged
(float) 3.14 (float) 3.14 ✅ Unchanged
(string) '3' (float) 3.00 ✅ Unchanged
(string) '3.00' (float) 3.00 ✅ Unchanged
(string) '3.14' (float) 3.14 ✅ Unchanged
(string) 'foo' (string) 'foo' ✅ Before: (float) 0.00, solves issue

I've also added some backwards compability for the old situation. The only rare situation in which some problems might occur, is when properties are cast to int|string and are set to a string-formatted float. Previously, the string was cast to an integer, now the actual string is set.

@calebporzio
Copy link
Collaborator

This is great. Thank you for the in-depth work, description, and tests. Love it.

@calebporzio calebporzio merged commit ed89372 into livewire:main Apr 18, 2024
5 checks passed
@PerryvanderMeer
Copy link
Contributor Author

@calebporzio Thanks! Can you please tag a new release with this included? This issue prevents our apps from updating from version 3.4.9.

@PerryvanderMeer PerryvanderMeer deleted the fix-hydrating-union-types branch April 18, 2024 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants