Skip to content
Advertisement

Own Symfony serializer gets added as encoder to default one

I’m using Symfony 6.0.7 and noticed this Behavior:

When I register a custom serializer as service, it gets added as encoder to the default “@serializer” one.

Why is this the case and how can I change this behavior?

My problem is, that this messes up the error handling of the FOSRestBundle which now always outputs the stacktrace of an exception even when debug is set to false.

My services.yaml

foo.serializer:
  class: SymfonyComponentSerializerSerializer
  ...
  ...

This is what I get when dumping the encoders of the “@serializer” service:

...
array (size=5)
  0 => 
    object(SymfonyComponentSerializerSerializer)[308]  <== @foo.serializer instance
      protected 'encoder' => 
        object(SymfonyComponentSerializerEncoderChainEncoder)[332]
          private array 'encoders' => 
            array (size=1)
              ...
          private array 'encoderByFormat' => 
            array (size=0)
...

Advertisement

Answer

I found the reason this is happening:

The Serializer implements the ContextAwareEncoderInterface which extends EncoderInterface.

Since all encoders will be tagged with serializer.encoder via auto configuration, the serializer itself will be registered as encoder.

I added autoconfigure: false to my serializer service definition which fixed the problem.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement