Skip to content

Bug in Serializer component: Catchable Fatal Error: Object of class Symfony\Component\Serializer\Mapping\AttributeMetadata could not be converted to string #16445

Closed
@ksm2

Description

@ksm2

When you access the \Symfony\Component\Serializer\Normalizer\AbstractNormalizer::getAllowedAttributes method with $attributesAsString = false, you run into the following issue:

Uncaught exception 'Symfony\Component\Debug\Exception\ContextErrorException' with message 'Catchable Fatal Error: Object of class Symfony\Component\Serializer\Mapping\AttributeMetadata could not be converted to string' in .../Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php:207

This is due to the fact that array_unique can only be used with arrays of scalars. As it includes instances of AttributeMetadataInterface, we get the exception.

Why there is no test case throwing this error is due to fact that this method never gets called with $attributesAsString = false. So I would propose to set the default value parameter for the parameter $attributesAsString to true or even remove the possibility to return AttributeMetadataInterface[]. (Because this error occurs, we know that nobody could have ever used this feature.)

I could fix this by myself by replacing the line of code with:

        if ($attributesAsString) {
            $allowedAttributes = array_unique($allowedAttributes);
        }
        return $allowedAttributes;

Kind regards,
Konstantin

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