You are here

public function JsonApiSchemaController::getResourceObjectSchema in JSON:API Schema 8

File

src/Controller/JsonApiSchemaController.php, line 184

Class

JsonApiSchemaController

Namespace

Drupal\jsonapi_schema\Controller

Code

public function getResourceObjectSchema(Request $request, $resource_type) {
  $resource_type = $this->resourceTypeRepository
    ->getByTypeName($resource_type);
  $schema = [
    '$schema' => static::JSON_SCHEMA_DRAFT,
    '$id' => $request
      ->getUri(),
    'title' => $this
      ->getSchemaTitle($resource_type, 'item'),
    'allOf' => [
      [
        'type' => 'object',
        'properties' => [
          'type' => [
            '$ref' => '#definitions/type',
          ],
        ],
      ],
      [
        '$ref' => static::JSONAPI_BASE_SCHEMA_URI . '#/definitions/resource',
      ],
    ],
    'definitions' => [
      'type' => [
        'const' => $resource_type
          ->getTypeName(),
      ],
    ],
  ];
  $cacheability = new CacheableMetadata();
  $schema = $this
    ->addFieldsSchema($schema, $resource_type);
  $schema = $this
    ->addRelationshipsSchemaLinks($schema, $resource_type, $cacheability);
  return CacheableJsonResponse::create($schema)
    ->addCacheableDependency($cacheability);
}