I'm using jms/serializer-bundle 2.4.3 on a symfony 4.2 and a I noticed an annoying problem in my application : when I post an entity, the DoctrineObjectConstructor uses id in content to retrieve another entity and thus patch it while it is excluded by my security groups
see rather entity
class Entity{/** * @var int * * @ORM\Column(name="id", type="int") * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") * @Serializer\Groups({"GetEntity"}) */private $id;/** * @var string * * @ORM\Column(name="name", type="string") * @Serializer\Groups({"GetEntity", "PostEntity"}) */private $name;}
controller
/** * @Route("/entity", name="post_entity", methods={"POST"}) */public function postEntity(Request $request, EntityManagerInterface $entityManager, SerializerInterface $serializer): JsonResponse{ $deserializationContext = DeserializationContext::create(); $deserializationContext->setGroups(['PostEntity']); $entity = $serializer->deserialize($request->getContent(), Entity::class, 'json', $deserializationContext); $entityManager->persist($entity); $entityManager->flush(); return $this->json($entity, Response::HTTP_OK, [], ['groups' => ['GetEntity']]);}
I have some JMS configurations changes in services
jms_serializer.object_constructor: alias: jms_serializer.doctrine_object_constructor public: truejms_serializer.unserialize_object_constructor: class: App\Serializer\ObjectConstructor
If anyone can explain to me how to ignore the id in this case I'm open to any suggestions.
Regards and thanks for any help