Skip to content

[Serializer] ArrayCollection is not denormalised (from json to object) #41191

Closed
@oleg-andreyev

Description

@oleg-andreyev

Symfony version(s) affected: 5.2.7

Description
When a model has as a collection described with ArrayCollection, it's deserialize into array not ArrayCollection

How to reproduce

<?php

use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

require_once './vendor/autoload.php';

class InnerModel
{
    /** @var string */
    public $id;
}

class Model
{
    /** @var ArrayCollection<InnerModel> */
    public $items;
}

$serializer = new Serializer([
    new ObjectNormalizer(
        new ClassMetadataFactory(
            new AnnotationLoader()
        ),
        null,
        null,
        new PhpDocExtractor()
    ),
    new ArrayDenormalizer(),
], ['json' => new JsonEncoder()]);

$data = $serializer->deserialize(
    '{"items":[{"id": "1"}]}',
    Model::class,
    'json',
    [
        'allow_extra_attributes' => true
    ]
);
var_dump($data);

Actual result

class Model#32 (1) {
  public $items =>
  array(1) {
    [0] =>
    class InnerModel#55 (1) {
      public $id =>
      string(1) "1"
    }
  }
}

Expected result

class Model#30 (1) {
  public $items =>
  class Doctrine\Common\Collections\ArrayCollection#53 (1) {
    private $elements =>
    array(1) {
      [0] =>
      class InnerModel#62 (1) {
        ...
      }
    }
  }
}

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