Integrations

JSON:API

Document the resources, the include and fields parameters and the errors of ark4ne/laravel-json-api.

With Laravel JSON:API installed, its resources are documented as JSON:API resource objects in application/vnd.api+json, with their attributes, relationships, links, meta and included resources. Nothing to configure: the installed version is detected, and php artisan about shows it.

Laravel JSON:API
JSON:API 1.1 resources for Laravel, with typed values, validated includes and fieldsets, and API revisions.
v3.0.0ark4ne/laravel-json-api

What is documented

app/Http/Resources/ArticleResource.php
/**
 * @extends JsonApiResource<Article>
 */
final class ArticleResource extends JsonApiResource
{
    public function toAttributes(Request $request): iterable
    {
        return [
            'title' => $this->string(),
            'published_at' => $this->date(),
        ];
    }

    protected function toRelationships(Request $request): iterable
    {
        return ['author' => $this->one(AuthorResource::class)];
    }
}
components.schemas.Article
{
  "type": "object",
  "required": ["id", "type"],
  "properties": {
    "id": { "type": "string" },
    "type": { "type": "string", "const": "article" },
    "attributes": {
      "type": "object",
      "properties": {
        "title": { "type": ["string", "null"] },
        "published_at": { "type": ["string", "null"], "format": "date-time" }
      }
    },
    "relationships": {
      "type": "object",
      "properties": {
        "author": {
          "type": "object",
          "properties": {
            "data": {
              "type": ["object", "null"],
              "required": ["type", "id"],
              "properties": {
                "type": { "type": "string", "const": "author" },
                "id": { "type": "string" }
              }
            }
          }
        }
      }
    }
  }
}
  • The descriptors give the type of each value: $this->string(), $this->date(), $this->struct()…
  • The relationships give the resource identifiers, and the resources a response may include (included).
  • A resource returned by an action is wrapped in a document: {"data": Article, "included": [Author]}; a collection gives {"data": [Article]}, with links and meta when paginated.

Include and sparse fieldsets

The Includes and Fields rules of a form request give the include and fields[type] query parameters, with the relationships and the fields they accept:

app/Http/Requests/ShowArticleRequest.php
public function rules(): array
{
    return [
        'include' => [new Includes(ArticleResource::class)],
        'fields' => [new Fields(ArticleResource::class)],
    ];
}
parameters
[
  {
    "name": "include",
    "in": "query",
    "schema": { "type": "string", "example": "author", "description": "Relationships to include, separated by commas: `author`." }
  },
  {
    "name": "fields[article]",
    "in": "query",
    "schema": { "type": "string", "description": "Fields of `article`, separated by commas: `title`, `published_at`." }
  },
  {
    "name": "fields[author]",
    "in": "query",
    "schema": { "type": "string", "description": "Fields of `author`, separated by commas: `name`." }
  }
]

The rules are Ark4ne\JsonApi\Requests\Rules\… in 1.x, Ark4ne\JsonApi\Http\Rules\… from 2.x.

Errors

If the application renders its errors as JSON:API error documents (JsonApi::renderErrors() from 2.x), say so:

config/openapi.php
'integrations' => [
    'json_api' => ['errors' => true],
],

The 401, 403, 404, 422 and other error responses are then JSON:API error documents, in application/vnd.api+json.

Versions

ark4ne/laravel-json-api ^1.9. The generator runs the hooks of the resources (toAttributes(), toRelationships(), toLinks(), toResourceMeta()) on the package's fake model and reads the descriptors they return.

The findings of the compilation of a definition (2.x and 3.x) are relayed as diagnostics of the document, once each.