Skip to content
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

[Serializer] Attribute SerializedName ignored when property name is the same as the related (get|has|can) method #54109

Open
systemasis opened this issue Feb 29, 2024 · 4 comments

Comments

@systemasis
Copy link

Symfony version(s) affected

7.0.*

Description

Symfony serializer ignores the attribute SerializedName for properties sharing the same name as the related method shall it start by can, has or get.

How to reproduce

Clone this repository created specifically and use the following command :

bin/console app:test:test

This will output the string of a json serialized App\Entity\ListView object.

Expected output :

"{"free_trial":true,"can_subscribe":false,"has_answers":true,"get_ready":false}"

Current output :

"{"free_trial":true,"subscribe":false,"answers":true,"ready":false}"

Possible Solution

No response

Additional Context

No response

@NeilPeyssard
Copy link
Contributor

Hello,

In a regular use case, you'll probably never call your properties with the same as their accessors (e.g. subscribe instead of canSubscribe).

But after digging around your problem, I can reproduce it. I'll try to work on it, but let's wait for somebody more experienced to see if it's not the expected behavior.

Just for your information, the command is missing in your project, but I guess it looks like this ?

<?php

namespace App\Command;

use App\Entity\ListView;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Serializer\SerializerInterface;

#[AsCommand(name: 'app:test:test')]
class TestCommand extends Command
{
    public function __construct(
        private readonly SerializerInterface $serializer
    ) {
        parent::__construct();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $listView = new ListView(true, false, true, false);
        $output->writeln($this->serializer->serialize($listView, 'json'));

        return Command::SUCCESS;
    }
}

@systemasis
Copy link
Author

systemasis commented Mar 1, 2024

I indeed forgot the command and apologize for this but you got it. I'll commit it anyway.

As for the "regular use case", I do agree with you but this would be considering people are perfect beings or at least fluent enough in english to avoid literal translations.
I encountered this problem because I had to serialize an existing object in my project. I managed to workaround it by renaming both the property and the method but this is not always possible.

@NeilPeyssard
Copy link
Contributor

Hello,

I've open a PR to fix your issue. See #54148

@fabpot fabpot closed this as completed Mar 17, 2024
fabpot added a commit that referenced this issue Mar 17, 2024
… same name as their accessor (NeilPeyssard)

This PR was merged into the 5.4 branch.

Discussion
----------

[Serializer] Fix object normalizer when properties has the same name as their accessor

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #54109
| License       | MIT

Hello,

This PR fix a bug in object normalization when properties has the same name as their accessor. The current behavior mess up class metadata between properties and methods so the keys in the normalized format does not match the object properties, and metadata are not correctly applied (`SerializedName` in our exemple).

This bug also affects versions 6.4 and 7+, but I'm not sure if we can merge this PR in these branches without refactoring. Let me know if another PR should be open for version 6.4 and 7.

Commits
-------

8575199 [Serializer] Fix object normalizer when properties has the same name as their accessor
@chalasr
Copy link
Member

chalasr commented Apr 17, 2024

Fix reverted in #54635, should be resubmitted against the development branch.

@chalasr chalasr reopened this Apr 17, 2024
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

5 participants