8000 Serializer throw exception when Ignore attribute is used · Issue #49710 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Serializer throw exception when Ignore attribute is used #49710

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
ludekbenedik opened this issue Mar 16, 2023 · 6 comments
Closed

Serializer throw exception when Ignore attribute is used #49710

ludekbenedik opened this issue Mar 16, 2023 · 6 comments

Comments

@ludekbenedik
Copy link

Symfony version(s) affected

5.4.21

Description

Hi,
the serializer throws an exception when the Ignore attribute is used and an entity contains a private getter/isser.

How to reproduce

This is OK:

class EntityWithSerializerIgnore
{
    public function getFoo(): int { return 1; }
    public function getBar(): int { return 2; }
    private function isSomething(): bool { return true; }
}

$entity = new EntityWithSerializerIgnore();
$serializer->serialize($entity, 'json'); // {"foo":1,"bar":2}

This throws an exception:

class EntityWithSerializerIgnore
{
    public function getFoo(): int { return 1; }
    #[Ignore]
    public function getBar(): int { return 2; }
    private function isSomething(): bool { return true; }
}

$entity = new EntityWithSerializerIgnore();
$serializer->serialize($entity, 'json'); // throws: Can't get a way to read the property "something" in class "EntityWithSerializerIgnore".

Possible Solution

The AbstractNormalizer::getAllowedAttributes method returns all attributes (public, private) loaded by the AnnotationLoader when the Ignore attribute is used. But the ObjectNormalizer load only public attributes (methods).

The AbstractObjectNormalizer::getAttributes method cannot returns all allowed attributes, but have to combine these attributes with extracted attributes.

Additional Context

No response

@xabbuh
Copy link
Member
xabbuh commented Mar 19, 2023

Can you create a small example application that allows to reproduce your issue?

@ludekbenedik
Copy link
Author

src/Controller/SerializerBugExampleController.php

<?php declare(strict_types = 1);

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Annotation\Ignore;
use Symfony\Component\Serializer\SerializerInterface;

class SerializerBugExampleController extends AbstractController
{
    #[Route('/serializer-bug-example', name: 'serializer_bug_example')]
    public function __invoke(SerializerInterface $serializer): JsonResponse
    {
        return new JsonResponse($serializer->serialize(new SerializerBugExampleEntity(), 'json'), 200, [], true);
    }
}

class SerializerBugExampleEntity
{
    public function getFoo(): int { return 1; }
    #[Ignore]
    public function getBar(): int { return 2; }
    private function isSomething(): bool { return true; }
}

@xabbuh
Copy link
Member
xabbuh commented Mar 25, 2023

Can you share your serializer config or create a fully working example application that we can check out? Right now, with your code example I end up with another error:

Could not normalize object of type "App\Controller\SerializerBugExampleEntity", no supporting normalizer found.

@ludekbenedik
Copy link
Author
ludekbenedik commented Mar 25, 2023

Hi,
the example works in a new webapp project without any changes in configuration.

Working example application is here. Run symfony server:start and open homepage in a browser.

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@ludekbenedik
Copy link
Author

Hi,
the bug is still there. I have updated the vendor in the example application and added a command where you can check it.

Run the command bin/console serializer-bug-example or start a server and open the homepage in a browser.

@carsonbot carsonbot removed the Stalled label Oct 17, 2023
nicolas-grekas added a commit that referenced this issue Nov 24, 2023
… using the ``@Ignore`` annotation (mtarld)

This PR was merged into the 5.4 branch.

Discussion
----------

[Serializer] Fix access to private properties/getters when using the ``@Ignore`` annotation

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #52673 #49710
| License       | MIT

Commits
-------

cc356b0 [Serializer] Fix access to private when Ignore
@fabpot fabpot closed this as completed Apr 8, 2024
fabpot added a commit that referenced this issue Apr 8, 2024
This PR was merged into the 5.4 branch.

Discussion
----------

[Serializer] Fix unexpected allowed attributes

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #52673 #49710
| License       | MIT

A more accurate approach than #52680

Commits
-------

900d034 [Serializer] Fix unexpected allowed attributes
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
0