Skip to content

OpenAPI & documentation

The bundle generates a faithful, rich OpenAPI 3.1 description of your JSON:API surface directly from the metadata you already declare — your resources, fields, constraints, relationships, filters, sorts, paginators, custom actions and security expressions. There is no second source of truth to keep in sync: the document is projected from the live registry, served live, exportable for CI, and — crucially — provably describes the responses you actually serve (see conformance).

Because the projection reads first-class JSON:API metadata (not a generic PHP-attribute scrape), it can express the JSON:API document shape precisely: typed attribute schemas from your constraints, linkage and compound-document envelopes, enumerated filter[…] / sort / include / fields[…] / page[…] parameters, and the standard error-status set per operation.

A sort parameter whose vocabulary holds a sort family lists the fixed keys alongside one pattern per family, and names the families under x-sort-families (core's projection). That holds on related and relationship endpoints too, whose vocabulary merges the related type's sorts with the relation's own.

Filters work the same way. A list filter gains a filter[<key>][] parameter for its array form, and the filter families an endpoint accepts share one filter parameter listed under x-filter-families. The bundle passes the declared filters through unchanged, so the document describes exactly the keys the data layer accepts.

Per-operation response declarations

By default each operation advertises one success response — POST → 201, PATCH → 200, DELETE → 204, GET → 200. A resource overrides the set per operation with the create/update/delete/fetchOne/fetchCollection params on #[AsJsonApiResource], and a custom action with its responds set. This is how the asynchronous-write lifecycle (async writes) becomes part of the contract:

  • new Accepted('export-jobs') → a 202 Accepted whose body is the export-jobs document, with Content-Location (the poll URL) and Retry-After headers.
  • new SeeOther() on fetchOne → a 303 See Other with a Location header (the completed job redirecting to the produced resource), no body.
  • new NoContent() → a 204; new MetaResult() → a 200 meta-only document.

So a codegen client sees, from the document alone, that POST /catalog-exports returns 202 pointing at an export-jobs resource, and that GET /export-jobs/{id} may 303 to the finished export. These responses ride core's shared projection, so the Laravel package's document for the same domain is byte-identical.

Enabling generation

OpenAPI generation is on by default. The HTTP routes that serve it are gated for safety (see exposure), but the document is always buildable via the CLI.

# config/packages/json_api.yaml
json_api:
  base_uri: 'https://api.example.com'
  version: '1.1'
  openapi:
    enabled: true            # generation available at all (CLI always works)
    info:
      title: 'My API'
      version: '1.0.0'
      description: 'A JSON:API surface.'

Import the docs routes once, alongside your resource routes:

# config/routes/json_api.yaml
json_api:
  resource: .
  type: jsonapi

json_api_openapi:
  resource: .
  type: jsonapi_openapi   # mounts /docs.json and /docs.yaml (+ /{server}/docs.*) and /docs

The routes

Route Path Serves
Document (default server) GET /docs.json the OpenAPI 3.1 JSON document
Document (named server) GET /{server}/docs.json that server's document (per-server mode)
YAML document (default server) GET /docs.yaml the same document as YAML, when YAML is on
YAML document (named server) GET /{server}/docs.yaml that server's document as YAML (per-server mode)
Viewer GET /docs a Swagger UI or ReDoc page rendering the document
JSON Schemas (default server) GET /schemas.json the per-type JSON Schemas, keyed by type
JSON Schemas (named server) GET /{server}/schemas.json that server's schemas (per-server mode)

All paths are configurable (json_api.openapi.json.path, …yaml.path, …ui.path, …json_schema.path). The document and the schemas are served as application/json, and the YAML document as application/yaml; they are not JSON:API routes, so they carry no application/vnd.api+json negotiation.

Try it live. The examples/music-catalog-symfony app serves these routes. Run docker compose up in that directory, then open http://localhost:8080/docs for the Swagger UI (raw document at /docs.json; the admin server's document at /admin/docs.json).

Exposure: the docs routes

The HTTP routes are auto-exposed when kernel.debug is true (your dev environment) and off in production unless you opt in. The CLI export is always available.

json_api:
  openapi:
    expose_in_prod: true   # serve /docs.json + /docs.yaml + /docs + /schemas.json outside kernel.debug too

The viewer route additionally honours ui.enabled, and the JSON Schema routes honour json_schema.enabled, so you can serve the raw document in prod while keeping the human viewer (or the schemas) dev-only — or any other combination.

Profile-gated parameters: x-profile

Some query parameters are recognised by the runtime only when a JSON:API profile is negotiated on the request (its URI supplied via the profile media-type parameter of Accept / Content-Type). withCount is the built-in case — the count tokens are honoured only under the Countable profile.

The served document advertises this constraint honestly: every such parameter carries an x-profile vendor extension whose value is the profile URI that must be negotiated for the parameter to take effect. A client (or a codegen tool) that does not negotiate that profile should treat the parameter as inert. For example, the withCount parameter on a countable collection is emitted as:

{
  "name": "withCount",
  "in": "query",
  "schema": { "type": "array", "items": { "type": "string", "enum": ["_self_", "tracks"] } },
  "style": "form",
  "explode": false,
  "x-profile": "https://haddowg.github.io/json-api/profiles/countable/"
}

The extension is produced by the core projection (so it is present on every served build path — the warmer, the controllers, and the CLI export alike); the bundle simply serves the projected document verbatim.

The YAML document

The document is also served as YAML at /docs.yaml, and at /{server}/docs.yaml for a named server. In combined mode the one YAML document sits at the YAML path, as the JSON one sits at the JSON path. It needs symfony/yaml:

json_api:
  openapi:
    yaml:
      enabled: ~         # on when symfony/yaml is installed, off otherwise
      path: /docs.yaml

enabled: true without symfony/yaml installed fails the container build and tells you to composer require symfony/yaml. The bundle registers the YAML routes behind the same expose gate as the JSON ones.

The YAML is the JSON document in another syntax. Each request builds the JSON body, serve decorators, pruning and all, and dumps that as YAML. An empty object stays {}, an empty array stays [], and a string that reads like a number or a boolean, such as '200' or 'true', is quoted, so a YAML parser reads back exactly what docs.json holds. The response carries the same Cache-Control and answers If-None-Match the same way, with its own ETag: the JSON document's tag with the format folded in, so a cache never hands out one for the other. The --format=yaml export and the public_path .yaml file are dumped the same way.

The links the server builds (each resource's self, relationship self and related, the top-level self, pagination links and action links) render as JSON:API link objects. When the document is served, each one carries a describedby pointing at the operation in the document that describes its target, and the document gets a top-level describedby naming the operation that produced it:

// GET /api/articles/1, with the routes imported under /api
{
  "data": {
    "type": "articles",
    "id": "1",
    "links": {
      "self": {
        "href": "https://example.test/api/articles/1",
        "describedby": "/api/docs.json#/paths/~1articles~1%7Bid%7D/get"
      },
      "publish": {
        "href": "https://example.test/api/articles/1/-actions/publish",
        "describedby": "/api/docs.json#/paths/~1articles~1%7Bid%7D~1-actions~1publish/post"
      }
    },
    "relationships": {
      "author": {
        "links": {
          "self": {
            "href": "https://example.test/api/articles/1/relationships/author",
            "describedby": "/api/docs.json#/paths/~1articles~1%7Bid%7D~1relationships~1author/get"
          },
          "related": {
            "href": "https://example.test/api/articles/1/author",
            "describedby": "/api/docs.json#/paths/~1articles~1%7Bid%7D~1author/get"
          }
        }
      }
    }
  },
  "links": {
    "self": {
      "href": "https://example.test/api/articles/1",
      "describedby": "/api/docs.json#/paths/~1articles~1%7Bid%7D/get"
    },
    "describedby": { "href": "/api/docs.json#/paths/~1articles~1%7Bid%7D/get" }
  }
}

The fragment is a JSON Pointer to the path and method in paths. An action link names the action's first declared method. A link whose operation the document doesn't carry gets no describedby, so none points at a fragment that isn't there.

The document URL is the path of the server's document route, host-relative and with the prefix the routes are imported under: /docs.json for the default server and /{server}/docs.json for a named one, or /docs.json for every server in combined mode. When the document lives somewhere else, such as a docs site or a CDN serving the public_path files, name it with document_url. It's used as given, with {server} replaced by the server name (or combined in combined mode), the same names the public_path files use:

json_api:
  openapi:
    document_url: 'https://docs.music.example/openapi/{server}.json'

The bundle picks the document URL for a server in this order:

  1. document_url, when it's set.
  2. The bundle's own document route, jsonapi.openapi.default or jsonapi.openapi.server.
  3. Any other route whose controller is OpenApiController, for an app that routes the document itself rather than importing the jsonapi_openapi routes. The controller can be written as the class, OpenApiController::class . '::__invoke' or the array form. The route serves the server named by its server default, the default server when it has none, or every server its {server} requirement accepts. A route that names the server wins over a parametric one. In combined mode any such route serves every server.
// config/routes.php
use haddowg\JsonApiBundle\Controller\OpenApiController;

$routes->add('api_v5_docs', '/api/v5/docs.json')
    ->controller(OpenApiController::class)
    ->defaults(['server' => 'v5']);

// The same document as YAML: the same controller with a format default.
$routes->add('api_v5_docs_yaml', '/api/v5/docs.yaml')
    ->controller(OpenApiController::class)
    ->defaults(['server' => 'v5', 'format' => 'yaml']);

A route with no format default serves JSON, so describedby only ever points at a JSON route. A hand-declared YAML route gets everything the bundle's does: the serve decorators, the cache keys and ETag, and application/yaml. It answers 404 while yaml.enabled is off. Like a hand-declared JSON route it isn't held to the expose gate, which only decides whether the bundle registers its own routes, so expose it as you route it.

The third step reads the whole route collection, so it doesn't run per request. The warmer scans the routes at cache:warmup and writes the result beside the documents. Without one, the first request that needs it scans once and writes it. In debug the scan goes into its own file under %kernel.cache_dir%/json_api_openapi, checked against the same route files and container the router's matcher cache is checked against. A request with nothing changed reads that file and never loads the routes, and a route you add or move is picked up on the next request without clearing the cache.

When describedby is on, no document_url is set, the expose gate is open, and no route serves a server's document, the warmup logs a warning naming the server and suggesting document_url or a document route. Links for that server carry no describedby, and nothing fails, since describedby is optional. A closed expose gate is a deliberate choice, so it gets no warning. Only a server that serves JSON:API routes is checked: one whose routes you never import, such as an implicit default server in an app that declares only named servers, renders no links and gets no warning.

Links get no describedby when the document isn't served and no document_url is set (the expose gate is closed, or generation is disabled), or when describedby: false. They stay link objects either way. To render bare URL strings instead, set json_api.link_objects: false, or the same key on a named server. A string can't carry a describedby, but the document's own top-level one still renders.

An Atomic Operations result's top-level describedby names the atomic endpoint's POST (/docs.json#/paths/~1operations/post with the default atomic_operations.path), and the resource objects inside it carry the same links as anywhere else. Error documents get none.

A describedby set on the response with withDescribedby(), or one inside its withLinks(), replaces the per-operation one, so set it only when a response should point somewhere else.

The generated document describes these links with one shared component per context, ResourceLinks, DocumentLinks, CollectionLinks, RelationshipLinks, RelationshipDocumentLinks and RelatedDocumentLinks, each listing the members that context can render. The link schemas carry no examples. The operation examples show each response whole, so that's where a type's own link URLs and their describedby appear.

A type whose resources carry named links gets its own <Type>ResourceLinks, an allOf of ResourceLinks and an object with one property per named link. An asLink action is listed there by its name. So is an action the resource links with an ActionLink, described by its describedAs() when it sets one.

A resource's declared links are named, optional properties of that second allOf entry. A bare link takes the open LinkObject schema; a link object gets its own schema with href, each static member as a const, and a meta object built from the declared members the way attributes are. The operation examples carry each link, built from its example() and its meta members' examples, and a missing one is reported like a missing attribute example, as links.<name> or links.<name>.meta.<member>. Core's OpenAPI page shows the full schema.

The warmer writes a small map of each document's operations beside it, which is all a request reads to resolve a describedby. Without a warmed map (in development, say) the first request builds the document once and writes the map.

Note

A describedby points into the document as the warmer built it. A per-request decorator that removes an operation for some requests doesn't remove the describedby pointing at it, so a client served the trimmed document can be handed a fragment it doesn't contain.

The viewer

A single config-driven route renders the document with Swagger UI or ReDoc (one, not both), as plain CDN-linked HTML — no Twig dependency, no asset build step.

json_api:
  openapi:
    ui:
      enabled: true
      renderer: swagger    # swagger | redoc
      path: /docs
      cdn: ~               # override the pinned CDN origin (self-host / air-gap)
      redoc_version: 3.0.1 # the exact ReDoc release; a 2.x one such as 2.5.4 also works
      redoc_telemetry: false

ReDoc defaults to 3.0.1. Its standalone bundle is an ES module, so the page loads it with <script type="module"> and it starts itself from the <redoc spec-url> tag. ReDoc 3 sends usage telemetry unless told not to, so the page renders disable-telemetry="true" on that tag. Set redoc_telemetry: true to leave the attribute off and let ReDoc send it.

redoc_version takes an exact release, never a range. A 2.x release such as 2.5.4 (the OpenApiUiController::REDOC_VERSION_2 constant) switches the page to the 2.x loader, a classic <script> that defines the Redoc global. ReDoc 2 has no telemetry, so redoc_telemetry does nothing there. Any other value fails the container build.

ReDoc offers the document for download through its downloadUrls option: a JSON entry, and a YAML one when the YAML document is served. ReDoc 3 reads it as JSON from a download-urls attribute on the <redoc> tag. ReDoc 2 reads attributes only as strings, so its page mounts on a plain <div> and calls Redoc.init with the option. Swagger UI's page has no download list and is unchanged.

The viewer shows the default server's document. A viewer route you declare yourself can set a server default to show a named server's, and its downloads follow:

use haddowg\JsonApiBundle\Controller\OpenApiUiController;

$routes->add('api_v5_viewer', '/api/v5/docs')
    ->controller(OpenApiUiController::class)
    ->defaults(['server' => 'v5']);

Your own documentation page

The viewer gets its URLs from the public DocumentUrls service, and a page of your own can do the same instead of hard-coding them:

use haddowg\JsonApiBundle\OpenApi\DocumentUrls;

public function __construct(private readonly DocumentUrls $urls) {}

$this->urls->json('v5');      // '/api/v5/docs.json'
$this->urls->yaml('v5');      // '/api/v5/docs.yaml', or null when YAML isn't served
$this->urls->downloads('v5'); // [['title' => 'JSON', 'url' => …], ['title' => 'YAML', 'url' => …]]

The server defaults to the default server, and in combined mode every server gets the combined document's URLs. Each URL is generated by the router, host-relative and with the current request's script base and any import prefix. It comes from the bundle's document route when the jsonapi_openapi routes are imported. Otherwise it comes from a route you declared to OpenApiController whose server and format defaults match, found the same way describedby finds the JSON one. Either method returns null when no route serves the document in that format.

The page loads Swagger UI / ReDoc from a pinned public CDN (jsDelivr) and points it at the document route, generated via the router (so the spec URL honours any routing prefix you mount the imported routes under — ->prefix('/api') — as well as the front-controller script base; the viewer and the document it points at always share the same mount). For an air-gapped deployment, mirror the assets and set ui.cdn to your origin; document a matching script-src / style-src CSP allowance for whichever origin you use.

ui.cdn replaces the directory the assets load from. For Swagger UI that's the swagger-ui-dist package root. For ReDoc it's the package's bundles/ directory, the one holding redoc.standalone.js, for either major: the page loads <cdn>/redoc.standalone.js. Mirror the release redoc_version names. A 3.x bundle is a module script, so the server must send it with a JavaScript Content-Type, and from another origin it also needs an Access-Control-Allow-Origin header, since browsers fetch module scripts with CORS.

To replace the viewer wholesale, register your own controller on ui.path — because your app imports the docs route loader, your own GET /docs route wins by registration order.

Production: the cache warmer

The document is never built per request (a serve decorator adjusts the pre-built one). A CacheWarmer pre-builds each server's document, the per-type JSON Schemas, and the aggregate schema document at cache:warmup — i.e. on every deploy — into %kernel.cache_dir%; the controllers then serve the pre-built artifacts with an O(file read). In dev (kernel.debug) the controllers lazy-build and cache on demand, since resources change between edits.

The warmer is optional and non-fatal: a documentation-generation failure never breaks a deploy (it logs and the controller's lazy build is the safety net). To also emit a fully static file a web server / CDN can serve with zero PHP:

json_api:
  openapi:
    public_path: '%kernel.project_dir%/public/openapi'   # writes <server>.json (+ .yaml + <server>.schemas.json) at cache:warmup

Serving the JSON Schemas

Each type's resource object is also published as a standalone JSON Schema 2020-12 document — the same projection the OpenAPI components use, wrapped with the $schema dialect keyword and an addressable $id so it validates on its own. They are served over HTTP as a single aggregate keyed by JSON:API type at GET /schemas.json (and GET /{server}/schemas.json per named server) — one fetch a client generator consumes to drive an opt-in request/response validation seam:

{
  "albums":  { "$schema": "https://json-schema.org/draft/2020-12/schema", "$id": "urn:jsonapi:schema:albums",  "type": "object", "properties": { "type": { "const": "albums" }, ... } },
  "tracks":  { "$schema": "...", "$id": "urn:jsonapi:schema:tracks",  ... }
}

The routes ride the same expose gate as the document, with their own toggle and path:

json_api:
  openapi:
    json_schema:
      enabled: true          # register GET /schemas.json (+ /{server}/schemas.json); default true
      path: /schemas.json    # the default server's aggregate path

In combined multi-server mode GET /schemas.json serves one aggregate spanning every server. The same schemas are available without HTTP exposure from the CLI (json-api:json-schema:export).

Exporting from the CLI

Two commands publish the document and the standalone schemas with no HTTP exposure — for CI spec-diffing, publishing and codegen. They establish the bundle's json-api: command namespace.

# The OpenAPI document (JSON to stdout, or a file; YAML needs symfony/yaml):
php bin/console json-api:openapi:export
php bin/console json-api:openapi:export --server=admin --format=yaml --output=openapi.yaml
php bin/console json-api:openapi:export --strict > openapi.json   # fail on a missing example or undeclared scope
php bin/console json-api:openapi:export --compact --output=openapi.min.json   # one line, no indentation

# Standalone per-type JSON Schema 2020-12 documents:
php bin/console json-api:json-schema:export --type=articles            # one type → stdout
php bin/console json-api:json-schema:export --output=build/schemas/    # every type → directory
php bin/console json-api:json-schema:export --output=build/schemas/ --compact
php bin/console json-api:json-schema:export --server=admin --output=build/schemas/admin/

Both commands pretty-print JSON by default. --compact writes each document on a single line with the same encoding otherwise: slashes and unicode stay unescaped, empty objects stay {}, and the trailing newline is there exactly when the pretty form has one. It works with stdout and --output. YAML has no compact form, so the OpenAPI export rejects --compact with --format=yaml and exits with status 2.

The standalone JSON Schemas are projected by the same core projector the OpenAPI document uses, so a standalone <type>.json and the in-document <Type>Resource component agree. Its meta is typed inline as <Type>ResourceMeta when the type declares resourceMeta(), and a relation declaring identifier meta or pivot fields gets a relationships entry whose identifiers' meta is typed as its <Type><Rel>IdentifierMeta, with the read <Type><Rel>Pivot inline under pivot. The rest of each relationship object, links, and the meta of a type declaring none stay open objects. A request's top-level meta and a write's data.meta are not part of a resource object, so it does not describe them.

The OpenAPI export lists every operation example it could not build as a warning on stderr, one line per missing value, so the document on stdout stays valid to pipe. --strict turns any of them into a non-zero exit and writes no document, which suits a CI step that guards the examples. The served document and the cache warmer never report them.

It also warns about each OAuth scope an operation needs that neither your scope catalogue nor the scope scheme's flows describe, and fails on them under --strict. A scope scheme that isn't oauth2 is a warning only.

Customisation

Inline authoring (descriptions & examples)

Add ->describedAs() and ->example() to any field, relation or filter builder; the projection surfaces them on the corresponding schema / parameter.

use haddowg\JsonApi\Resource\Field\Str;

Str::make('title')
    ->required()->minLength(3)->maxLength(120)
    ->describedAs('The article headline, shown in listings.')
    ->example('JSON:API in PHP');

The constraints you already declare (minLength, pattern, in, min/max, …) map 1:1 to JSON Schema 2020-12 keywords — that fidelity is the whole reason the target is 3.1. The few constraints with no lossless keyword (When with a dynamic condition, CompareField, the After/Before/Between date bounds) degrade to a human-readable note in the schema description rather than emitting a wrong schema.

Describing filters and sorts

Every filter and sort can carry its own description, and the bundle reports what core needs to give each filter[<key>] and sort parameter an example. Core's filters and sorts pages cover the builders. In short:

  • A presence filter, meaning WhereHas, WhereDoesntHave, WhereNull, WhereNotNull, a fixed Where or an all-fixed group, takes describedAs(). The document adds "Applied when present; value ignored." and always shows true as the example.
  • SortByField and SortFamily take describedAs(). Described keys are listed as a table in the sort parameter's description and in its x-sort-descriptions map, and a sortable field's sort reuses the field's description.
  • FilterFamily takes describedAs() and example($suffix, $value).
  • The sort example is the resource's sortExample() when it returns one, else its defaultSort(), else the first sort ascending. A relation's related and relationship endpoints show the relation's own defaultSort() first.
  • A filter's default() shows as the default of its parameter's schema, except on a relation's endpoints when the relation drops it.
  • The Doctrine-only WhereHasMatching takes describedAs() and example() like core's value filters.
public function filters(): array
{
    return [WhereHas::make('tracks')->describedAs('Only albums with at least one track.')];
}

public function sortExample(): array
{
    return ['-releasedAt', 'title'];
}

The music-catalog example's AlbumResource declares both, and describes the duration sort on its tracks relation.

Operation examples

Every operation's request body and success responses, and each shared error response, carry a whole worked example built from the ->example() values your fields declare (core's operation examples). Nothing is invented: an operation that needs a value with no declared example gets no example, and json-api:openapi:export names the type and field it was missing. Declare an Id example too, since every resource object and linkage needs one.

Linkage to a type the server doesn't register has no Id example to borrow, so give the relation an exampleId() (see relationships). A collection whose paginator counts on every request, one built with withCount(), shows meta.total, meta.page.total and the last link as it is served. The bundle reads that from the same paginator the endpoint uses: the relation's own, then the related resource's pagination(), then the server default. A cursor paginator never counts every request, so a countable cursor collection shows the meta.total a withCount=_self_ request gets, with no meta.page.total or last.

operationExamples on #[AsJsonApiResource] (or #[AsJsonApiSerializer]) replaces or switches off an operation's example. Keys are core's operation names: fetchCollection, fetchOne, create, update, delete, and for a relation fetchRelated.<relation>, fetchRelationship.<relation>, updateRelationship.<relation>, addRelationship.<relation> and removeRelationship.<relation>. A core OperationExample replaces the request and the responses it names and leaves the rest generated; false emits none:

use haddowg\JsonApi\OpenApi\Metadata\OperationExample;
use haddowg\JsonApiBundle\Attribute\AsJsonApiResource;

#[AsJsonApiResource(operationExamples: [
    'fetchOne' => new OperationExample(responses: [200 => ['data' => ['type' => 'playlists', 'id' => '1', 'attributes' => ['title' => 'Road trip']]]]),
    'update' => false,
])]
final class PlaylistResource extends AbstractResource { /* … */ }

An unknown key, or a CRUD operation the type doesn't expose, fails the container build; a relation the type doesn't have fails the projection. A hand-written serializer declares no fields, so its operations have nothing to build from; switch them off with false or write them out.

A custom action takes one example on #[AsJsonApiAction], with the same meaning, which is the way to show a meta document:

#[AsJsonApiAction(
    type: 'albums',
    path: 'summary',
    scope: ActionScope::Collection,
    responds: [new MetaResult()],
    example: new OperationExample(responses: [200 => ['meta' => ['albums' => ['total' => 15]]]]),
)]

Example documents are written as PHP arrays and stored in the compiled container as JSON, so an empty array always renders as []. json_api.openapi.examples: false turns generation off for every server, and a named server's own openapi_examples overrides it.

Describing types, operations & relationships

Every documentable element has a generated default description, and every one of them is overridable declaratively (never by reaching into the document with a decorator). There are three override surfaces:

  • a type's resource-object schema — #[AsJsonApiResource(description: …)], or the resource's getDescription(): ?string method hook;
  • a CRUD operation (collection / fetch-one / create / update / delete) — #[AsJsonApiResource(operationDescriptions: […])] keyed by the Operation case name, or the resource's describeOperation(OperationType $op): ?string method hook;
  • a relationship's related + relationship operations — ->describedAs(…) on the relation field (the one description applies to every endpoint of that relationship).
