We have some Entity classes that implement custom getter functions which return entities as well, as those are not properties there seems to be no way to exclude them. As soon as the ItemNormalizer uses the ResourceClassResolver to resolve the class for this property it will crash. What is the best way to approach this?
An example would be an Entity like this:
//...
class Category{
//...
/**
* @ORM\OneToMany(targetEntity="Category", mappedBy="parent", cascade={"remove"})
* @Groups({"read","write"})
*/
protected $children;
//Getters, setters etc. here - works fine
//..
//Custom method which will fail
public function getLastLeaf(){
//Skipping implementation
return $mySomeOtherEntityRelatedToThis;
}
}
Seems like this could be done using the setIgnoredAttributes function in the Normalizer used by the Serializer. But I have no idea what would be the best place to get that done.
So from investigating a little further I found out that currently there is no way to use the setIgnoredAttributes function as AbstractItemNormalizer overrides the function and completely ignores whatever was set before.
@dunglas
Do you think this is worth a patch or am I on the completely wrong path here?
Ok, I found a way - but I'm still not convinced this is the right way. I built a custom normalizer which is just a copy of the JSONLD normalizer - I implemented the ignoredAttributes handling there.
I think it would be a good addition to handle all of this in a way that the JMSSerializer does it by using an annotation for their dynamic exclusion strategy.
This would allow to just exclude some properties completely without rewriting the whole model classes.
@aggrosoft yes this feature should be supported, a patch would be very welcome!
This feature will be supported with https://github.com/api-platform/core/pull/1123! See https://github.com/api-platform/core/pull/1123/files#diff-3e45e8e6ce08f8a7cbbbda1601d5ff29R136 馃槈
@aggrosoft does it work for you ?
@Simperfit Is this documented somewhere? Would love to try that out but I do not understand how this is working.
@aggrosoft It's just the support of AbstractNormalizer::setIgnoredAttributes(): http://symfony.com/doc/current/components/serializer.html#ignoring-attributes 馃槈
No official annotation/configuration for ignored attributes though. I think you need to add your own normalizer extending api-platform's one to make this work no?
@meyerbaptiste, @Simperfit - setIgnoredAttributes() is discouraged in 4.1 - so the issue remains respectively will appear again.
Using groups makes is work for me.
Eg:
/**
* Layout
*
* @ApiResource(
* normalizationContext={"groups"={"layout:read"}},
* denormalizationContext={"groups"={"layout:write"}},
* )
*/
class Layout {
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=63, nullable=false)
* @Groups({"layout:read", "layout:write"})
* Available in get and post
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="hidden_field", type="string", length=63, nullable=false)
* @Groups({"none"})
* Excluded completely
*/
private $hiddenField;
Most helpful comment
Using groups makes is work for me.
Eg: