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

feat(unions): Add support for simple arrays #1571

Merged
merged 2 commits into from
Dec 3, 2024

Conversation

scyzoryck
Copy link
Collaborator

Q A
Bug fix? yes
New feature? yes
Doc updated no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #1566
License MIT

Add support for array (de)serialization in Union types. Only basic one. For the future there might be needed some some refactoring as the code debugging is terrible here :D

@scyzoryck scyzoryck force-pushed the union-properties-array branch from 7d8dcba to 7a8b0e9 Compare November 17, 2024 12:33
@scyzoryck scyzoryck requested a review from goetas November 18, 2024 20:48
@scyzoryck
Copy link
Collaborator Author

@goetas do you have moment to take a look at it?

Copy link
Collaborator

@goetas goetas left a comment

Choose a reason for hiding this comment

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

code wise this looks good.

I see that unions are a rabbit hole. :(

in the non-unions, using @Type("array") is possible but discouraged, mainly because there is no easy way to define the content of the array. The type array works mainly because json has the knowledge of some types such as int or bool.

I can see that many people might want to have the ability to use array as type....

@@ -60,9 +60,9 @@ public function __construct(DriverInterface $delegate, ?ParserInterface $typePar
private function reorderTypes(array $types): array
{
uasort($types, static function ($a, $b) {
$order = ['null' => 0, 'true' => 1, 'false' => 2, 'bool' => 3, 'int' => 4, 'float' => 5, 'string' => 6];
$order = ['null' => 0, 'true' => 1, 'false' => 2, 'bool' => 3, 'int' => 4, 'float' => 5, 'array' => 6, 'string' => 7];
Copy link
Collaborator

Choose a reason for hiding this comment

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

the more I look at this the more I think that it would make sense to use the order used in the code (by looking at how the union types are declared) rather that having our own way of sorting.

is there a reason for not doing it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I see 2 risks:

  • the order might be forced by Code Formatters, so coding style might impact Serializer behavior. Not sure if such linter exists.
  • It might be confusing why int|string gives different result than string|int.
    Moved into Union Types - code/tests improvements #1576 as an idea for the future.

@scyzoryck
Copy link
Collaborator Author

In general yes, it is missing a lot of edge cases. But from the bright side, it looks like our community got involved more into the package life 🥳

@scyzoryck scyzoryck merged commit b4285f4 into schmittjoh:master Dec 3, 2024
19 of 20 checks passed
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.

2 participants