use haddowg\JsonApi\OpenApi\Metadata\OperationType;
use haddowg\JsonApi\Resource\AbstractResource;
use haddowg\JsonApi\Resource\Field\BelongsTo;
use haddowg\JsonApiBundle\Attribute\AsJsonApiResource;
use haddowg\JsonApiBundle\Operation\Operation;

#[AsJsonApiResource(
    description: 'A sellable product in the catalog.',
    operationDescriptions: [
        Operation::FetchCollection->name => 'Browse the product catalog.',
    ],
)]
final class ProductResource extends AbstractResource
{
    public function fields(): array
    {
        return [
            // … the relation description flows onto GET /products/{id}/category
            //    and GET /products/{id}/relationships/category.
            BelongsTo::make('category', 'categories')
                ->describedAs('The catalog category this product belongs to.'),
        ];
    }

    // Equivalent to the attribute fields above; useful when the text is computed.
    public function getDescription(): ?string
    {
        return 'A sellable product in the catalog.';
    }

    public function describeOperation(OperationType $op): ?string
    {
        return $op === OperationType::Delete ? 'Permanently retires a product.' : null;
    }
}

Precedence is method hook → attribute → generated default: when a resource declares both, the method hook wins (it is the more specific, runtime surface). Returning null (the method default) or omitting the attribute key leaves the generated default in place. An unknown operationDescriptions key fails the build with a clear error.

The generated summaries and descriptions name one resource with the singular of its type name and the right article, so agencies reads "Fetch an agency" and export-jobs "Fetch an export job". Collections keep the plural ("List agencies"). When the inflection reads badly for your type, name the singular yourself, with #[AsJsonApiResource(singular: 'media item')] or the resource's getSingularName(): ?string hook. Give the noun without an article; the generator picks "a" or "an". The same precedence applies, the method hook first, and a blank singular: fails the build.

#[AsJsonApiResource(singular: 'media item')]
final class MediaResource extends AbstractResource
{
    public static string $type = 'media';
}

Typed identifier meta

A relation's identifierMeta() resolver is free-form, so its members are an open meta in the document until you declare them with fields:. The bundle reports them to core's projector, which adds a closed <Type><Rel>IdentifierMeta component and points each response linkage identifier's meta at it. On a belongsToMany the component lists pivot among its own properties. See parent-aware identifier meta.

Typed document meta

A resource's documentMeta() declares the members of each operation's top-level meta, with the field builders an attribute takes (see core's typed document meta). The music catalog's albums count the explicit albums on each page:

use haddowg\JsonApi\OpenApi\Metadata\OperationType;
use haddowg\JsonApi\Resource\Field\Integer;

public function documentMeta(OperationType $operation, ?string $relation = null): iterable
{
    return match ($operation) {
        OperationType::FetchCollection => [
            Integer::make('explicitCount')->required()->min(0)
                ->describedAs('How many of the albums on this page are marked explicit.')
                ->example(1)
                ->extractUsing(static fn (array $albums): int => \count(\array_filter(
                    $albums,
                    static fn (mixed $album): bool => $album instanceof Album && $album->explicit,
                ))),
        ],
        default => [],
    };
}

The document then answers GET /albums with an AlbumsFetchCollectionDocument and GET /artists/{id}/albums with an ArtistsAlbumsFetchRelatedDocument, each narrowing the shared document's meta to a closed object marked x-document-meta. A type composed without a resource declares the same method by implementing haddowg\JsonApi\Serializer\DeclaresDocumentMetaInterface on its standalone serializer.

An action's handler declares its response documents' members per status by implementing haddowg\JsonApi\Resource\DeclaresActionDocumentMetaInterface, since an #[AsJsonApiAction] argument cannot hold field builders. The responds list stays as it is, and the bundle reads the handler service, without a request, when it builds the action's metadata. The music catalog's reissue action reports the album's previous release date:

public function documentMeta(int $status): iterable
{
    return match ($status) {
        200 => [DateTime::make('previousReleasedAt')->required()->nullable()->example('1997-05-21T00:00:00+00:00')],
        default => [],
    };
}

public function handle(ActionContext $context): DataResponse
{
    // …
    return $context->data($album)->withMeta(['previousReleasedAt' => $previous?->format(\DATE_ATOM)]);
}

A delete's documentMeta(OperationType::Delete) members apply when the type declares a meta-only 200 with delete: [new MetaResult()]. The delete then answers that document with the members filled, their hooks receiving the resource it removed. An After hook may still answer otherwise, and a resource document a soft-delete hook renders is not checked against them.

At runtime the CRUD handler fills each member whose extractUsing() hook supplies it once the handler and its After hooks have built the response. The hook receives the page of resources as a list for a collection, the one resource otherwise, and the request, and may type its first parameter accordingly. A collection a singular filter answers with one resource hands its hooks that resource, and a to-one related endpoint with nothing related hands them null. A value a hook or the handler sets with withMeta() wins, and a member without a hook is left to them. The same members reach a related endpoint serving the type, its FetchCollection ones for a to-many and its FetchOne ones for a to-one, and each result of an atomic batch. A relationship endpoint serves linkage only and carries none. A collection keeps the page and total members the bundle writes there.

With kernel.debug on, the view listener checks each declaring response's top-level meta, and the atomic loop checks each result that renders a document. Either throws DocumentMetaMismatch, rendered as a 500, for a missing required member, an undeclared member or a value of the wrong JSON type. Production checks nothing. A collection member named page or total, a member declared twice, or an action's members declared both on its response and by its handler fails the cache warm-up.

Typed request and resource object meta

A resource's requestDocumentMeta() declares the top-level meta each write may send, and its resourceMeta() the meta of its resource objects, read and written (see core's typed request meta). The music catalog's albums say where a create came from, required inside an atomic batch, render their release year, and take a note for the editors that nothing stores:

