JMS 序列化捆绑包@exclude注释不起作用


JMS serialize bundle @exclude annotation not working

My entity FosUserBundle

namespace AppBundle'Entity;
use JMS'Serializer'Annotation'Expose;
use JMS'Serializer'Annotation'Exclude;
use JMS'Serializer'Annotation'ExclusionPolicy;
use Doctrine'ORM'Mapping as ORM;
use FOS'UserBundle'Model'User as BaseUser;
use FOS'UserBundle'Model'Group;
/**
 * User
 *
 * @ORM'Table(name="user")
 * @ORM'Entity(repositoryClass="AppBundle'Repository'UserRepository")
 * @ExclusionPolicy("all")
 */
class User extends BaseUser
{
    /**
     * @ORM'Id
     * @ORM'Column(type="integer")
     * @ORM'GeneratedValue(strategy="AUTO")
     * @Exclude
     */
    protected $id;
    /**
     * @ORM'Column(type="integer")
     */
    private $balance = 0;

但是如果我尝试序列化应用程序''实体''用户对象:

$this->get('jms_serializer')->serialize($user, 'json');

它返回了我的ID字段!

 {
  "id": 1,
  "username": "admin",
  "username_canonical": "admin",
  "email": "admin",
  "email_canonical": "admin",
  "enabled": true,
  "salt": "o12yxgxp3vkk0w4sck80408w8s8o84s",
  "password": "$2y$13$o12yxgxp3vkk0w4sck804uSVjSMSB1W0qwEjunGTHomBqqoGvkW9G",
  "last_login": "2016-02-28T17:28:19+0300",
  "locked": false,
  "expired": false,
  "roles": [
    "ROLE_ADMIN"
  ],
  "credentials_expired": false
}

由于 FOSUserBundle 序列化程序规则,排除策略将不起作用。

若要使用排除项,需要告诉 JMSSerializer 压缩 FOSUB 序列化。

config.yml 中添加以下内容:

jms_serializer:
    metadata:
        auto_detection: true # Don't forget this line
        directories:
            FOSUB:
                namespace_prefix: FOS'UserBundle
                path: %kernel.root_dir%/serializer/FOSUB

然后,创建文件app/serializer/FOSUB/Model.User.yml并将以下内容粘贴到:

FOS'UserBundle'Model'User:
    exclusion_policy: ALL

现在,所有继承的属性都被排除在外,您可以公开所需的属性。

如果要保留某些属性,或在实体中重新定义它们,请根据需要对其进行调整。

请参阅相应的问题 #78。

更新

对于不起作用的@Expose注释,请添加auto_detection,如我前面的示例所示。

如果这不能解决问题,请使用@Groups注释并执行以下操作:

use JMS'Serializer'Annotation'Groups;
/**
 * @ORM'Column(type="integer")
 * @JMS'Groups({"user"})
 */
private $balance = 0;

像这样更改控制器:

use JMS'Serializer'SerializationContext;
$context = SerializationContext::create()->setGroups(array('user'));
$serializer->serialize($user, 'json', $context);

它应该有效。

请记住,此问题的存在只是因为您的 User 实体扩展了已具有序列化程序规则的类。

        $serializer = SerializerBuilder::create()
            ->setSerializationContextFactory(function () {
                return 'JMS'Serializer'SerializationContext::create()
                    ->setSerializeNull(true)
                    ;
            })
            ->setDeserializationContextFactory(function () {
                return 'JMS'Serializer'DeserializationContext::create()
                    ->setSerializeNull(true)
                    ;
            })
            ->setAnnotationReader($annotationReader)
            ->setPropertyNamingStrategy(new SerializedNameImportantThanPropertyNameStrategy())
            ->build();

忽略某些父类:CComponent、CModel、CActiveRecord

        $class = new 'ReflectionClass($serializer);
        $property = $class->getProperty('navigator');
        $property->setAccessible(true);
        $navigator = $property->getValue($serializer);
        $class = new 'ReflectionClass($navigator);
        $property = $class->getProperty('metadataFactory');
        $property->setAccessible(true);
        $metadataFactory = $property->getValue($navigator);
        $class = new 'ReflectionClass($metadataFactory);
        $property = $class->getProperty('loadedClassMetadata');
        $property->setAccessible(true);
        $property->setValue($metadataFactory, [
            'CComponent' => new NullMetadata(new 'stdClass()),
            'CModel' => new NullMetadata(new 'stdClass()),
            'CActiveRecord' => new NullMetadata(new 'stdClass()),
        ]);