core icon indicating copy to clipboard operation
core copied to clipboard

Serialization on entity from Operation and not from Resource

Open lekeutch opened this issue 11 years ago • 6 comments

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?

lekeutch avatar Aug 04 '15 18:08 lekeutch

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?

theofidry avatar Sep 14 '15 13:09 theofidry

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.

Jarablus avatar Jun 20 '16 10:06 Jarablus

You can have that, just declare serialization on the operation level

soyuka avatar Jun 20 '16 10:06 soyuka

@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']}

soyuka avatar Jun 20 '16 13:06 soyuka

Feature added in v2.

dunglas avatar Aug 19 '16 22:08 dunglas

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

aistis- avatar Feb 03 '17 09:02 aistis-