Quantcast
Channel: Active questions tagged config - Stack Overflow
Viewing all articles
Browse latest Browse all 5049

Symfony 4.2 Custom Circular Reference Handler Does not Apply

$
0
0

The context is that I have been trying to set up a Custom Circular Reference Handler for my entities that will effect ALL entities.

According to documentation ( https://symfony.com/doc/current/components/serializer.html#handling-circular-references ), we are guided to set the circular_reference_handler value under the serializer service in the config/packages/framework.yaml file as such:

framework:
    validation:
        enabled: true

    messenger:
        enabled: true

    assets:
        enabled: true

    serializer:
        circular_reference_handler: App\Services\CircularReferenceHandler
        enable_annotations: true

My actual problem is that when the serializer attempts to load the circular reference handler my override doesn't seem to exist: image showing key referencedimage showing context empty

Why isn't my override getting applied to the context at all??

I have attempted to:

  • Reload cache
  • Use a ContextBuilder class from my API Platform package
  • Apply it in a Normalizer

None of have worked

I showed you my yaml, but here is the handler:

<?php

namespace App\Services;

class CircularReferenceHandler
{
    /**
     * @param object $object
     * @return mixed
     */
    public function __invoke($object)
    {
        return $object->getId();
    }
}

and here is the Context builder I attempted to use as an alternative:

<?php

namespace App\Serializer;

use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
use App\Services\CircularReferenceHandler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;

final class GlobalSerializerContextBuilder implements SerializerContextBuilderInterface
{
    /**
     * Creates a serialization context from a Request.
     *
     * @param Request    $request
     * @param bool       $normalization
     * @param array|null $extractedAttributes
     *
     * @return array
     */
    public function createFromRequest(Request $request, bool $normalization, array $extractedAttributes = null): array
    {
        $resourceClass = $context['resource_class'] ?? null;

        $context[AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER] = CircularReferenceHandler::class;

        return $context;
    }
}

and here is where I registered that ( doc ):

    App\Serializer\GlobalSerializerContextBuilder:
        decorates: 'api_platform.serializer.context_builder'
        arguments: ['@App\Serializer\GlobalSerializerContextBuilder.inner']
        autoconfigure: false

I expected my custom circular_reference_handler to be applied when the AbstractNormalizer->handleCircularReference() method is used, but the actuality is the context received in this method does not include my additional key/value pair ( e.g. ['circular_reference_handler => App\Services\CircularReferenceHandler]


Viewing all articles
Browse latest Browse all 5049

Trending Articles