You are here

protected function JsonApiGenerator::getJsonSchema in OpenAPI for JSON:API 8.2

Same name and namespace in other branches
  1. 3.x src/Plugin/openapi/OpenApiGenerator/JsonApiGenerator.php \Drupal\openapi_jsonapi\Plugin\openapi\OpenApiGenerator\JsonApiGenerator::getJsonSchema()
1 call to JsonApiGenerator::getJsonSchema()
JsonApiGenerator::getDefinitions in src/Plugin/openapi/OpenApiGenerator/JsonApiGenerator.php

File

src/Plugin/openapi/OpenApiGenerator/JsonApiGenerator.php, line 760

Class

JsonApiGenerator
Defines an OpenApi Schema Generator for the JsonApi module.

Namespace

Drupal\openapi_jsonapi\Plugin\openapi\OpenApiGenerator

Code

protected function getJsonSchema($described_format, $entity_type_id, $bundle_name = NULL) {
  if ($entity_type_id !== $bundle_name) {
    $schema = $this->schemaFactory
      ->create($entity_type_id, $bundle_name);
  }
  else {
    $schema = $this->schemaFactory
      ->create($entity_type_id);
  }
  if ($schema) {
    $json_schema = $this->serializer
      ->normalize($schema, "schema_json:{$described_format}");
    unset($json_schema['$schema'], $json_schema['id']);
    $json_schema = $this
      ->cleanSchema($json_schema);
    if (!$bundle_name) {

      // Add discriminator field.
      $entity_type = $this->entityTypeManager
        ->getDefinition($entity_type_id);
      if ($bundle_field = $entity_type
        ->getKey('bundle')) {
        $json_schema['discriminator'] = $bundle_field;
      }
    }
  }
  else {
    $json_schema = [
      'type' => 'object',
      'title' => $this
        ->t('@entity_type Schema', [
        '@entity_type' => $entity_type_id,
      ]),
      'description' => $this
        ->t('Describes the payload for @entity_type entities.', [
        '@entity_type' => $entity_type_id,
      ]),
    ];
  }
  return $json_schema;
}