Skip to content

Max depth produces array of nulls #671

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
danmartin-epiphany opened this issue Nov 3, 2016 · 6 comments
Closed

Max depth produces array of nulls #671

danmartin-epiphany opened this issue Nov 3, 2016 · 6 comments

Comments

@danmartin-epiphany
Copy link

Since updating from v1.3.x to 1.4.x the serializer now returns an array of nulls when max_depth is exceeded whereas before it returned an empty array.

E.g. I have something like this set up:

class Tag {
    public $name;
}

class User {
    public $name;
    public User $lineManager;
    public tags = []; // array of Tag
}

In my serializer config I have max_depth set to 1 for $lineManager.

I used to get something like this when serializing a user:

{
    name: "Employee",
    tags: [
        {
            name: "Junior"
        },
        {
            name: "Developer"
        }
    ],
    lineManager: {
        name: "Manager",
        tags: [],
        lineManager: null
    }
}

Now I get this:

{
    name: "Employee",
    tags: [
        {
            name: "Junior"
        },
        {
            name: "Developer"
        }
    ],
    lineManager: {
        name: "Manager",
        tags: [null, null, null],
        lineManager: null
    }
}

lineManager.tags has gone from [] to [null, null, null]

I've had a look through the notes etc. and I can't see anything about this. It's possible that it was intended but it doesn't seem right to me.

@goetas
Copy link
Collaborator

goetas commented Nov 3, 2016

for arrays you need max_depth = 2

@danmartin-epiphany
Copy link
Author

Thanks for responding so quickly but if I set max_depth = 2 then that just pushes the problem further down:

{
    name: "Employee",
    tags: [
        {
            name: "Junior"
        },
        {
            name: "Developer"
        }
    ],
    lineManager: {
        name: "Manager",
        tags: [
            {
                name: "Senior"
            },
            {
                name: "Developer"
            }
        ],
        lineManager: {
             name: "Director",
             tags: [null, null]
        }
    }
}

Now I get the full set of tags for the lineManager but I also get their lineManager and they have an array of nulls for tags.

@goetas
Copy link
Collaborator

goetas commented Nov 3, 2016

it is a known bug (and very old), but you can put max_depth = 2 on your collections

@danmartin-epiphany
Copy link
Author

Forgive me if I am misunderstanding but does this mean I should have max_depth = 1 on the line manager property and max_depth = 2 on tags? That doesn't seem to affect anything for me.

I'm also wondering why the behaviour has recently changed if it was an old bug?

@goetas
Copy link
Collaborator

goetas commented Nov 4, 2016

I'm also wondering why the behaviour has recently changed if it was an old bug?

this is a old bug, if you look at the issue list, there are issues 2 years old talking about it

Fixing the bug now will almost create a BC break, since a lot of people (me includes) are solving the issue with max depth = 2

@danmartin-epiphany
Copy link
Author

OK I'm going to work around the problem. Thanks for your help.

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