【问题标题】:JMSSerializerBundle deserialization skip groups exclusion on id property using DoctrineObjectConstructorJMSSerializerBundle 反序列化使用 DoctrineObjectConstructor 跳过对 id 属性的排除组
【发布时间】:2019-01-28 14:32:40
【问题描述】:

我在 symfony 4.2 上使用 jms/serializer-bundle 2.4.3,我注意到我的应用程序中有一个烦人的问题: 当我发布一个实体时,DoctrineObjectConstructor 在内容中使用 id 来检索另一个实体,从而在它被我的安全组排除时对其进行修补

看实体

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;
}

控制器

/**
 * @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']]);
}

我在服务中有一些 JMS 配置更改

jms_serializer.object_constructor:
    alias: jms_serializer.doctrine_object_constructor
    public: true

jms_serializer.unserialize_object_constructor:
    class: App\Serializer\ObjectConstructor

如果有人可以向我解释如何在这种情况下忽略 id,我愿意接受任何建议。

感谢您的帮助

【问题讨论】:

  • 您是否尝试将ExclusionPolicy 设置为全部?
  • 你好,是的,我试过了,但我得到了相同的结果。我终于找到了解决方案,方法是覆盖 DoctrineObjectConstructor 并排除其 id 上没有上下文反序列化组的对象。现在像魅力一样工作,无论如何谢谢!

标签: symfony doctrine symfony4 jmsserializerbundle


【解决方案1】:

要解决,只需在您的 services.yaml 中添加覆盖

jms_serializer.doctrine_object_constructor:
    class: App\Serializer\DoctrineObjectConstructor
    arguments:
        - '@doctrine'
        - '@jms_serializer.unserialize_object_constructor'

jms_serializer.object_constructor:
    alias: jms_serializer.doctrine_object_constructor

并添加更新的本地 DoctrineObjectConstructor 以忽略 id 属性上没有当前反序列化组的实体

class DoctrineObjectConstructor implements ObjectConstructorInterface
{
const ON_MISSING_NULL      = 'null';
const ON_MISSING_EXCEPTION = 'exception';
const ON_MISSING_FALLBACK  = 'fallback';

private $fallbackStrategy;
private $managerRegistry;
private $fallbackConstructor;

/**
 * Constructor.
 *
 * @param ManagerRegistry            $managerRegistry     Manager registry
 * @param ObjectConstructorInterface $fallbackConstructor Fallback object constructor
 * @param string                     $fallbackStrategy
 */
public function __construct(ManagerRegistry $managerRegistry, ObjectConstructorInterface $fallbackConstructor, $fallbackStrategy = self::ON_MISSING_NULL)
{
    $this->managerRegistry     = $managerRegistry;
    $this->fallbackConstructor = $fallbackConstructor;
    $this->fallbackStrategy    = $fallbackStrategy;
}

/**
 * {@inheritdoc}
 */
public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $data, array $type, DeserializationContext $context)
{
    // Locate possible ObjectManager
    $objectManager = $this->managerRegistry->getManagerForClass($metadata->name);

    if (!$objectManager) {
        // No ObjectManager found, proceed with normal deserialization
        return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
    }

    // Locate possible ClassMetadata
    $classMetadataFactory = $objectManager->getMetadataFactory();

    if ($classMetadataFactory->isTransient($metadata->name)) {
        // No ClassMetadata found, proceed with normal deserialization
        return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
    }

    // Managed entity, check for proxy load
    if (!\is_array($data)) {
        // Single identifier, load proxy
        return $objectManager->getReference($metadata->name, $data);
    }

    // Fallback to default constructor if missing identifier(s)
    $classMetadata  = $objectManager->getClassMetadata($metadata->name);
    $identifierList = [];

    foreach ($classMetadata->getIdentifierFieldNames() as $name) {
        $propertyGroups = [];

        if ($visitor instanceof AbstractVisitor) {
            /** @var PropertyNamingStrategyInterface $namingStrategy */
            $namingStrategy = $visitor->getNamingStrategy();
            $dataName       = $namingStrategy->translateName($metadata->propertyMetadata[$name]);
            $propertyGroups = $metadata->propertyMetadata[$name]->groups;
        } else {
            $dataName = $name;
        }

        if (!array_key_exists($dataName, $data) || true === empty(array_intersect($context->getAttribute('groups'), $propertyGroups))) {
            return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
        }

        $identifierList[$name] = $data[$dataName];
    }

    // Entity update, load it from database
    $object = $objectManager->find($metadata->name, $identifierList);

    if (null === $object) {
        switch ($this->fallbackStrategy) {
            case self::ON_MISSING_NULL:
                return null;
            case self::ON_MISSING_EXCEPTION:
                throw new ObjectConstructionException(sprintf('Entity %s can not be found', $metadata->name));
            case self::ON_MISSING_FALLBACK:
                return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
            default:
                throw new InvalidArgumentException('The provided fallback strategy for the object constructor is not valid');
        }
    }

    $objectManager->initializeObject($object);

    return $object;
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多