1

When using the Framework Bundle of Symfony 4.2 with the Serializer Component there are several Normalizers configured automatically by the Framework in a specific order.

I want to replace one of these Normalizers (ObjectNormalizer) with my personal implementation (which is an extension of the ObjectNormalizer) without touching all other provided normalizers or the order of them.

Is there a way to achieve this without redeclaring all normalizers and their order in the Serializer configuration?

Another option would be to make sure my custom Normalizer is directly positioned before the ObjectNormalizer.

cezar
  • 11,616
  • 6
  • 48
  • 84
LBA
  • 3,859
  • 2
  • 21
  • 60
  • have you looked at adding a compiler pass to override the component, https://symfony.com/doc/current/service_container/compiler_passes.html? I'm not familiar with 4 and the serializer component but if you can access the normalizer by name you should be able to override it. – N3SS4H Mar 26 '19 at 16:35
  • Thanks, to my understanding it's just adding Normalizers to an indexed array, there's no name to directly identify it. That's why I am asking if there's a better way. – LBA Mar 26 '19 at 16:38

1 Answers1

0

Normalizers are set with priority by the configuration of the Symfony Framework Bundle. One can find the priorities listed here

Knowing that one can define its own priority to place CustomNormalizer where required in service.yaml:

App\Normalizer\MyCustomNormalizer:
    tags:
    - {name: serializer.normalizer, priority: -995}
LBA
  • 3,859
  • 2
  • 21
  • 60