You are here

protected function JsonApiGenerator::getEntityResponsesJsonApi in OpenAPI 8

1 call to JsonApiGenerator::getEntityResponsesJsonApi()
JsonApiGenerator::getPaths in src/Plugin/openapi/OpenApiGenerator/JsonApiGenerator.php
Returns the paths information.

File

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

Class

JsonApiGenerator
Defines an OpenApi Schema Generator for the JsonApi module.

Namespace

Drupal\openapi\Plugin\openapi\OpenApiGenerator

Code

protected function getEntityResponsesJsonApi($entity_type_id, $method, $bundle_name, $route_name, Route $route = NULL) {
  $route_type = $this
    ->getRoutTypeFromName($route_name);
  if ($route_type === 'collection') {
    if ($method === 'get') {
      $schema_response = [];
      if ($definition_ref = $this
        ->getDefinitionReference($entity_type_id, $bundle_name)) {
        $definition_key = $this
          ->getEntityDefinitionKey($entity_type_id, $bundle_name);
        $definition = $this
          ->getDefinitions()[$definition_key];
        $ref = NestedArray::getValue($definition, [
          'definitions',
          'data',
        ]) ? "{$definition_ref}/definitions/data" : "{$definition_ref}/properties/data";
        $schema = $definition;
        $schema['properties']['data'] = [
          'type' => 'array',
          'items' => [
            '$ref' => $ref,
          ],
        ];
        $schema_response = [
          'schema' => $schema,
        ];
      }
      $responses['200'] = [
        'description' => 'successful operation',
      ] + $schema_response;
      return $responses;
    }
  }
  elseif (in_array($route_type, [
    'relationship',
    'related',
  ])) {
    $resource_type = $this
      ->getResourceType($route_name, $route);
    $target_resource_type = $this
      ->relatedResourceType($route_name, $route);
    $is_multiple = $this
      ->isToManyRelationship($route_name, $resource_type);
    if ($route_type === 'relationship') {
      $schema = static::buildRelationshipSchema($is_multiple, $target_resource_type
        ->getTypeName());
      if ($method === 'get') {
        return [
          200 => [
            'description' => 'successful operation',
            'schema' => $schema,
          ],
        ];
      }
      elseif ($method === 'post') {
        return [
          201 => [
            'description' => 'created',
            'schema' => $schema,
          ],
        ];
      }
      elseif ($method === 'patch') {
        return [
          200 => [
            'description' => 'successful operation',
            'schema' => $schema,
          ],
        ];
      }
      elseif ($method === 'delete') {
        return [
          204 => [
            'description' => 'no content',
          ],
        ];
      }
    }
    else {

      // Fake a route name that will yield the expected results for the related
      // responses.
      $target_route_name = Routes::getRouteName($target_resource_type, $is_multiple ? 'collection' : 'individual');
      return $this
        ->getEntityResponsesJsonApi($target_resource_type
        ->getEntityTypeId(), $method, $target_resource_type
        ->getBundle(), $target_route_name);
    }
  }
  else {
    return parent::getEntityResponses($entity_type_id, $method, $bundle_name);
  }
  return [];
}