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

Serializing an ArrayCollection of a given type #1586

Open
andrei-dascalu opened this issue Feb 13, 2025 · 1 comment
Open

Serializing an ArrayCollection of a given type #1586

andrei-dascalu opened this issue Feb 13, 2025 · 1 comment

Comments

@andrei-dascalu
Copy link
Contributor

andrei-dascalu commented Feb 13, 2025

Hello,

I'm having a bit of an issue after migrating from annotations to attributes in the sense that I have a class such as

<?php

declare(strict_types=1);

namespace App\Tests\Services\SerializeObjects;

use Doctrine\Common\Collections\ArrayCollection;
use JMS\Serializer\Annotation as JMS;

class TestList
{
    #[JMS\Type('integer')]
    #[JMS\SerializedName('item')]
    private int $item;

    #[JMS\Type('Doctrine\Common\Collections\ArrayCollection<App\Tests\Services\SerializeObjects\TestObject>')]
    #[JMS\SerializedName('list')]
    private ArrayCollection $testObjects;

    public function __construct()
    {
        $this->item = 2;
        $this->testObjects = new ArrayCollection([
            new TestObject(),
            new TestObject(),
        ]);
    }

    /**
     * Get the value of item
     */
    public function getItem(): int
    {
        return $this->item;
    }

    /**
     * Get the value of testObjects
     */
    public function getTestObjects(): ArrayCollection
    {
        return $this->testObjects;
    }
}

when I call serialize on this, list becomes a JSON object that contains an elements array which contains the actual TestObject representations. If I use attributes, it works as expected (eg: list is a JSON array of TestObject representations). It seems that the attributes behavior is different somehow in a way that's not documented.

How the serializer is created:

        $cacheDir = '/tmp/jms';

        $strategy =
            new \JMS\Serializer\Naming\SerializedNameAnnotationStrategy(
                new \JMS\Serializer\Naming\IdenticalPropertyNamingStrategy(
                    new \JMS\Serializer\Naming\CamelCaseNamingStrategy()
                )
            );


        $builder = SerializerBuilder::create()
            ->setPropertyNamingStrategy($strategy)
            ->setExpressionEvaluator(new ExpressionEvaluator(new ExpressionLanguage()))
            ->setSerializationContextFactory(function () {
                return SerializationContext::create()
                    ->setSerializeNull(true)
                ;
            })
            ->setDebug(true);


            $builder->setCacheDir($cacheDir);
        $jms = $builder->build();

Now, my problem seems to go away if I manually register the ArrayCollectionHandler with the handler registry, though I was under the impression that the built-in handlers don't need to be manually registered (the docs says that the custom ones do).

Is this something lacking in the docs ?

@scyzoryck
Copy link
Collaborator

Hi!
Could you provide a test case in this repo or separate repo with the repro steps?
I check that but I was unable to reproduce the issue :(

Best, Marcin.

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

2 participants