Skip to content

SerializedName with numbers does not work properly (symfony/serializer) #49538

@elevado

Description

@elevado

Symfony version(s) affected

6.2.*

Description

A JSON must be created where the properties are field numbers.

class Person
{
    #[SerializedName('1')]
    private string $firstname;

    #[SerializedName('2')]
    private string $lastname;

    #[SerializedName('3')]
    private string $email;
   
    /*
         getter and setter
    */   
}
$person = new Person();
$person->setFirstname('John');
$person->setLastname('Doe');
$person->setEmail('john.doe@example.com');

$jsonContent = $this->serializer->serialize($person, 'json');

/*
        {
    	    "1": "John",
    	    "2": "Doe"
    	    "3": "john.doe@example.com"
	}
*/

However, an error occurs during deserialisation.

$deserializePerson = $this->serializer->deserialize($jsonContent, Person::class, 'json');

/*
	App\Dto\Person Object
	(
	    [firstname:App\Dto\Person:private] => Doe
	    [lastname:App\Dto\Person:private] => john.doe@example.com
	)
*/
  • When debugging, I discovered that the last property in \Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter::denormalize is not processed, so the method is not called for it.
  • The second to last property gets the value of the last property.

How to reproduce

I have pushed a fresh Symfony with only one Controller and DTO in here:
https://github.com/elevado/symfony-serializer-serializedname-bug

Possible Solution

No response

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