Andrey Lebedev
Andrey Lebedev

Reputation: 151

API Platform: Change embedded sub-resources to theirs @id

I'm using Symfony 4.3 and API Platform 2.4. In my API there are resource 'groups' and related sub-resource 'phones':

{
    "@context": "/contexts/groups",
    "@id": "/groups/7116",
    "@type": "groups",
    "id": 7116,
    "name": "Standard Test",
    "description": "",
    "phones": {
        "7848412": {
            "@id": "/phones/7848412",
            "@type": "phones",
            "id": 7848412,
            "phone": "+412344545656",
            "a": "o2",
            "b": "",
            "c": "",
            "d": "",
            "e": ""
        }
}

Resources YAMLs:

entities\ClientPhoneGroup:
  shortName: 'groups'
  description: "Phone's group"
  collectionOperations:
    post:
      denormalization_context:
        groups: ['write']
  itemOperations:
    get: ~
    put:
      denormalization_context:
        groups: ['write']
    delete: ~

  attributes:
    normalization_context:
      groups: ['read']
    security: "is_granted('IS_AUTHENTICATED_FULLY')"
    subresource_operations:
      phone_get_subresource:
        method: 'GET'

  properties:
    id:
      identifier: true
    phones:
      subresource:
        resourceClass: 'entities\ClientPhone'
        collection: false

and

entities\ClientPhone:
  shortName: 'phones'
  description: "Phones list by group"
  collectionOperations:
    post:
      path: /groups/{group_id}/phones
      requirements:
        group_id: '\d+'
      denormalization_context:
        groups: ['write']
  itemOperations:
    get: ~
    put:
      denormalization_context:
        groups: ['write']
    delete: ~
  attributes:
    normalization_context:
      groups: ['read']
    security: "is_granted('IS_AUTHENTICATED_FULLY') "

serializers YAMLs:

entities\ClientPhoneGroup:
  attributes:
    id:
      groups: ['read']
    name:
      groups: ['read', 'write']
    description:
      groups: ['read', 'write']
    phones:
      groups: ['read']

and

entities\ClientPhone:
  attributes:
    id:
      groups: ['read']
    phone:
      groups: ['read', 'write', 'edit']
    a:
      groups: ['read', 'write', 'edit']
    b:
      groups: ['read', 'write', 'edit']
    c:
      groups: ['read', 'write', 'edit']
    d:
      groups: ['read', 'write', 'edit']
    e:
      groups: ['read', 'write', 'edit']
  1. How to change embeded phones list to '/groups/7116/phones' in groups resource?
  2. How to add link to 'group' resource (e.g. /groups/7116) to the phone sub-resource?

Upvotes: 0

Views: 1024

Answers (1)

Vipulw
Vipulw

Reputation: 1283

Answer Question 2. Remove normalization_context:groups: ['read'] from entities\ClientPhone so when you will call a GET group resource api, phone sub-resource will have IRI instead of all fields.

Upvotes: 2

Related Questions