Search code examples
symfonydatetimeserializationsnakecasing

symfony 4 - default normalization to snake_case


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?


Solution

  • 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