Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

ObjectNormalizer can't normalize data if data class has annotation @Ignore and method "getSomething" with the required parameter  #46592

Copy link
Copy link
Closed
@DariusPeza

Description

@DariusPeza
Issue body actions

Symfony version(s) affected

6.1.0

Description

ObjectNormalizer can't normalize data if data class has annotation @ignore and method "getSomething" with the required parameter.
If data class don't use @Ignore annotation then methods with prefix get and required parameters are successfully ignored. But if any other method is annotated with @Ignore in this case ObjectNormalizer fails to ignore "get" methods with required parameters.

How to reproduce

<?php
require_once __DIR__ . '/vendor/autoload.php';

use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\Serializer\Annotation\Ignore;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

class Person
{
    private $name;

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }

    /**
     * @Ignore()
     */
    public function getLowerCaseName()
    {
        return strtolower($this->name);
    }

    public function getNameWithPrefix($prefix)
    {
        return sprintf('%s %s', $prefix, $this->name);
    }
}

$data = [
    'name' => 'Person name',
];

$metaFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$serializer = new Serializer([new ObjectNormalizer($metaFactory)]);

$person = $serializer->denormalize($data, Person::class);

$personData = $serializer->normalize($person);
var_dump($personData);

expected response:
array(2) { ["name"]=> string(11) "Person name" }
got:

Fatal error: Uncaught Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException: Can't get a way to read the property "nameWithPrefix" in class "Person". in /home/project/test/my_project/vendor/symfony/property-access/PropertyAccessor.php:449
Stack trace:
#0 /home/project/test/my_project/vendor/symfony/property-access/PropertyAccessor.php(115): Symfony\Component\PropertyAccess\PropertyAccessor->readProperty(Array, 'nameWithPrefix', false)
#1 /home/project/test/my_project/vendor/symfony/serializer/Normalizer/ObjectNormalizer.php(140): Symfony\Component\PropertyAccess\PropertyAccessor->getValue(Object(Person), 'nameWithPrefix')
#2 /home/project/test/my_project/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php(183): Symfony\Component\Serializer\Normalizer\ObjectNormalizer->getAttributeValue(Object(Person), 'nameWithPrefix', NULL, Array)
#3 /home/project/test/my_project/vendor/symfony/serializer/Serializer.php(161): Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->normalize(Object(Person), NULL, Array)
#4 /home/project/test/my_project/rep.php(47): Symfony\Component\Serializer\Serializer->normalize(Object(Person))

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.