Giter VIP home page Giter VIP logo

Comments (5)

scyzoryck avatar scyzoryck commented on August 15, 2024 2

Hello!

Looking at the code it looks like as of 3.26 version this package has limited support.

  • It does not work with Type annotation - works only with typed properties
  • It does not work with deserialisation.
    To solve your problem I would suggest:
  1. Create some fake type like:
class ClassWithUnionType
{
    #[\JMS\Serializer\Annotation\Type('expandable<ComplexType>')]
    public string|array $propertyUnderTest;
}
  1. Register custom handler for your type that will check if your data is a string or array of objects. Inside you will get access to DeserializationVisitorInterface, and type, so you will be able to deserialise it to array of objects or to string.

from serializer.

dgafka avatar dgafka commented on August 15, 2024

Hello,
Serialization should work in this scenario, as JMS will know what type of reference is serialized object holding.
However for the deserialization JMS can't know which type it should pick for deserialization, therefore it will fail.

You can set up Type Annotation to state what kind of type it should deserialize too however.

from serializer.

Anticom avatar Anticom commented on August 15, 2024

Hi, thanks for the quick reply!

You can set up Type Annotation to state what kind of type it should deserialize too however.

I don't quite understand how I'm supposed to declare that union using type annotations other than what I've tried. Can you please elaborate on this?


Background:

I'm working on an API client for a 3rd party vendor API I don't have any control over.
They have implemented a mechanism, that allows you to "expand" certain fields in the response. If those fields are not expanded, they have a string value. If they are expanded they contain an array of complex types.

Example

Request:

GET /api/foo

Response:

{
    "bar": "bar is collapsed, you can expand it using ?expand=bar"
}

Request:

GET /api/foo?expand=bar

Response:

{
    "bar": [
        { "id": 1, "message": "Now suddenly" },
        { "id": 2, "message": "bar is an array" },
        { "id": 3, "message": "of objects" },
    ]
}

Unfortunately I can't solve this by having just a second method on my client since there's multiple fields that can be "expanded" that way.

To be precise we're talking about 6 fields.
That would add up to 2^6=64 possible combinations of whether to expand certain fields or not which is unfeasible bloat and complexity in the client implementation.

from serializer.

Anticom avatar Anticom commented on August 15, 2024

Hi!

Sorry for the late reply.
What I don't understand is how I can delegate to other handlers (or whatever the correct terminology is here).

Currently I'm declaring the expandable types as #[Type('expandable<array<ComplexType>>')] which is giving me the type argument in my handler fn with the following shape (as expected):

$type = [
    'name' => 'expandable',
    'params' => [
        [
            'name' => 'array',
            'params' => [
                [
                    'name' => 'ComplexType',
                    'params' => [],
                ],
            ],
        ],
    ],
];

Now what I'd like to do is (pseudo code):

class ExpandableHandler
{
    // ...

    public function deserializeExpandableFromJson(JsonDeserializationVisitor $visitor, $expandableData, array $type, Context $context)
    {
        // since in the collapsed state, $expandableData is guaranteed to be a string, we can return it without further processing
        if ($this->isCollapsed($expandableData)) {
            return $expandableData;
        }

        // delegate back with an unwrapped type
        return $this->whatMethodToCallHere($visitor, $expandableData, $type['params'][0], $context);
        //                                                            ^
        //                                                            Basically "unwrap" the expandable type here
    }
}

Aside: The reason I'm declaring the type with an explicit array is, that I've found out that certain expandable fields won't expand into an array of complex types but a singular complex type instead.

from serializer.

Anticom avatar Anticom commented on August 15, 2024

Nevermind, I've figured it out looking at the EnumHandler.php

Thanks for the support guys!

from serializer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.