use haddowg\JsonApi\Resource\MetaContext;
use haddowg\JsonApi\Resource\RequestMetaContext;

public function requestDocumentMeta(RequestMetaContext $context): iterable
{
    if (!$context->is(OperationType::Create)) {
        return [];
    }
    $source = Str::make('source')->in(['web', 'import', 'api'])->example('web');

    return [$context->isAtomic() ? $source->required() : $source];
}

public function resourceMeta(MetaContext $context): iterable
{
    return [
        Integer::make('releaseYear')->readOnly()->nullable()->example(1969)
            ->extractUsing(static fn (Album $album): ?int => $album->releasedAt === null ? null : (int) $album->releasedAt->format('Y')),
        Str::make('editorNote')->writeOnly()->maxLength(500)->example('Remastered edition'),
    ];
}

POST /albums then takes an AlbumsCreateRequestDocument, the shared create request narrowed to that meta and marked x-request-meta, and AlbumsResource points its meta at AlbumsResourceMeta, marked x-resource-meta, with AlbumsCreateResourceMeta and AlbumsUpdateResourceMeta typing the requests' data.meta. An atomic add of an album is an AlbumsAtomicAddOperation of its own, carrying the atomic declaration's meta. A type composed without a resource implements core's DeclaresRequestDocumentMetaInterface on its hydrator and DeclaresResourceMetaInterface on its serializer.

The handler validates every write's meta before it hydrates, as it validates attributes: a member the declaration does not name is a 422 VALIDATION_FAILED at /meta/<member> or /data/meta/<member>, and so is a value its constraints, its JSON type or its strict temporal format refuse, each number first put in its member's type. Inside an atomic batch each operation's meta is checked against what its type declares for an atomic operation, at /atomic:operations/<n>/meta/<member>, whether the operation names its target by ref or by href, and the batch's own meta stays free-form. A type declaring nothing keeps meta open in both directions.

An update validates its meta after its security gate, as it validates its attributes, and a delete validates its body in a BeforeDeleteEvent listener below the delete gate, so a caller a gate refuses gets its 403 whatever the body holds. A create validates its meta before its BeforeSave gate, as its attributes.

The typed values reach the write: $event->writtenMeta() on the create, update, delete and relationship events, HookContext::writtenMeta() in a resource's lifecycle hooks, and ActionContext::requestMeta() in an action. data.meta members declaring fillUsing() are applied to the entity as it hydrates; the others stay on the written meta. A write whose type declares no request meta for it has an empty writtenMeta()->document, as an action declaring none has an empty requestMeta(), and one whose type declares no resourceMeta() for it has an empty writtenMeta()->resource. The meta and data.meta as sent stay on the request.

An action's handler declares its request meta per HTTP method by implementing core's DeclaresActionRequestMetaInterface, read from the handler service like its response meta. A document-input action's data.meta is validated and applied against its input type's resourceMeta(), as a create of that type is.

A relationship write answers a meta-only 200 when its type's documentMeta() declares members for that write's case, OperationType::UpdateRelationship, AddRelationship or RemoveRelationship, which covers that write on every relationship of the type. Its second parameter, ?string $relation = null, receives the name of the relation written, so an override can declare one relationship alone; the other relationships then answer their linkage. Its hooks receive the parent, and it is documented as <Type><Rel><Kind>RelationshipDocument on that relationship's operation. A create's or update's 202 job document carries what documentMeta() declares for CreateAccepted or UpdateAccepted, and an action's 202 what its handler's documentMeta(202) declares; the persister's or action's own meta wins.

Every declared meta object, pivot fields and identifierMeta() members included, is closed: a write sending an undeclared meta.pivot member is now a 422. One AdditionalProperties::make() entry in a declaration opens it, its of() typing the extra members, its extractUsing() returning extra members to render and its fillUsing() taking the written ones. On a pivot the hook receives the join row being written: the association entity of a belongsToMany, or each link row of an identified MorphToMany, whether the relation declares its link row fields or writes its rows through its own fillUsing().

With kernel.debug on, the view listener and the atomic loop check every rendered resource object's meta, and every linkage identifier's pivot and identifier meta, against the declarations, and throw ResourceMetaMismatch, rendered as a 500, for a missing required member, an undeclared member of a closed object or a value of the wrong JSON type. A declared member always wins over an AdditionalProperties entry's member of the same name, so with kernel.debug on the request listener also marks each request, an atomic batch's operations included, for core's render-time check: an entry returning a declared member's name throws ResourceMetaMismatch, or DocumentMetaMismatch for a document's top-level meta, naming it. Production checks nothing and renders the declared member.

getMeta() is deprecated, for removal in the next major. A resource returning [AdditionalProperties::make()->extractUsing(fn ($object, $request) => /* what getMeta() returned */)] from resourceMeta() renders the same meta and documents the object as open. A resource that overrides getMeta() while declaring resourceMeta() fails the cache warm-up, and so does a member declared twice, a second AdditionalProperties entry, or request meta on an action taking a raw body.

Two documents stay untyped by design: a meta-only response an After hook answers in place of a data document, since the operation's documented shape is the data document, and the top-level jsonapi.meta, which is server-level.

Vendor extensions

#[AsJsonApiResource(extensions: […], operationExtensions: […])] adds OpenAPI x- extensions to a type's operations, for example to tell a client generator which feature gates a resource:

use haddowg\JsonApiBundle\Attribute\AsJsonApiResource;
use haddowg\JsonApiBundle\Operation\Operation;

#[AsJsonApiResource(
    extensions: ['x-feature' => 'agency'],
    operationExtensions: [
        Operation::Delete->name => ['x-feature' => 'agency-admin', 'x-rate-limit' => 10],
    ],
)]
final class AgencyResource extends AbstractResource { /* … */ }

extensions goes on every operation the document emits for the type: the five CRUD operations, every related and relationship endpoint, and the type's custom actions. operationExtensions is keyed by the Operation case name and merged over extensions for that CRUD operation, so DELETE /agencies/{id} above carries x-feature: agency-admin and x-rate-limit: 10, and every other operation carries x-feature: agency. A type that declares neither renders exactly as before.

operationExtensions also takes the operation names that operationExamples and operationScopes use, which reach a single related or relationship endpoint as well as a CRUD operation: fetchCollection, fetchOne, create, update, delete, and for a relation fetchRelated.<relation>, fetchRelationship.<relation>, updateRelationship.<relation>, addRelationship.<relation> and removeRelationship.<relation>. The usual reason is to keep one endpoint out of the tools an MCP server builds from the document:

#[AsJsonApiResource(
    operationExtensions: [
        'fetchRelated.agencies' => ['x-mcp' => false],
    ],
)]
final class UserResource extends AbstractResource { /* … */ }

json-api-mcp leaves out the tool for an operation carrying x-mcp: false. An x-mcp object with name, title or description keeps the tool and overrides those fields instead. It applies to every operation that becomes a tool: the CRUD operations, custom actions, the related reads, and the relationship read of a relation with no related endpoint, which becomes a <type>_<relation>_linkage tool keyed fetchRelationship.<relation>. Relationship writes, and the relationship read of a relation that has a related endpoint, never become tools, so x-mcp on them is ignored.

The relationship read of a queriedBy() relation, and its related read when it has one, carry x-mcp: {"parentLinkage": false}, which tells the MCP server the relation's linkage is read from those operations, since a parent's document never renders it. An x-mcp you declare for fetchRelationship.<relation> or fetchRelated.<relation> merges with it: an object keeps its members and gains parentLinkage: false unless it sets parentLinkage itself, and x-mcp: false stays as it is. it is.

Each operation takes extensions first, then the set under its Operation case name, then the set under its operation name, and a later set wins on a shared key. A CRUD operation takes one key or the other, so declaring both Delete and delete on one type fails the container build.

Every key must be a full extension name starting with x-. A bare name such as feature, x- on its own, a name under the OAS-reserved x-oai-/x-oas- prefixes, or an unknown operation key fails the container build, and so does an operationExtensions entry for an operation the type doesn't expose (outside operations, a write on a readOnly: true resource, or a relationship mutation on a type without Update). The bundle can't see a type's relations while it builds the container, so a relation the type doesn't have, such as fetchRelated.agnecies, fails when the document is generated instead, naming the operations the type does expose. Values can be any JSON-encodable scalar or array. There is no method hook for extensions, so when the value has to be computed, add it with a decorator.

A custom action can carry its own on #[AsJsonApiAction(extensions: […])], merged over the type's set for that action's operation; see declaring how safe an action is.

A standalone serializer declares them the same way on #[AsJsonApiSerializer]. Its exposed operations are its operations list, so a serialize-only type can only declare extensions. Those still reach any custom action mounted on the type:

use haddowg\JsonApiBundle\Attribute\AsJsonApiSerializer;

#[AsJsonApiSerializer(
    type: 'charts',
    operations: [Operation::FetchCollection, Operation::FetchOne],
    extensions: ['x-feature' => 'charts'],
    operationExtensions: [Operation::FetchCollection->name => ['x-cache' => 'long']],
)]
final class ChartSerializer extends AbstractSerializer { /* … */ }

Deprecation

The document marks what you deprecate with OpenAPI's own deprecated: true, so Swagger UI, ReDoc and client generators show it without extra setup. Nothing changes at runtime beyond the Deprecation and Sunset headers described under response headers.

A type is deprecated by the same deprecation and sunset that send its headers:

#[AsJsonApiResource(deprecation: true, sunset: 'Sat, 31 Dec 2050 23:59:59 GMT')]
final class LegacyTrackResource extends AbstractResource { /* … */ }

With deprecation set, every operation the document emits for the type is deprecated: true: its CRUD operations, its related and relationship endpoints and its custom actions. A sunset adds a sentence naming the date to the end of each of those operations' descriptions, "This operation may stop responding on or after 2050-12-31, its sunset date.", whether or not the type is deprecated. The bundle reads the date out of an HTTP-date in any of its three forms, a structured-field date such as @2556143999, or an ISO 8601 date. Any other value still goes out in the Sunset header as written, but the document leaves the sentence out. The json_api.defaults and per-server defaults send their headers without marking anything in the document.

To deprecate single operations instead, list them in deprecatedOperations, by the names operationExamples uses:

#[AsJsonApiResource(deprecatedOperations: ['delete', 'fetchRelated.agencies'])]
final class UserResource extends AbstractResource { /* … */ }

Each operation listed is deprecated: true, and a response from it carries Deprecation: true, plus the type's Sunset if it declares one. An unknown name, an operation outside the type's operations or readOnly set, or a relationship mutation on a type without Update fails the container build. A relation the type doesn't have fails when the document is generated. A standalone serializer takes deprecatedOperations on #[AsJsonApiSerializer] the same way, checked against its own operations list.

A custom action declares its own with #[AsJsonApiAction(deprecated: true)]. Each of its methods is marked, and its responses carry the Deprecation header too.

Fields, relations, filters and sorts take a deprecated() call on their builders. A field is marked on its property in the attribute schemas, a relation on its property under relationships, a filter on its filter[…] parameter, and a sort is listed under the sort parameter's x-deprecated-sorts. These are documentation only, with no header, because the request still works and the member is still served. Core's fields, filters and sorts docs cover each one:

public function fields(): array
{
    return [
        Id::make(),
        Str::make('subtitle')->deprecated()->describedAs('Use `tagline` instead.'),
        BelongsTo::make('legacyOwner', 'users')->deprecated(),
    ];
}

public function filters(): array
{
    return [Where::make('code')->deprecated()];
}

A document with any of these markers declares the deprecation tier of its info.x-generator.contract, so a client generator that doesn't know about the markers warns rather than drops them. A sunset on its own is prose and leaves the stamp alone.

Tagging (grouping operations)

OAS tags drive how Swagger UI / ReDoc group operations. Each resource's operations are grouped under one tag; the default is the humanized, title-cased type (blog-post → Blog Posts). Override per resource and per action:

#[AsJsonApiResource(tags: ['Catalog'])]
final class ProductResource extends AbstractResource { /* … */ }

#[AsJsonApiAction(tags: ['Catalog'])]
final class RecalculatePrices implements ActionHandlerInterface { /* … */ }

An action with no tags inherits the resource tag of the type it mounts on. When the type is served by a different resource class on each server, each server's document uses that server's class's tags, for the resource and for its untagged actions. The top-level tag definitions (description, externalDocs, order) are config-authoritative; any tag referenced but not defined is auto-synthesized (name only):

json_api:
  openapi:
    tags:
      - name: Catalog
        description: 'Products and catalog operations'
        externalDocs: { url: 'https://docs.example.com/catalog' }

Enum descriptions

An In-constrained attribute sourced from a backed enum becomes a reusable named component (#/components/schemas/<Enum>), $ref'd everywhere it is used. Each reference carries the enum's backing type, string or integer, beside a single-entry allOf that holds it. A nullable field pairs that type and null with an anyOf of the reference and null. Either way the field reads the same type whether its enum is inline or hoisted. Declare per-value descriptions with the core enum trio:

use haddowg\JsonApi\Resource\Enum\DescribedEnum;
use haddowg\JsonApi\Resource\Enum\DescribesEnumCases;
use haddowg\JsonApi\Resource\Enum\EnumCaseDescription;

enum CatalogStatus: string implements DescribedEnum
{
    use DescribesEnumCases;

    #[EnumCaseDescription('Not yet visible in the catalog')] case Draft = 'draft';
    #[EnumCaseDescription('Live and listed')]                case Published = 'published';
    #[EnumCaseDescription('Withdrawn from sale')]            case Archived = 'archived';
}
Str::make('status')->enum(CatalogStatus::class);

The descriptions are emitted both as a markdown value → description table in the schema description (the only form the free CDN renderers show) and as x-enum-varnames / x-enum-descriptions vendor extensions (codegen-portable). Switch with enum_value_descriptions: both | extensions | description (default both).

Security schemes

Declare named security schemes; operations carrying a security* expression get the configured requirement. The authz expression itself is never parsed for scheme semantics — you declare the scheme, the projection attaches it.

json_api:
  openapi:
    security:
      schemes:
        bearer: { type: bearer, bearerFormat: JWT }   # bearer = shorthand for http + bearer
        apiKey: { type: apiKey, apiKeyName: X-Api-Key, in: header }
        oidc:   { type: openIdConnect, openIdConnectUrl: 'https://issuer.example.com/.well-known/openid-configuration' }
        oauth:                                         # full oauth2 flows graph
          type: oauth2
          flows:
            authorizationCode:
              authorizationUrl: 'https://auth.example.com/authorize'
              tokenUrl: 'https://auth.example.com/token'
              refreshUrl: 'https://auth.example.com/refresh'   # optional
              scopes:
                'catalog:read': 'Read the catalog'
                'catalog:write': 'Manage the catalog'
            # also: implicit | password | clientCredentials
      default_requirement: [bearer]

Every scheme type the OpenAPI 3.1 spec defines is supported — http/bearer, apiKey, openIdConnect, and oauth2 (with its implicit / password / clientCredentials / authorizationCode flows). A scheme that cannot be built (e.g. an oauth2 with no flow, or an openIdConnect with no URL) is dropped rather than emitting an invalid document, and a default_requirement naming a scheme that did not resolve is dropped with it (the document never carries a dangling security reference).

scope_scheme (default oauth2) names the scheme each operation's OAuth scopes are listed under. It is only read once some operation needs a scope.

#[AsJsonApiResource(securityRead: 'is_granted("ROLE_USER")')]
final class PlaylistResource extends AbstractResource { /* … */ }

Your own error codes

The document catalogues every error code the server can raise, each a const branch of a shape grouping the codes at one status, so a generated client gets a typed exception per code rather than one opaque error type. Core's codes are always there; point json_api.error_codes.paths at the directory your own described errors live in and they join them. See errors. A code only some operations raise declares a scope on its descriptor, or is registered through a ScopedErrorSource, and only those operations offer it; see scoping a code to the operations that raise it.

Wholesale customisation (the decorator)

For anything the projection can't express declaratively — a server variable, an extra scheme, per-individual-operation tags, vendor extensions, hand-written examples, or rewriting any part of the document — implement OpenApiFactoryInterface. It is autoconfigured and applied after the projection on every build path (warmer, controller, CLI), so your decorator gets the last word.

use haddowg\JsonApi\OpenApi\OpenApi;
use haddowg\JsonApiBundle\OpenApi\OpenApiFactoryInterface;

final class StampInternalServer implements OpenApiFactoryInterface
{
    public function decorate(OpenApi $document, string $server): OpenApi
    {
        return $document->withServers([
            ...$document->servers,
            new \haddowg\JsonApi\OpenApi\Server('https://internal.example.com', 'Internal'),
        ]);
    }
}

Register multiple by priority (#[AsTaggedItem] or a priority tag attribute); lower priority runs first, the highest-priority decorator gets the final mutation.

Per-request customisation

OpenApiFactoryInterface runs once per deploy, so it can't see who is asking. When one deployment serves several hosts that each need their own view of the spec (absolute servers URLs, operations a tenant hasn't paid for, example hosts), implement OpenApiServeDecoratorInterface instead. It runs every time docs.json or docs.yaml is served and gets the Request:

use haddowg\JsonApi\OpenApi\OpenApi;
use haddowg\JsonApi\OpenApi\Server;
use haddowg\JsonApiBundle\OpenApi\OpenApiServeCacheKeyInterface;
use haddowg\JsonApiBundle\OpenApi\OpenApiServeDecoratorInterface;
use Symfony\Component\HttpFoundation\Request;

final class ServersFromHost implements OpenApiServeDecoratorInterface, OpenApiServeCacheKeyInterface
{
    public function decorate(OpenApi $document, Request $request, string $server): OpenApi
    {
        return $document->withServers([new Server('https://' . $request->getHost() . '/api/v5')]);
    }

    public function cacheKey(Request $request, string $server): string
    {
        return $request->getHost();
    }
}

Hiding operations works well with the x-feature vendor extension: mark operations at build time, then drop the ones the requesting tenant lacks.

use haddowg\JsonApi\OpenApi\OpenApi;
use haddowg\JsonApi\OpenApi\Operation;
use haddowg\JsonApi\OpenApi\Paths;
use haddowg\JsonApiBundle\OpenApi\OpenApiServeCacheKeyInterface;
use haddowg\JsonApiBundle\OpenApi\OpenApiServeDecoratorInterface;
use Symfony\Component\HttpFoundation\Request;

final class FeatureGate implements OpenApiServeDecoratorInterface, OpenApiServeCacheKeyInterface
{
    public function __construct(private readonly TenantFeatures $features) {}

    public function decorate(OpenApi $document, Request $request, string $server): OpenApi
    {
        $enabled = $this->features->forHost($request->getHost());
        $items = [];
        foreach ($document->paths->items ?? [] as $path => $item) {
            $items[$path] = $item->withOperations(\array_filter(
                $item->operations,
                static function (Operation $operation) use ($enabled): bool {
                    $feature = $operation->extension('x-feature');

                    return !\is_string($feature) || \in_array($feature, $enabled, true);
                },
            ));
        }

        return $document->withPaths(new Paths($items));
    }

    public function cacheKey(Request $request, string $server): array
    {
        // Bump 'v' whenever decorate() changes behaviour.
        return ['v' => 1, 'features' => $this->features->forHost($request->getHost())];
    }
}

Both are autoconfigured and ordered by priority like the build-time decorators: lower runs first, the highest gets the last word. $server is the server name, or [combined] in combined mode.

You only remove operations. After the last decorator the bundle cleans up what they orphaned:

  • a path item left with no operation is dropped;
  • a component (schema, response, parameter, request body, header and so on) that the served document referenced before decoration and nothing references now is dropped, following $refs through other components;
  • a tag definition whose operations all went is dropped.

Components the document never referenced (or that a decorator added) stay, and so does everything they reference. Security schemes are never touched. Relationship members aren't removed either: if articles keeps its author relationship while the users endpoints are hidden, the article responses still carry that linkage on the wire, so the users schemas it points at stay in the document. Hide a relationship in the document only when you also hide it at runtime.

The same goes for describedby: links point into the document as the warmer built it, so a response can carry a describedby naming an operation a serve decorator hid from that client.

schemas.json has its own interface, JsonSchemaServeDecoratorInterface, which gets the schemas keyed by type (unset a key to hide a type). The schemas are standalone, so nothing is pruned. The CLI export commands and the static public_path files never run serve decorators: the exported spec stays the full contract for diffing and codegen.

Objects in example and default values

So decorators don't rebuild the document on every request, the warmer stores it beside the JSON as serialized PHP value objects, and a decorated request unserializes that copy. The copy records the installed versions of core and the bundle, so one left behind by a deploy reads as missing and the document is built again. Unserializing only allows core's OpenAPI value objects, stdClass and enums. If an example(), a default or an extension value holds another object (a DateTimeImmutable example, say), the warmer can't store the copy: it logs a warning naming the class, and each decorated request builds the document instead. List the class to cache it again:

json_api:
  openapi:
    serve_allowed_classes:
      - DateTimeImmutable

Only list classes you trust to be unserialized from your own cache directory. Without serve decorators the copy is never read, so the warning costs nothing.

Caching at the edge

docs.json, docs.yaml and schemas.json always send an ETag and answer a matching If-None-Match with a 304. The comparison is weak, so the W/ prefix a CDN adds when it compresses the body still matches. How the ETag is computed depends on the decorators, and docs.yaml hashes the format into the result, so it never shares a tag with docs.json:

Serve decorators ETag Cost of a 304
none the warmed artifact's hash, stored beside it one 32-byte file read
all implement OpenApiServeCacheKeyInterface a hash of the artifact hash and every key, in decorator order that read plus your cacheKey() calls
any without a key a hash of the final body a full decorate and serialise

A partial key would let two different documents share an ETag, so one keyless decorator switches that document kind to the body hash. The key mixes in each decorator's class name, so adding or removing a decorator changes it; a change inside decorate() doesn't, which is why the example carries a version.

Set Cache-Control for the document, schemas and viewer routes with cache_control:

json_api:
  openapi:
    cache_control: 'public, max-age=60, s-maxage=300, stale-while-revalidate=86400'

It is off by default, which leaves Symfony's private default in place. The docs routes are often behind a firewall in production, and a public default would let a shared cache hand an authenticated document to anyone. The responses carry no Vary header: the routes don't negotiate (the format is in the path), and a CDN already keys on the host. If a decorator reads anything other than the host, such as a cookie or an Authorization header, keep cache_control private.

Nothing on the docs, schemas or viewer routes starts a session or sets a cookie, and a lazy firewall leaves an anonymous request alone too. Under a stateful firewall, anything that reads the security token (a locale listener, say) marks the session as used, and Symfony's session listener would normally rewrite the response to private. When cache_control is set, these responses opt out of that rewrite, so the configured value is what the edge sees. A private document behind a login should therefore keep a private cache_control.

The servers list

Unless json_api.openapi.servers overrides it, each document's servers comes from the JSON:API server it describes:

  • A configured base_uri is the one server URL, as it is (https://api.example.com/api/v5).
  • An empty base_uri advertises the prefix the server's routes are imported under, as a relative URL, so a client resolves /articles against the document's host as /api/v5/articles. Routes imported with no prefix advertise no servers, as before. A server imported under two prefixes gets one entry per prefix, sorted, and an unprefixed import beside them adds /.
  • A prefix with placeholders (/{tenant}/api) becomes a templated URL with a server variable whose default is the route default for that placeholder:
# config/routes/json_api.yaml
json_api_tenant:
  resource: default
  type: jsonapi
  prefix: /{tenant}/api
  defaults: { tenant: acme }
"servers": [{ "url": "/{tenant}/api", "variables": { "tenant": { "default": "acme" } } }]

OpenAPI requires a variable default, so a placeholder prefix without a route default is left out of servers rather than given an invented value.

The document is built from the compiled routes, not from a request, so it is the same whether the cache warmer or the first request builds it. That also means it can't see a front controller served below the web root (/app/index.php) or a proxy prefix: those are only known per request. Set json_api.openapi.servers (or a configured base_uri) when the API is reached through one.

Multi-server

With several JSON:API servers, the default is one document per server (/docs.json for the default server, /{server}/docs.json for each named server, and the same paths ending .yaml). Switch to a single union document with multi_server: combined:

json_api:
  openapi:
    multi_server: combined   # one document spanning every server, at the json and yaml paths only

Conformance: proving the document describes reality

The headline guarantee is round-trip: the generated document is proven to describe the responses you actually serve. The bundle ships a test trait, SchemaConformanceTrait, that validates a real response body against the generated component schema for its type — so a drift between your document and your API fails a test.

use haddowg\JsonApiBundle\Testing\SchemaConformanceTrait;
use haddowg\JsonApiBundle\Testing\SchemaDocumentKind;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

final class OpenApiConformanceTest extends KernelTestCase
{
    use SchemaConformanceTrait;

    public function test_single_resource_matches_its_schema(): void
    {
        $response = /* your functional GET of /articles/1 → an HttpFoundation Response */;

        $this->assertResponseMatchesGeneratedSchema($response, 'articles', SchemaDocumentKind::Single);
    }

    public function test_collection_and_relationship_match(): void
    {
        $this->assertResponseMatchesGeneratedSchema($collection, 'articles', SchemaDocumentKind::Collection);
        $this->assertResponseMatchesGeneratedSchema($related, 'articles', SchemaDocumentKind::Related, 'author');
        $this->assertResponseMatchesGeneratedSchema($linkage, 'articles', SchemaDocumentKind::Relationship, 'author');
    }
}

SchemaDocumentKind selects which envelope component validates the response — Single (GET /{type}/{id}), Collection (GET /{type}, incl. a compound ?include document), Relationship (…/relationships/{rel} linkage) or Related (…/{rel} for a to-one). The trait builds the document once per server through the same DocumentFactory the warmer / controller / CLI use, and validates over opis/json-schema — which implements the 2020-12 dialect natively, so the helper is fully offline (no meta-schema to vendor). assertBodyMatchesGeneratedComponent($body, '<Component>') is the escape hatch for a component the four kinds don't name (a custom enum component, a per-relation collection). The envelope's included is a oneOf over the <Type>Resource of every type the document's ?include paths reach, discriminated by type, so an included resource of any other type fails. The trait also checks each included resource against its own type's <Type>Resource component, and reports that failure, which names the resource's index, rather than the envelope's oneOf.

Sparse fieldsets

The read schemas mark every attribute required, because a full response always carries them. A response to ?fields[articles]=title leaves the other members out, so tell the trait which fieldsets the request asked for. Pass the browser that made the request in place of the response, and the trait reads fields from its last request:

$client->get('/articles?include=author&fields[articles]=title,author&fields[authors]=name');

$this->assertResponseMatchesGeneratedSchema($client, 'articles', SchemaDocumentKind::Collection);

With a bare Response or body string, pass the map yourself as fieldsets:, in the same shape as the query (['articles' => 'title,author'], or a list of names per type). assertBodyMatchesGeneratedComponent() and bodyMatchesGeneratedComponent() take the same argument.

For each type with a fieldset, attributes and relationships outside it stop counting as required, in primary data and in included. Members inside the fieldset are still required, and a type without a fieldset still requires everything, so a member that goes missing from a full response still fails.

A selected key, such as fields[albums]=credits.producer, keeps credits required and narrows its schema the way the response is narrowed: the object requires none of its keys and admits only the selected ones. Listing credits whole as well keeps its schema whole. A nested path such as address.geo.lat narrows address to geo and geo to lat the same way, and a path through a list, editions.*.format, narrows the list's items to format.

This is a real, biting guarantee: when a response diverges from its schema, the assertion names exactly which member diverged. The bundle's own suite runs these assertions across both the in-memory and Doctrine providers, so the generated document is proven against the wire output of both data layers.

Beyond the response round-trip, every emitted document is also validated against the official OAS 3.1 meta-schema in the bundle's tests, so the document is well-formed OpenAPI as well as accurate.