Serialization on entity from Operation and not from Resource
Hi,
I use API Platform for a personal project and i was blocked at level of the serialization on my entities.
In fact, i want to serialize my entities from my custom operations (Operation) and not from my custom resource (Resource). Let me explain: I have an A entity, I create
- a custom resource AResource (implements ResourceInterface),
- a custom controller AController (extends ResourceController)
- three configured operations like getA, getAs, and myA
I'll want put the serialization groups for each operation and not on the resource AResource in order to choice the attributes different to display.
Can someone help me?
Sorry for the late answer @lekeutch, is this issue still relevant? If yes can you provide us some details such as the version you're using?
Sorry to revive this zombie , but I have the same question with V2. I think it could be nice to have a serialization exemple in custom operation.
You can have that, just declare serialization on the operation level
@napalm33700 for example with annotations:
/**
* @ApiResource(
* attributes={"normalization_context"={"groups"={"get"}}},
* itemOperations={
* "get"={"method"="GET"},
* "put"={"method"="PUT", "normalization_context"={"groups"={"put"}}}
* }
* )
*/
It's the same using yaml:
job:
class: 'Acme\BlogBundle\Entity\Job'
itemOperations:
get:
method: 'GET'
normalization_context: {'groups': ['job_read', 'relation_read']}
post:
method: 'POST'
attributes:
normalization_context: {'groups': ['job_read']}
denormalization_context: {'groups': ['job_write']}
Feature added in v2.
Hi, where I could find an example of yaml configuration instead of annotation?
Edit: fount it https://api-platform.com/docs/core/getting-started#mapping-the-entities