JSON:API: `JsonApiCollectionBaseSchemaNoPagination` declares `data` as `type: array` without `items`

Geschlossen Anfängerfreundlich
#8,699 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 2 Tagen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
82/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
openapi, php
Bereich
api

Rechercherichtung

Beginne in JsonApi\JsonSchema\SchemaFactory::buildSchema() und suche die Definition von COLLECTION_BASE_SCHEMA_NAME_NO_PAGINATION, in der data als type: array ohne items deklariert ist. Reproduziere das Problem mit bin/console api:openapi:export am Book-Beispiel und prüfe die Ausgabe mit einem OpenAPI-3.0-Validator. Fertig ist es, wenn data ein items-Schema hat (zum Beispiel {"type": "object"}), das 3.0-Dokument validiert und die bestehenden JSON:API-Schematests weiterhin laufen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

API Platform version(s) affected

4.4.4

Description

When pagination is disabled on a GetCollection operation, the JSON:API collection response refers to JsonApiCollectionBaseSchemaNoPagination. In that schema, data is {"type": "array"} with no items.

OpenAPI 3.0 requires items when type is array, so 3.0 validators and some code generators reject the document. In 3.1 and 3.2 the schema is valid, but it says nothing about the members.

How to reproduce
#[ApiResource(operations: [
    new Get(provider: [self::class, 'provide']),
    new GetCollection(paginationEnabled: false, provider: [self::class, 'provideAll']),
])]
final class Book
{
    public function __construct(public int $id = 1, public string $title = 'Dune') {}
    public static function provide(): self { return new self(); }
    /** @return list<self> */
    public static function provideAll(): array { return [new self()]; }
}

Generated schema (shortened):

"JsonApiCollectionBaseSchemaNoPagination": {
  "type": "object",
  "properties": {
    "links": { "...": "..." },
    "meta": { "...": "..." },
    "data": { "type": "array" }
  },
  "required": ["data"]
}

Expected: data has an items schema, for example {"type": "object"}. The concrete collection schema can still narrow it with allOf.

Possible Solution

Add 'items' => ['type' => 'object'] to data in the COLLECTION_BASE_SCHEMA_NAME_NO_PAGINATION definition in JsonApi\JsonSchema\SchemaFactory::buildSchema().

Additional Context

Checked on api-platform/core v4.4.4, Symfony 7.4, PHP 8.5, in an empty Symfony app. The code of JsonApi\JsonSchema\SchemaFactory on main is the same as in v4.4.4.

The app has only FrameworkBundle, TwigBundle and ApiPlatformBundle, with this config:

$c->extension('api_platform', [
    'mapping' => ['paths' => ['%kernel.project_dir%/src/ApiResource']],
    'formats' => ['jsonapi' => ['application/vnd.api+json']],
    'docs_formats' => ['jsonopenapi' => ['application/vnd.openapi+json']],
    'doctrine' => ['enabled' => false],
]);

The spec comes from bin/console api:openapi:export.

Vorherrschende Sprache
PHP
Sterne
2.6k
Forks
988
Ø Merge
4 T. 10 Std.
Gemergte PRs (30 T.)
92

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus api-platform/core

Alle Issues in api-platform/core

Ähnliche Issues

Weitere Issues zu PHP

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.