Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DariusPeza opened this issue Jun 6, 2022 · 1 comment

Comments

@DariusPeza
Copy link

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

@astepin
Copy link
Contributor

astepin commented Jul 16, 2022

Hey @DariusPeza in my opinion this is indeed a bug. I have created a PR for the fix here: #46958

astepin added a commit to CloudPlayDev/symfony that referenced this issue Jul 17, 2022
@fabpot fabpot closed this as completed Jul 19, 2022
fabpot added a commit that referenced this issue Jul 19, 2022
…46592) (astepin)

This PR was merged into the 5.4 branch.

Discussion
----------

[Serializer] Ignore getter with required parameters (Fix #46592)

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #46592
| License       | MIT
| Doc PR        | na

If no Ignore annotation is used, the attributes for serialization are obtained using `Symfony\Component\Serializer\Normalizer\ObjectNormalizer::extractAttributes`.
There it is checked if the method has required parameters and if yes, the method is ignored.

However, if you use the Ignore annotation, the attributes are determined with a different method. Here I have adapted at least for get methods the behavior as it was before.

If someone serialized a class with Ignore annotations before, he got here `\Symfony\Component\PropertyAccess\PropertyAccessor::readProperty` an exception as written in ticket #46592. With this fix the methods are ignored and there is no exception anymore.

Commits
-------

fda9281 Fix #46592 - Ignore getter with required parameters
derrabus added a commit that referenced this issue Jul 19, 2022
* 5.4:
  Fix CS
  Fix CS
  quote address names if they contain parentheses
  [FrameworkBundle] Fail gracefully when forms use disabled CSRF
  [Mime] Fix inline parts when added via attachPart()
  Fail gracefully when attempting to autowire composite types
  [VarDumper] Add a test case for nesting intersection and union types
  Fix #46592 - Ignore getter with required parameters
  [Serializer] Fix inconsistent behaviour of nullable objects in key/value arrays
derrabus added a commit that referenced this issue Jul 19, 2022
* 6.0:
  Fix CS
  Fix CS
  quote address names if they contain parentheses
  [FrameworkBundle] Fail gracefully when forms use disabled CSRF
  [Mime] Fix inline parts when added via attachPart()
  Fail gracefully when attempting to autowire composite types
  [VarDumper] Add a test case for nesting intersection and union types
  Fix #46592 - Ignore getter with required parameters
  [Serializer] Fix inconsistent behaviour of nullable objects in key/value arrays
derrabus added a commit that referenced this issue Jul 19, 2022
* 6.1:
  Fix CS
  Fix CS
  quote address names if they contain parentheses
  [FrameworkBundle] Fail gracefully when forms use disabled CSRF
  [Mime] Fix inline parts when added via attachPart()
  Fail gracefully when attempting to autowire composite types
  [VarDumper] Add a test case for nesting intersection and union types
  Fix #46592 - Ignore getter with required parameters
  [Serializer] Fix inconsistent behaviour of nullable objects in key/value arrays
This was referenced Jul 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants