Skip to content

[JsonStreamer] Selialization brokes on multilevel collections #60984

Closed
@p-golovin

Description

@p-golovin

Symfony version(s) affected

7.3.1

Description

If I try to serialize collection of objects which have embedded collection, I get Undefined property error.
The problem is in vendor/symfony/json-streamer/Write/StreamWriterGenerator.php:186, which generate code like

foreach ($value->events as $value)

and redefine $value with embedded object.

How to reproduce

namespace App\Dto;

use Symfony\Component\JsonStreamer\Attribute\JsonStreamable;

#[JsonStreamable]
class User
{
    public array $names;
}
namespace App\Controller;

use App\Dto\User;
use App\Repository\UserRepository;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\JsonStreamer\StreamWriterInterface;
use Symfony\Component\TypeInfo\Type;

class UserController
{
    public function retrieveUsers(StreamWriterInterface $jsonStreamWriter): StreamedResponse
    {
        $user1 = new User();
        $user1->names = ['John', 'Alex'];

        $user2 = new User();
        $user1->names = ['Mark', 'Andre'];

        $users = [$user1, $user2];

        $type = Type::list(Type::object(User::class));

        $json = $jsonStreamWriter->write($users, $type);

        return new StreamedResponse($json);
    }
}

Possible Solution

Replace in vendor/symfony/json-streamer/Write/StreamWriterGenerator.php:186

                $this->createDataModel($type->getCollectionValueType(), new VariableDataAccessor('value'), $options, $context),

to

                $this->createDataModel($type->getCollectionValueType(), new VariableDataAccessor('value' . rand()), $options, $context),

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy