JMSSerializerBundle.无法控制第三方元数据 [英] JMSSerializerBundle. no control over third party meta data
问题描述
我有两个实体希望使用 JMSSerializerBundle 进行序列化.Music
实体有一个带有 exclusion_policy: NONE
的映射文件.
I have two entities I wish to serialize with the JMSSerializerBundle. The Music
Entity has a mapping-file with exclusion_policy: NONE
.
Music
实体具有来自 FOSUserBundle
的实体 User
的字段.User
实体有一个带有 exclusion_policy: ALL
的映射文件,其中一些字段设置为 expose: true
,因此它们将被序列化.
The Music
entity has a field of the entity User
from FOSUserBundle
. The User
entity has a mapping-file with exclusion_policy: ALL
with a few fields set to expose: true
, so they will be serialized.
问题是,User
字段被完全序列化.如果我更改 User
实体的映射文件并不重要.
The problem is, the User
field gets fully serialized. It does not matter if I change the mapping-file of the User
entity.
这是它的样子:
#My/Bundle/Resources/config/serializer/Entity.Music.yml
xxx\xxx\Entity\Music:
exclusion_policy: NONE
#My/Bundle/Resources/config/serializer/Entity.User.yml
xxx\xxx\Entity\User:
exclusion_policy: ALL
properties:
id:
expose: true
username:
expose: true
username_canonical:
exclude: true
email:
exclude: true
email_canonical:
exclude: true
enabled:
exclude: true
salt:
exclude: true
password:
exclude: true
last_login:
exclude: true
confirmation_token:
exclude: true
password_requested_at:
exclude: true
groups:
exclude: true
locked:
exclude: true
expired:
exclude: true
expires_at:
exclude: true
roles:
expose: true
credentials_expired:
exclude: true
credentials_expired_at:
exclude: true
为什么不引用它自己的映射文件?还是我哪里弄错了?
Why does it not refer to it's own mapping file? Or am I mistaken somewhere?
我已阅读第三方元数据文档.它只是说在我的序列化程序服务中添加一个新目录.我已经这样做了,但我必须扩展 FOS\UserBundle\Entity
类,并且它也无法访问我试图排除的父级受保护字段.
I have read the third party meta data documentation. It simply says to add a new directory in my serializer service. I have done that, but I have to extend the FOS\UserBundle\Entity
class, and that also does not have access to the parent protected fields I'm trying to exclude.
推荐答案
我敢打赌 xxx\xxx\Entity\User:
指的是你的自己的命名空间和类.
I bet xxx\xxx\Entity\User:
refers to your own namespace and class.
如果是,那是错误的做法.
必须将规则应用于属性所在的类.
The rules must be applied to the class where the properties live.
鉴于您在配置中公开的属性,我猜您正在使用 FOSUserBundle
.
Given the property you exposed in your configuration, I guess you're using FOSUserBundle
.
因此,您必须在 FOS\UserBundle\Model\User
上应用您的规则.
Therefore, you must apply your rules on FOS\UserBundle\Model\User
.
然后您需要添加一个 JMSSerializer
配置以指示给定命名空间的序列化器元数据所在的位置.
Then you need to add a JMSSerializer
config to indicate where the serializer metadata live for the given namespace.
它应该是这样的:
jms_serializer:
metadata:
auto_detection: true
directories:
FOSUserBundle:
namespace_prefix: "FOS\\UserBundle"
path: "@YourUserBundle/Resources/config/serializer/fos"
在 fos/
目录中你应该有 Model.User.yml
In fos/
directory you should have Model.User.yml
类似于:
FOS\UserBundle\Model\User:
exclusion_policy: ALL
properties:
id:
expose: true
groups: [list, details]
username:
expose: true
groups: [details]
email:
expose: true
groups: [me]
roles:
expose: true
groups: [details]
<小时>
详情:
当通过元数据将规则应用于序列化程序时,序列化程序会在元数据中定义的类中查找声明的属性.
When applying rules to the Serializer through metadata, the Serializer looks for the property which are declared inside the class which is defined in the Metadata.
示例:
class Foo {
protected $foo;
}
class Bar extends Foo {
protected $bar;
}
您的元数据将如下所示:
Your metadata will look like this:
Foo:
exclusion_policy: ALL
properties:
foo:
expose: true
Bar:
exclusion_policy: ALL
properties:
bar:
expose: true
下面的例子不是正确的做法
Bar:
exclusion_policy: ALL
properties:
foo:
expose: true
bar:
expose: true
如果您这样做,则只会应用(并公开)属性 bar
上的规则.
if you do this, only the rules on the property bar
will be applied (and exposed).
这篇关于JMSSerializerBundle.无法控制第三方元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!