schmittjoh / serializer

Library for (de-)serializing data of any complexity (supports JSON, and XML)
http://jmsyst.com/libs/serializer
MIT License
2.32k stars 586 forks source link

Union types deserialisation #1546

Closed idbentley closed 4 months ago

idbentley commented 5 months ago
Q A
Bug fix? no
New feature? yes
Doc updated no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #1502
License MIT

This is just a minor improvement on https://github.com/schmittjoh/serializer/pull/1504 - addressing @goetas feedback, improving type detection and tests.

scyzoryck commented 4 months ago

Thanks for taking up this topic. Unfortunately I'm super short in time recently :(

It looks like there was few issues found by the CI pipeline like:

Error: Class 'JMS\Serializer\Tests\Fixtures\DocBlockType\UnionTypedDocBlockProperty' not found

Also, please use vendor/bin/phpcbf to format the code :) I will take a deeper look tomorrow.

idbentley commented 4 months ago

@scyzoryck I completely understand. I need union support for a project I'm working on, so I'm happy to push this forward. Thanks for taking the time to work with me.

If you can kickoff the workflows again (I don't seem to be able to), I think I've fixed those issues.

idbentley commented 4 months ago

I will look into these phpstan issues tomorrow.

idbentley commented 4 months ago

Hey @scyzoryck I addressed the phpstan issues (sorry, I'm embarrassed I didn't pay more attention to the contributor guide).

However, there were a number of phpstan related issues on a file that is not touched in this pull request: src/Metadata/Driver/DoctrineTypeDriver.php. I can address those too, but I left them out because they seem unrelated to the other code changes. Let me know if you'd like me to apply that patch before we run through CI again.

idbentley commented 4 months ago

Hiya!

I added a commit to only register the UnionHandler on PHPVersion > 8.

Not sure how to get the PHPStan to ignore the | usage in UnionHandler though. It's only used in the return type of the determineType function - as a test I tried to use ?string instead of string|null?

It's not clear to me what the Error: Ignored error pattern #Constructor of class JMS\\Serializer\\Annotation\\.*? has an unused parameter# was not matched in reported errors. error reported by PHPStan means (that error does appear for me locally in master).

idbentley commented 4 months ago

Seems like the remaining CI issues are in the DoctrineTypeDriver.php. Let me know if you need anything else from me!

idbentley commented 4 months ago

@scyzoryck Yep, I think we've addressed all issues. I'm happy for you to merge when ready!

scyzoryck commented 4 months ago

@idbentley thanks for contribution! You are welcome with the next Pull Requests! :) Could you test it with your projects & dev version of the package, please? :)

Best, Marcin

goetas commented 4 months ago

it would be great to have this handler integrated in the bundle as well

idbentley commented 4 months ago

@goetas I'm currently working on a PR to expand this functionality to support Objects, arrays, non primitive data types. I thought it would make sense to get it into documentation and any build targets once that PR is ready as well.

goetas commented 4 months ago

@idbentley I have talked a little with @scyzoryck about it, deserializing objects used in unions is certainly a good thing to have but I do not have any idea on how to make it work.... how to decide to which class to map an arbitrary data structure? do you have any idea on how to do so?

simPod commented 4 months ago

@idbentley this crashes here with Class "union" does not exist:

image

It calls MetadataFactory::getClassHierarchy() with 'union' input.

calling serialize with instance of

final class Foo
{
    public function __construct(
        public A|B $union,
    ) {
idbentley commented 4 months ago

@simPod that's definitely a bad way of handling the issue, and I'm gonna look into more graceful failure. However, I think your main problem is that there is currently only support for unions of primitives at this time. See #1549 to follow progress on my attempt at unions of objects.

simPod commented 4 months ago

Yup, it should ignore unsupported cases. Good luck!

On Fri, Jul 19, 2024, 17:35 Ian Bentley @.***> wrote:

@simPod https://github.com/simPod that's definitely a bad way of handling the issue, and I'm gonna look into more graceful failure. However, I think your main problem is that there is currently only support for unions of primitives at this time. See #1549 https://github.com/schmittjoh/serializer/pull/1549 to follow progress on my attempt at unions of objects.

— Reply to this email directly, view it on GitHub https://github.com/schmittjoh/serializer/pull/1546#issuecomment-2239474013, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACQAJMTPSFZVE3TR5EV3STZNEW43AVCNFSM6AAAAABKJYMCYGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMZZGQ3TIMBRGM . You are receiving this because you were mentioned.Message ID: @.***>