Skip to content

[Serializer] Named serializer should get correct normalizer instance #59627

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
wants to merge 1 commit into
base: 7.2
Choose a base branch
from

Conversation

ruudk
Copy link
Contributor

@ruudk ruudk commented Jan 27, 2025

Q A
Branch? 7.2
Bug fix? yes
New feature? no
Deprecations? no
Issues
License MIT

Named serializers were introduced in #56823 and they work great.

We noticed a small bug when using custom name convertors.

The MimeMessageNormalizer holds a reference to serializer.normalizer.property.
But when using named serializers, it should use the specific child normalizer instead.

With this change, we fix this problem for any service that starts with serializer.normalizer..

/cc @HypeMC

@ruudk ruudk requested a review from dunglas as a code owner January 27, 2025 11:12
@carsonbot carsonbot added this to the 7.2 milestone Jan 27, 2025
Named serializers were introduced in symfony#56823 and they work great.

We noticed a small bug when using custom name convertors.

The MimeMessageNormalizer holds a reference to `serializer.normalizer.property`.
But when using named serializers, it would use the specific child normalizer instead.

With this change, we fix this problem for any service that starts with `serializer.normalizer.`.
@ruudk ruudk force-pushed the ruudk/2025/1/fix-mime-message-normalizer branch from 93ca946 to e0caeb7 Compare January 27, 2025 11:12
@ruudk ruudk changed the title Named serializer should get correct normalizer instance [Serializer] Named serializer should get correct normalizer instance Jan 27, 2025
foreach ($container->getDefinition($id)->getArguments() as $index => $argument) {
if ($argument instanceof Reference && self::NAME_CONVERTER_METADATA_AWARE_ID === (string) $argument) {
$definition->replaceArgument($index, new Reference($config['name_converter']));
} elseif ($argument instanceof Reference && str_starts_with((string) $argument, 'serializer.normalizer.')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ruudk This won't work since the service is not tagged with serializer.normalizer:

->set('serializer.normalizer.property', PropertyNormalizer::class)
->args([
service('serializer.mapping.class_metadata_factory'),
service('serializer.name_converter.metadata_aware'),
service('property_info')->ignoreOnInvalid(),
service('serializer.mapping.class_discriminator_resolver')->ignoreOnInvalid(),
null,
])
->set('serializer.denormalizer.array', ArrayDenormalizer::class)

Without the tag, the child service will never be created. As far as I can tell, the service was intentionally not tagged, but I could be wrong, see #37847.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird because this fix does solve the problem.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? I've tested the fix on a clean install and got an error:

framework:
    serializer:
        named_serializers:
            foo:
                name_converter: serializer.name_converter.camel_case_to_snake_case
The service "serializer.normalizer.mime_message.foo" has a dependency on a non-existent service "serializer.normalizer.property.foo".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, you're right. For some reason, the project where we experience this problem, had the following compiler pass:

 $container->addCompilerPass(
            new class() implements CompilerPassInterface {
                public function process(ContainerBuilder $container) : void
                {
                    $container->getDefinition('serializer.normalizer.property')
                        ->addTag('serializer.normalizer', [
                            'priority' => -990,
                        ]);
                }
            },
            priority: 10,
        );

I cannot recall why we added this, but probably to make sure serialization worked the same way as JMS Serializer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know why we added this, because we want to use the PropertyNormalizer to make sure no getters are called while serializing. We only want properties to be serialized.

Is there a better way to enable this? Or is the compiler pass above the recommended way to use this normalizer?

Maybe we can check in this PR if the serializer.normalizer.*.$serializer_name exists, and then replace it?

@carsonbot carsonbot changed the title [Serializer] Named serializer should get correct normalizer instance Named serializer should get correct normalizer instance Feb 10, 2025
@carsonbot carsonbot changed the title Named serializer should get correct normalizer instance [Serializer] Named serializer should get correct normalizer instance Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants