Skip to content

JSON:API OpenAPI Schema duplicates relations #8308

Closed

Description

@ytsdv

API Platform version(s) affected: 4.3

Description
For resources using the JSON:API format, the generated OpenAPI schema documents relation properties twice: once correctly as resource linkage under relationships, and once incorrectly as a full nested object under attributes. The attributes variant never appears in an actual JSON:API response, so the generated spec misrepresents the API.

How to reproduce

  1. Using the getting started bookshop api create two resources Book and Review:
<?php
// api/src/Entity/Book.php
namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/** A book. */
#[ApiResource]
#[ORM\Entity]
class Book
{
    /** The ID of this book. */
    #[ORM\Id, ORM\Column, ORM\GeneratedValue]
    private ?int $id = null;

    #[ORM\Column]
    public string $title = '';

    /** @var Review[] Available reviews for this book. */
    #[ORM\OneToMany(targetEntity: Review::class, mappedBy: 'book', cascade: ['persist', 'remove'])]
    public iterable $reviews;

    public function __construct()
    {
        $this->reviews = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }
}
<?php
// api/src/Entity/Review.php
namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use Doctrine\ORM\Mapping as ORM;

/** A review of a book. */
#[ApiResource]
#[ORM\Entity]
class Review
{
    /** The ID of this review. */
    #[ORM\Id, ORM\Column, ORM\GeneratedValue]
    private ?int $id = null;

    #[ORM\Column(type: 'text')]
    public string $body = '';

    /** The book this review is about. */
    #[ORM\ManyToOne(inversedBy: 'reviews')]
    public ?Book $book = null;

    public function getId(): ?int
    {
        return $this->id;
    }
}
  1. Inspect the generated OpenAPI Documentation, the example response for GET /books will look like this:
{
  "links": {...},
  "meta": {...},
  "data": [
    {
      "id": "string",
      "type": "string",
      "attributes": {
        "id": 0,
        "title": "",
        "reviews": [
          "https://example.com/"
        ]
      },
      "relationships": {
        "reviews": {
          "data": [
            {
              "type": "string",
              "id": "path/실례.html"
            }
          ]
        }
      }
    }
  ],
  "included": [...]
}

Note the reviews property in attributes.

  1. Create a Book and a Review and query /books. The response looks like this, no property reviews in attributes:
{
  "links": {
    "self": "/books"
  },
  "meta": {
    "totalItems": 1,
    "itemsPerPage": 30,
    "currentPage": 1
  },
  "data": [
    {
      "id": "/books/1",
      "type": "Book",
      "attributes": {
        "_id": 1,
        "title": "test"
      },
      "relationships": {
        "reviews": {
          "data": [
            {
              "type": "Review",
              "id": "/reviews/1"
            }
          ]
        }
      }
    }
  ]
}

Possible Solution
In ApiPlatform\JsonApi\JsonSchema\SchemaFactory::buildDefinitionPropertiesSchema(), the relation properties are already identified while building relationships, but attributes is left as a $ref to the full resource schema that still contains them.
We would need to exclude those relation keys from the attributes schema instead of reusing the unmodified $ref.

Activity

  1. added 2 commits that reference this issue on Jun 16, 2026
    454a259
    75c275c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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