Q:

How to display nested documents in API Platform

I'm trying to display nested documents as a default, meaning when I query the api, it should return the documents with their relationships.
Two of my entities:
<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Traits\TimestampableTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass="App\Repository\ClaimRepository")
 * @ORM\Table("claims")
 */
class Claim
{
    use TimestampableTrait;

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Activity", mappedBy="claim", cascade={"persist", "remove"})
     */
    private $activities;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\ClaimState", inversedBy="claims", cascade={"persist", "remove"})
     * @ORM\OrderBy({"id" = "desc"})
     */
    private $states;

And the other one:
<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Traits\TimestampableTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass="App\Repository\ActivityRepository")
 * @ORM\Table("activities")
 */
class Activity
{
    use TimestampableTrait;

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Claim", inversedBy="activities")
     */
    private $claim;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Invoice", mappedBy="activity", orphanRemoval=true, cascade={"persist","remove"})
     */
    private $invoices;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\ActivityError", mappedBy="activity", cascade={"persist","remove"})
     */
    private $errors;

When I do GET /claims I would like it to display the Activity relations with all the properties, what I get right now is:
{
  "data": {
    "id": "/claims/7",
    "type": "Claim",
    "attributes": {
      "_id": 7,
      "name": "My Valid Claim",
      "created": "2019-01-07T15:14:21+01:00",
      "updated": "2019-01-07T15:14:21+01:00"
    },
    "relationships": {
      "activities": {
        "data": [
          {
            "type": "Activity",
            "id": "/activities/7"
          }
        ]
      }
    }
  }
}

I would like to get something like this:
{
  "data": {
    "id": "/claims/7",
    "type": "Claim",
    "attributes": {
      "_id": 7,
      "name": "My Valid Claim",
      "created": "2019-01-07T15:14:21+01:00",
      "updated": "2019-01-07T15:14:21+01:00"
    },
    "relationships": {
      "activities": {
        "data": [
          {
            "type": "Activity",
            "id": "/activities/7",
            "attributes": {
              "_id": 7,
              "name": "Activity no. 1",
              "created": "2019-01-07T15:14:21+01:00",
              "updated": "2019-01-07T15:14:21+01:00"
            },
            "relationships": {
              "invoices": {
                "data": [
                  {
                    "type": "Invoice",
                    "id": "/invoices/7",
                    "attributes": {
                      "_id": 7,
                      "vendor": "Vendor Ltd."
                    }
                  }
                ]
              }
            }
          }
        ]
      }
    }
  }
}

The format used is JSON:API. I tried using groups, but it doesn't help at all and I can't find anything in any documentation.
Any help is appreciated.

A:

https://api-platform.com/docs/core/serialization/#embedding-relations
So in your example add normalizationContext={"groups"={"claim"}} to Claim entity, @Groups({"claim"}) to Claim properties, and then @Groups("claim") to the properties of Activity class that you would like to embed in the response.

