Skip to content

Swagger documentation of related entities isn't proper #986

@kate-kate

Description

@kate-kate

Hello everyone.
I have a Job entity with related Campaign. According to https://api-platform.com/docs/core/serialization-groups-and-relations I configured them like this:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * Job
 * @ApiResource(attributes={"normalization_context"={"groups"={"dashboard"}}})
 * @ORM\HasLifecycleCallbacks
 * @ORM\Table(name="job")
 * @ORM\Entity
 */
class Job
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @Groups({"dashboard"})
     * @ORM\Column(name="status", type="integer", nullable=false)
     */
    private $status;

    /**
     * @Groups({"dashboard"})
     * @ORM\Column(name="postcode", type="string", length=255, nullable=true)
     */
    private $postcode;

    /**
     * @Groups({"dashboard"})
     * @ORM\ManyToOne(targetEntity="Campaign")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="campaign_id", referencedColumnName="id")
     * })
     */
    private $campaign;
 
    // ...
}
<?php
namespace AppBundle\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * Campaign
 * @ORM\HasLifecycleCallbacks
 * @ApiResource(attributes={"normalization_context"={"groups"={"dashboard"}}})
 * @ORM\Table(name="campaign")
 * @ORM\Entity
 */
class Campaign
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     * @ORM\Column(name="external_id", type="string", length=255, nullable=true)
     */
    private $externalId;

    /**
     * @var string
     * @Groups({"dashboard"})
     * @ORM\Column(name="name", type="string", length=255, nullable=false)
     */
    private $name; 

    // ...
}

And it works perfectly OK, when I get the response body, but the example value of response is quite strange.

Example value swagger shows to me

[
  {
    "status": 0,
    "postcode": "string",
    "campaign": {
      "status": 0,
      "postcode": "string",
      "campaign": {}
    }
  }
]

As you can see, there are job fields expected from campaign instead of its own

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions