Skip to content

The denormalize method in the ItemNormalizer does not set the resource_class correctly. #2158

Closed
@nielsvandermolen

Description

@nielsvandermolen

Stack trace:
In ItemNormalizer.php line 54:

[ErrorException]
Notice: Undefined index: resource_class

Exception trace:
ApiPlatform\Core\Serializer\ItemNormalizer->updateObjectToPopulate() at /var/www/environments/vendor/api-platform/core/src/Serializer/ItemNormalizer.php:42
ApiPlatform\Core\Serializer\ItemNormalizer->denormalize() at /var/www/environments/vendor/symfony/serializer/Serializer.php:179
Symfony\Component\Serializer\Serializer->denormalize() at /var/www/environments/vendor/symfony/serializer/Normalizer/AbstractNormalizer.php:370
Symfony\Component\Serializer\Normalizer\AbstractNormalizer->instantiateObject() at /var/www/environments/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php:143
Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->instantiateObject() at /var/www/environments/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php:244
Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->denormalize() at /var/www/environments/vendor/symfony/serializer/Serializer.php:179
Symfony\Component\Serializer\Serializer->denormalize() at /var/www/environments/vendor/symfony/serializer/Serializer.php:128
Symfony\Component\Serializer\Serializer->deserialize() at /var/www/environments/vendor/symfony/messenger/Transport/Serialization/Serializer.php:54`

What triggered this issue:
Using API Platform in combination with the Symfony Message component and encoding an entity that is an APIPlatform resource.

Root cause
In the denormalize method in api-platform/core/src/Serializer/ItemNormalizer.php the resource_class is not set in the context. It seems this is common because it is handled in the AbstractItemNormalizer class line 123:

$context['api_denormalize'] = true;
if (!isset($context['resource_class'])) {
$context['resource_class'] = $class;
}

Unfortunatly, this is called after we need it

        $this->updateObjectToPopulate($data, $context);  // The error is thrown in this method.
    }
    return parent::denormalize($data, $class, $format, $context); // Calls the method in the AbstractClass

Potential solution
Adding the code in the ItemNormalizer instead of the AbstractNormalizer seems to solve the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions