Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
Closed
@oleg-andreyev

Description

@oleg-andreyev
Issue body actions

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.