Skip to content

Automatically handle merging default context for normalizers #59614

@mathroc

Description

@mathroc

Symfony version(s) affected

7.2.x

Description

When using a default context in a named serializer, targeted normalizers & encoders need to have support for this. (they need to have an array $defaultContext constructor argument and merge it themselves with the context they get from normalize() & co.)

Instead, it would be great if it could be applied automatically. It would simplify the existing Symfony normalizers and won't receive anything for 3rd party normalizers either.

It would also be less surprising when trying this with a custom normalizer as it's not easy to discover what is needed (even if it was documented)

Possible solution

It could be implemented in the serializer, or as decorators of the normalizers and encoders.

maybe something to have in mind when choosing a solution: #59638


Initial (wrong) report:

Description

when a default context is set in a named serializer, normalizer don't have access to this context

How to reproduce

framework:
    serializer:
        named_serializers:
            my:
                default_context:
                    datetime_format: U
                    datetime_cast: int
                    yaml_indent: 1
echo $mySerializer->serialize(['a' => ['date' => new \DateTimeImmutable()]], format: 'yaml');

the printed YAML will be indented but the datetime format & cast will be ignored

Possible Solution

This probably requires some changes to the Serializer implementation as of now the context is only set in the encoders and the serializer as no concept of a default context to merge with the one given to serialize(), deserialize(), normalize(), etc.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy