Skip to content

[Serializer] Named Serializers do not work on intersection types #60682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
MarijnDoeve opened this issue Jun 4, 2025 · 1 comment
Open

Comments

@MarijnDoeve
Copy link

Symfony version(s) affected

7.3.0, 7.2.0

Description

Named Serializes are only injected when using SerializerInterface. When I use an intersection type like SerializerInterface&DenormalizerInterface I'm getting the default Serializer.

How to reproduce

#config/serializer.yaml
framework:
    serializer:
        named_serializers:
            somthing:
                name_converter: 'serializer.name_converter.camel_case_to_snake_case'
                default_context:
                    datetime_format: 'U'
    public function __construct(
        protected SerializerInterface&DenormalizerInterface $somethingSerializer,
    ) {
    }

Possible Solution

No response

Additional Context

No response

@stof
Copy link
Member

stof commented Jun 4, 2025

Recommendation: use the #[Target] attribute instead of relying on matching by name when you expect a non-default service. This way, you will get an explicit failure instead of a silent fallback to the default one, as Symfony will know that you expect to target a specific implementation thanks to the attribute (it cannot make such assumption based on the usage of a name for the argument, as all arguments have a name)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants
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