Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Serializer] MaxDepth annotation not working as expected #20932

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

Closed
digilist opened this issue Dec 15, 2016 · 2 comments
Closed

[Serializer] MaxDepth annotation not working as expected #20932

digilist opened this issue Dec 15, 2016 · 2 comments

Comments

@digilist
Copy link
Contributor

digilist commented Dec 15, 2016

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 3.1.7

I tested the new MaxDepth annotation in the Symfony serializer (see #17113). Consider some code that extends the example code given in the pull request with more levels (4, 5, etc.). By keeping a max depth of 2, the result is the following:

        'foo' => 'level1',
        'child' =>
            array (
                'foo' => 'level2',
                'child' =>
                    array (
                        'child' =>
                            array (
                                'child' =>
                                    array (
                                        'child' =>
                                            array (
                                                'child' => NULL,
                                            ),
                                    ),
                            ),
                    ),
            ),
    )

As you can see, it considers all child nodes and does not stop after the configured max depths. Only the data of the objects in deeper levels is excluded (attribute foo).

@ogizanagi
Copy link
Contributor

I guess the real issue here is that the example code should set the @MaxDepth annotation on the child property rather on the foo one.

Trying the following code:

<?php

$loader = require __DIR__.'/vendor/autoload.php';

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Symfony\Component\Serializer\Annotation\MaxDepth;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;

AnnotationRegistry::registerLoader([$loader, 'loadClass']);

class MyObj
{
    public $foo;

    /**
     * @var self
     *
     * @MaxDepth(2)
     */
    public $child;
}

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$normalizer = new ObjectNormalizer($classMetadataFactory);
$serializer = new Serializer(array($normalizer));

$level1 = new MyObj();
$level1->foo = 'level1';

$level2 = new MyObj();
$level2->foo = 'level2';
$level1->child = $level2;

$level3 = new MyObj();
$level3->foo = 'level3';
$level2->child = $level3;

$level4 = new MyObj();
$level4->foo = 'level4';
$level3->child = $level4;

$level5 = new MyObj();
$level5->foo = 'level5';
$level4->child = $level5;

$result = $serializer->normalize($level1, null, array(ObjectNormalizer::ENABLE_MAX_DEPTH => true));

dump($result);

The output will be:

array:2 [
  "foo" => "level1"
  "child" => array:2 [
    "foo" => "level2"
    "child" => array:1 [
      "foo" => "level3"
    ]
  ]
]

which looks perfectly fine to me.

@digilist
Copy link
Contributor Author

Okay sorry, my bad. I ran into this issue, because I was debugging a problem with circular references which I tried to solve by setting a max depth. As it did not work, I used your example code for further tests. I just oversaw that the MaxDepth was set on the wrong property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants