Home > OS >  symfony 4 - default normalization to snake_case
symfony 4 - default normalization to snake_case

Time:06-10

trying to get the sf4 serializer component to use snake_case as the default:

Symfony\Component\Serializer\Normalizer\ObjectNormalizer:
        public: true
        arguments: ['@serializer.mapping.class_metadata_factory', '@serializer.name_converter.camel_case_to_snake_case']
        tags: [serializer.normalizer]

works.

but now DateTime is being normalized to empty arrays.
I don't get why, without the config changes, its normalized to a date string, as you would expect.

What am i doing wrong here?

CodePudding user response:

turns out, you simply need to enable a name_converter the proper way like this instead:

# config/packages/framework.yaml
framework:
    # ...
    serializer:
        name_converter: 'serializer.name_converter.camel_case_to_snake_case'

see
https://symfony.com/doc/current/serializer.html#enabling-a-name-converter https://github.com/symfony/symfony/issues/40818

  • Related