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

Error deserializing a map of (nullable) objects #762

Closed
marcospassos opened this issue May 9, 2017 · 4 comments
Closed

Error deserializing a map of (nullable) objects #762

marcospassos opened this issue May 9, 2017 · 4 comments

Comments

@marcospassos
Copy link
Contributor

marcospassos commented May 9, 2017

Hi folks!

We have a use case here where we need to deserialize an array<string,User> where a user can be null. Furthermore, User has a couple of subclasses. Whenever we try to deserialize such map, we got the following error:

The discriminator field name "type" for base-class "Acme\User" was not found in input data.

Is this the expected behavior?

@goetas
Copy link
Collaborator

goetas commented May 9, 2017

actually yes. if your type is array<string,User> then all the instances inside the array have to be User (this means having a valid type field). array<string,User|null> is not a supported feature.

@goetas goetas closed this as completed May 9, 2017
@marcospassos
Copy link
Contributor Author

Is there any workaround?

@goetas
Copy link
Collaborator

goetas commented May 9, 2017

you can specify the array as:array<string,NullableVirutalUser> and add a custom type handler:

class NullableVirutalUserHandler implements SubscribingHandlerInterface
{
    public static function getSubscribingMethods()
    {
        $methods = array();
            $methods[] = array(
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
                'type' => 'NullableVirutalUser',
                'format' => 'json',
                'method' => 'deserializeNullableUser',
            );

        return $methods;
    }

    public function deserializeNullableUser(VisitorInterface $visitor, $data, array $type, Context $context)
    {
        if (!$data) { // null
            return  null;
        } else {
          return  $context->accpet($data, ['name'=> 'User', 'params' => []], $context);
        }
    }
}

maybe the syntax is no 100% correct, but hope you get the idea

@scaytrase
Copy link
Contributor

@goetas hello, will this be supported at some point?

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

No branches or pull requests

3 participants