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

Skip to content

Custom normalizer causes infinite recursion #53708

Closed
@emi87290

Description

@emi87290

Symfony version(s) affected

7.0.3

Description

When I create a basic custom normalizer to add a data to the normalization process I run into an infinite recursion.
My worked on 6.4 but without the NormalizerAwareInterface, since I ran in 7.0.3 I needed it or the serializer would not be instancied.
Anyways, I took the documentation and try it without success:

Maximum call stack size of 8339456 bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached. Infinite recursion?

If I look in the profiler, the recursion loop on the line $data = $this->normalizer->normalize($topic, $format, $context); from the documentation like code

How to reproduce

Create a custom serializer near to the documentation (adding url to the process)

<?php

namespace App\Serializer;

use App\Entity\Category;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class CategoryNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
    use NormalizerAwareTrait;

    public function __construct(private readonly UrlGeneratorInterface $router) {}

    public function normalize($category, string $format = null, array $context = []): array
    {
        $data = $this->normalizer->normalize($category, $format, $context);

        if (isset($context['groups']) && in_array('admin:category:list', $context['groups'])) {
            $data['link'] = [];

            $data['link']['edit'] = $this->router->generate('app_admin_photo_shoot_category_edit', [
                'id' => $category->getId()
            ]);

            $data['link']['delete'] = $this->router->generate('app_admin_photo_shoot_category_delete', [
                'id' => $category->getId()
            ]);
        }

        return $data;
    }

    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
    {
        return $data instanceof Category;
    }

    public function getSupportedTypes(?string $format): array
    {
        return [
            Category::class => true,
        ];
    }
}

And a controller that serve the data:

#[Route('/data/categories', name: '_api_categories')]
    public function getCategories(CategoryRepository $categoryRepository): JsonResponse
    {
        $categories = $categoryRepository->findBy([], ['label' => Criteria::ASC]);

        return $this->json($categories, context: ['groups' => ['admin:category:list']]);
    }

Possible Solution

No response

Additional Context

No response

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