JSON:API: resource schemas (`*.jsonapi`) have `properties` but no `type: object`

Offen Anfängerfreundlich
#8,701 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 2 Tagen

Ein zugehöriger Pull Request wurde bereits gemerged.

  • #8724 von @ousamabenyounes — gemerged

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
Unter einer Stunde
Anfängerfreundlichkeit
80/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
openapi, php
Bereich
api

Rechercherichtung

Das Problem wird im Issue genannt: JsonApi\JsonSchema\SchemaFactory::buildSchema(), wo Item-Definitionen ihre properties zugewiesen bekommen. Lies diese Methode und füge dann type mit dem Wert object direkt neben dieser Zuweisung hinzu, aber nur für die Item-Schemas. Fertig ist es, wenn die exportierte Spezifikation aus bin/console api:openapi:export "type": "object" bei Book.jsonapi, Order.jsonapi, Order.OrderInput.jsonapi und OrderLine.jsonapi zeigt und die bestehenden Tests für JSON:API-Schemas weiterhin bestehen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

API Platform version(s) affected

4.4.4

Description

The JSON:API resource document schemas, such as Book.jsonapi and the input schemas like Order.OrderInput.jsonapi, have only a properties key at the top level. They have no type: object.

A schema without type also accepts a string, number, array or null, because properties applies only to objects. Validators and fuzzers that generate requests from the spec (for example Schemathesis) then send bodies like "foo" or 42, which the API never accepts. The nested data objects already declare "type": "object". Only the top level is missing it.

How to reproduce

Any resource with the jsonapi format shows it, for example the Book resource from #8699. Generated schema:

"Book.jsonapi": {
  "properties": {
    "data": { "type": "object", "properties": { "...": "..." } }
  }
}

In a minimal app with the resources from #8698 and #8699, these four schemas have no type: Book.jsonapi, Order.jsonapi, Order.OrderInput.jsonapi, OrderLine.jsonapi.

Expected: every *.jsonapi document schema declares "type": "object".

Possible Solution

In JsonApi\JsonSchema\SchemaFactory::buildSchema(), set $definitions[$definitionName]['type'] = 'object' next to the properties assignment for item schemas.

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.)
109

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.