Skip to content

[Serializer] Allow max depth of 0 #41152

Closed
@senaria

Description

@senaria

Description

Feature:
Max depth can be defined set to 0.

Usecase:
A user object that has the property parentUser. That parent user should be represented as string rather than an object as it would be with maxDepth=1 on the first level.

Example

class UserNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
{
    [...]
    public function normalize($object, $format = null, array $context = [])
    {
        [...]
        $maxDepthHandler = static fn ($innerObject): string => $innerObject ? $innerObject->toString() : '';
        $context[AbstractObjectNormalizer::MAX_DEPTH_HANDLER] = $maxDepthHandler;

        return $this->normalizer->normalize($object, $format, $context);
    }
    [...]
}

Before:

class User 
{
   /**
     * @MaxDepth(1)
     */
    private ?User $parentUser = null;
}

Result: 

[
    'parentUser' => [
        'parentUser' => 'string'
    ]
]

After:

class User 
{
   /**
     * @MaxDepth(0)
     */
    private ?User $parentUser = null;
}

Result: 

[
    'parentUser' => 'string'
]

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