-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Comments
Can you create a small example application that allows to reproduce your issue? |
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; }
} |
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:
|
Hi, Working example application is here. Run |
Hey, thanks for your report! |
Hi, Run the command |
… 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
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
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:
This throws an exception:
Possible Solution
The
AbstractNormalizer::getAllowedAttributes
method returns all attributes (public, private) loaded by theAnnotationLoader
when theIgnore
attribute is used. But theObjectNormalizer
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
The text was updated successfully, but these errors were encountered: