79733091

Date: 2025-08-12 11:36:12
Score: 1.5
Natty:
Report link

Here is the solution i use :

  1. Create custom Object normalizer.
  2. Copy the entire ObjectNormalizer from symfony vendors.
  3. Add the denormalize function from AbstractObjectNormalizer.
  4. Add new lines between foreach and if (change if to elseif).

Before :

foreach ($nestedAttributes as $property => $serializedPath) {
    
    if (null === $value = $propertyAccessor->getValue($normalizedData, $serializedPath)) {
    ...

After :

foreach ($nestedAttributes as $property => $serializedPath) {
    if ($serializedPath->getElement(0) === '@parentKey') {
        if (!isset($context['deserialization_path'])) {
            throw new \Error('Deserialized objet have no parent');
        }
        preg_match("/\[(?'key'\w*)\]$/", $context['deserialization_path'], $matches);
        if (!isset($matches['key'])) {
            throw new \Error('Deserialized objet is not emmbed in array');
        }
        $value = $matches['key'];
     } elseif (null === $value = $propertyAccessor->getValue($normalizedData, $serializedPath)) {
    ...
  1. Add SerializedPath attribute do DTO :
class ItemDTO
{
    #[SerializedPath('[@parentKey]')]
    public ?string $slug = null;
    public ?string $name = null;
    public ?string $description = null;
}

How it works ?

It's a little hack that use the SerializedPath attribute to communicate with the ObjectNormaliser with a custom special path '@parentKey'.

The new object normalizer detect this path and look in the deserialization context to find the key value.

How to improve ?

The best symfony way would be a new tag to do the job. But it needs to create multiple new files like AttributeMetadata, AttributeLoader down to ObjectNormalizer and inject them into right services.

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Self-answer (0.5):
  • Looks like a comment (1):
  • Low reputation (1):
Posted by: Cedric Molines