Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Automatically handle merging default context for normalizers #59614

Copy link
Copy link
Open
@mathroc

Description

@mathroc
Issue body actions

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.