laravel-serializer icon indicating copy to clipboard operation
laravel-serializer copied to clipboard

Empty output when serializing

Open gildebrand opened this issue 6 years ago • 1 comments

I've added this repo to my Laravel 5.6 installation. The output of the code below is {"_type":"test_user"}, so the fields are missing. I've created a small example, taken from the JMS documentation. Why is the fields missing from the output?

class UsersController extends RestAPIController
{
    public function index(Request $request)
    {
        $john = new TestUser(
            'John',
            new TestUser(
                'John Manager',
                new TestUser('The boss'),
                array(
                    new TestUser('John Manager friend 1'),
                )
            ),
            array(
                new TestUser(
                    'John friend 1',
                    new TestUser('John friend 1 manager')
                ),
                new TestUser(
                    'John friend 2',
                    new TestUser('John friend 2 manager')
                ),
            )
        );

        $context = SerializationContext::create()->setGroups(array(
            'Default',
            'manager_group',
            'friends_group',
            'manager' => array('Default', 'friends_group'),
            'friends' => array('manager_group', 'manager' => array('Default')),
        ));

        echo $this->serializer->serialize($john, 'json', $context);
    }
}

class TestUser
{
    private $name;

    /** @Groups({"manager_group"}) */
    private $manager;

    /** @Groups({"friends_group"}) */
    private $friends;

    public function __construct($name, TestUser $manager = null, array $friends = null)
    {
        $this->name = $name;
        $this->manager = $manager;
        $this->friends = $friends;
    }
}

gildebrand avatar Mar 15 '18 16:03 gildebrand

Heh, it's been a while for me on this library, but let's see if we can figure your issue out... 😄

I'm not seeing it in your example code here, but maybe you did create one... Did you make a mapping? I think by default I made this library such that all fields are opt-in by default. To only get the type metadata might make sense at that point until you provide the mapping.

Let me know if this helps you!

Also, I would caution: This package hasn't been updated for some while as it was primarily intended to work with Laravel 4. I've typically resorted to using Laravel's own built-in serialization since later 4.x versions.

atrauzzi avatar Mar 18 '18 03:03 atrauzzi