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
Disable serializer for FOSUserBundle entities #78
Comments
I had the same problem recently and there is a new section on overriding metadata in docs. I can tell you that this overriding won't work with annotations. |
Thanks @mvrhov Is there no fix for it? /ping @schmittjoh |
@mvrhov I moved my annotations to a YAML file. But I'm still getting the properties of the FOSUserBundle. How did you fix this? |
Read Overriding Metadata section in docs. |
Already did, but that doesn't work.. or I don't know how to do it... |
From the top of my head... jms_serializer:
metadata:
directories:
FOSUB:
namespace_prefix: FOS\UserBundle
path: %kernel.root_dir%/serializer/FOSUB # %kernel.root_dir%/serializer/FOSUB/Model.User.yml
FOS\UserBundle\Model\User:
exclusion_policy: NONE
properties:
emailCanonical:
exclude: true
usernameCanonical:
exclude: true |
You can still use annotations in your model, but obviously not for a model that is provided by a third-party where you can't change the code. |
My UserEntity looks like this:
I annotate my User class with @ExclusionPolicy(all) so that everything is excluded by default. I @expose a couple of properties in my own User class And then all the inherited properties of the FOS User class will get exposed automatically... I think that is bad because I said in my User class to Exclude everything by default? |
@schmittjoh Could this please be reopened? |
@ruudk the solution has been posted by @mvrhov: #78 (comment) |
Yes but that only works if I don't use annotations. But why isn't is possible to use annotations? When I put @ExclusionPolicyAll(all) on top of MY class then it should respect that for the inheritance of the FOSUser class too, right? Because the FOSUser class doesn't even have any Serializer annotations or what so ever. |
@ruudk you can still use annotations for your own classes. You cannot use annotations for FOSUserBundle classes as adding them would require editing third party code. |
The exclusion policy only affects the class in which it is defined, not any parent entities. |
<?php
abstract class Parent {
protected $property;
}
class Child extends Parent {
/**
* @Exclude
*/
protected $property;
} Doing this should work. /cc @schmittjoh @stof |
Hello,
and my file
Maybe someone can help me ? |
You have to clear the cache ! |
My bad, thanks :) |
Just a quick note that might be obvious for some but had me scratching my head for a good while... If you're using MongoDB with FOSUserBundle you still need to set this up on FOS\UserBundle\Model\User rather than FOS\UserBundle\Document\User because you can't influence parent classes as previously mentioned. |
+1 for this solution. Didn't know Overriding Third-Party Metadata section in documentation. Saved my day! Thank you all! ;) |
I was checking my code, and for some unknown reason, overriding metadata for FOSUserBundle is not working anymore. I was trying to discover what happened and I saw that Entity classes in FOSUserBundle was deprecated, acording to this commit: FriendsOfSymfony/FOSUserBundle@89be8e9. My code is exactly this:
And my file %kernel.root_dir%/serializer/FOSUB/Model.User.yml
Does anyone knows if I have to change something after this depraction in FOSUseBundle? I've tried to point config fo FOS\UserBundle\Model, but it didn't work also. @schmittjoh any ideia? |
@hugomn did you clear your cache? |
Yes, a lot of times. And I've tried a lot of combinations for |
I've been thinking about adding something to the Symfony profiler which lists the location where metadata was loaded from. If someone has time to contribute something. |
@schmittjoh I can try it, I'm just not so familiar with JMSSerializerBundle. The metadata for each directory is loaded in the file JMSSerializerExtension, right? |
We need to add the location from where the metadata was loaded by the drivers to the ClassMetadata (this needs to be done in schmittjoh/serializer and schmittjoh/metadata) and then add a data collector for the Symfony profiler to this bundle. |
@schmittjoh sorry, I don't know how to do it... =/ |
No worries, maybe someone else comes along and can help :) On Tue, Jan 7, 2014 at 9:48 PM, Hugo Magalhães notifications@github.comwrote:
|
@hugomn I am also seeing something similar to you I am not able to exclude anything anymore from FOSUserBundle. My user entity extends the FOSUserBundle User Model, and neither the expose rules in the annotations, or the serializer\Entity.User.yml are working. I tried it out on an entity not related to FOSUser and it is working just fine. Not sure what would stop it from using these rules even when I extended their model and am using a separate Entity class. |
@stof I've just copied your configs and it didn't work. I'm thinking maybe its versions issues. Which version of FOSUserBundle and JMSSerializerBundle are you using? Thanks! |
FOSUserBundle master branch (commit is FriendsOfSymfony/FOSUserBundle@c66cb66) |
@stof even using all bundles in master still not working. How is you User class definition? Mine is like this: <?php
namespace Acme\AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use FOS\UserBundle\Model\UserInterface;
use JMS\Serializer\Annotation\ExclusionPolicy;
/**
* @ExclusionPolicy("all")
*/
class User extends BaseUser implements UserInterface {
// my code
} |
@stof Those config changes worked for me, but only whenever I took out "duplicates" between the Model and my Entity. I tested by upgrading to dev-master of FOSUserBundle and it told me that username was duplicated. I am using Symfony 2.4.1, JMSSerializerBundle 0.13.0 and FOSUserBundle 1.3.3. However, if I stay on the versions above, all data except id is null:
When I upgrade to FOSUserBundle dev-master I get the correct data
|
well, if you use 1.3.3, why are you extending from Model directly ? It looks to me that you followed the documentation of FOSUserBundle dev-master while using the 1.3.x version |
@stoff I changed to extending the entity for 1.3.3 (updated config and my entity) and it doesn't seem to work anymore. I think I will stick to the dev-master version for now. Thanks for the help! |
It works for me # app/config/config.yml
jms_serializer:
metadata:
directories:
FOSUserBundle:
path: "%kernel.root_dir%/Resources/FOSUserBundle/serializer"
namespace_prefix: "FOS\\UserBundle" #app/Resources/FOSUserBundle/serializer/Model.User.yml
FOS\UserBundle\Model\User:
exclusion_policy: ALL
properties:
id:
expose: true
email:
expose: true
enabled:
expose: true
locked:
expose: true Packages: jms/metadata 1.5.0 Class/method/property metadata management in PHP
jms/parser-lib 1.0.0 A library for easily creating recursive-descent parsers.
jms/serializer 0.15.0 Library for (de-)serializing data of any complexity; supports XML, JSON, and YAML.
jms/serializer-bundle dev-master ca66cd3 Allows you to easily serialize, and deserialize data of any complexity
friendsofsymfony/user-bundle dev-master 23d15ad Symfony FOSUserBundle |
I still have the same problem. My conf: No way to make it work... |
I had this same problem. Then I found from http://stackoverflow.com/questions/12960141/jmsserializerbundle-no-control-over-third-party-meta-data that I was using the JMSSerializer method rather than the JMSSerializerBundle. I used:
which doesn't work, but this does:
Hope this helps someone else. |
Running into this same issue now with "jms/metadata": "~1.3", Used to work perfectly with older version. RESOLVED: I've located the issue down to a change in my yaml config. Make sure namespace_prefix is using double quotes and not single quotes for those of you who may run into the same issue. jms_serializer: |
@james-bw Thank you for your answer! |
I had the same error as you, don't forget to specify the groups if you use serializerGroups.
hope this help |
Of course @PierrickMartos ! Thank you to specify this precision for future readers ;) |
Is there a technical reason why this bundle cannot support using an @exclude annotation on an inherited property? I'm working on a project that is not using yaml files to configure Entities and it would be much easier to use an annotation. If there is such a reason, then it should be documented to avoid confusion rather than simply dismissing attempts to use the bundle in this way. |
+1 @cambot |
@igorladela, the project I was working on ended up writing custom functions to provide arrays containing subsets of the entity fields. We were already doing this to provide several different views for a single entity, so it was an easy way for us to get around this issue too. |
Solution as described in #78 Works after clearing the cache 👍 |
@mvrhov @schmittjoh @skonsoft @PierrickMartos @geoffreytran @hpatoio @stof Tried all these solutions and they don't work. Even after clearing the cache. What reason could be? My config is: #app/config/config.yml
jms_serializer:
metadata:
directories:
FOSUB:
namespace_prefix: "FOS\\UserBundle"
path: "%kernel.root_dir%/serializer/FOSUB" #app/serializer/FOSUB/User.yml
FOS\UserBundle\Model\User:
exclusion_policy: ALL
properties:
email:
expose: true
groups: [Registration, Profile, Default]
# enabled:
# expose: true
# locked:
# expose: true <?php
//src/UserBundle/Entity/User.php
namespace UserBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User extends BaseUser
{ |
My friend, you have to name your file as Model.User.yml Before:
After:
And then, of course, change the file name as well. Let me know if this helped you. |
the other solution if you want to keep the file name as |
Hello everybody, I've got a very similar problem into my project. I'm working on an API and I need to exclude some fields from my entities when they are requested. I try to implement the solutions presented here, but nothing works. Does somebody can help me ? |
Hello, I've spent a lot of time on these pitfalls, maybe the doc could have a tooltip to help prevent them :
|
@hugomn I'm using SF4 and I have the same issue have you figure out your problem? update:I noticed that the issue was related to wrong way of calling the serializer: I was doing :
now it's fixed. |
In addition to @bruno-ds comment, it may also be helpful to note that when overwriting third-party metadata, only files with the |
Hi guys, Since my upgrade to Symfony 3.4, I can't serialize the Here is my config config.yml
Model.User.yml (in app/serializer/FOSUB)
Now my API doesn't return me any attributes from the thrid-party class Any clue? |
@picks44 |
Hi @goetas, I don’t use the Symfony Serializer.... and it’s not related to a form. The API method is supposed to return the user info through a simple Doctrine findOneBy request. |
Can you post the full stack trace of the exception?
…On Mon, 18 Feb 2019, 12:42 VinZ ***@***.*** wrote:
Hi @goetas <https://github.com/goetas>, I don’t use the Symfony
Serializer.... and it’s not related to a form. The API method is supposed
to return the user info through a simple Doctrine findOneBy request.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#78 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAvaJ64JwkV6yNOwPHSJiVsWpohwgBx6ks5vOpGHgaJpZM4AD-SR>
.
|
Alright my bad, just some naughty cache in my test env... |
I am using the FOSUserBundle and have my own entity extended of the FOSUserBundle Model.
In my user entity I want to serialize some things. But the rest should be excluded.
So I added
@Serializer\ExclusionPolicy("all")
but somehow all the UserBundle's properties are exposed.In my cache directory I keep getting this file:
serializer/FOS-UserBundle-Model-User.cache.php
.How can I get rid of this?
The text was updated successfully, but these errors were encountered: