You are here

protected function OpenApiGeneratorBase::getJsonSchema in OpenAPI 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/openapi/OpenApiGeneratorBase.php \Drupal\openapi\Plugin\openapi\OpenApiGeneratorBase::getJsonSchema()

Gets the JSON Schema for an entity type or entity type and bundle.

Parameters

string $described_format: The format that will be described, json, json_api, etc.

string $entity_type_id: The entity type id.

string $bundle_name: The bundle name.

Return value

array The JSON schema.

2 calls to OpenApiGeneratorBase::getJsonSchema()
JsonApiGenerator::getDefinitions in src/Plugin/openapi/OpenApiGenerator/JsonApiGenerator.php
Get model definitions for Drupal entities and bundles.
RestGenerator::getDefinitions in src/Plugin/openapi/OpenApiGenerator/RestGenerator.php
Get model definitions for Drupal entities and bundles.

File

src/Plugin/openapi/OpenApiGeneratorBase.php, line 374

Class

OpenApiGeneratorBase
Defines base class for OpenApi Generator plugins.

Namespace

Drupal\openapi\Plugin\openapi

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;
}