You are here

public function RestGenerator::getPaths in OpenAPI 8

Returns the paths information.

Return value

array The info elements.

Overrides OpenApiGeneratorBase::getPaths

File

src/Plugin/openapi/OpenApiGenerator/RestGenerator.php, line 152

Class

RestGenerator
Defines an OpenApi Schema Generator for the Rest module.

Namespace

Drupal\openapi\Plugin\openapi\OpenApiGenerator

Code

public function getPaths() {
  $bundle_name = isset($this
    ->getOptions()['bundle_name']) ? $this
    ->getOptions()['bundle_name'] : NULL;
  $resource_configs = $this
    ->getResourceConfigs($this
    ->getOptions());
  if (!$resource_configs) {
    return [];
  }
  $api_paths = [];
  foreach ($resource_configs as $resource_config) {

    /** @var \Drupal\rest\Plugin\ResourceBase $plugin */
    $resource_plugin = $resource_config
      ->getResourcePlugin();
    foreach ($resource_config
      ->getMethods() as $method) {
      if ($route = $this
        ->getRouteForResourceMethod($resource_config, $method)) {
        $open_api_method = strtolower($method);
        $path = $route
          ->getPath();
        $path_method_spec = [];
        $formats = $this
          ->getMethodSupportedFormats($method, $resource_config);
        $format_parameter = [
          'name' => '_format',
          'in' => 'query',
          'type' => 'string',
          'enum' => $formats,
          'required' => TRUE,
          'description' => 'Request format',
        ];
        if (count($formats) == 1) {
          $format_parameter['default'] = $formats[0];
        }
        $path_method_spec['parameters'][] = $format_parameter;
        $path_method_spec['responses'] = $this
          ->getErrorResponses();
        if ($this
          ->isEntityResource($resource_config)) {
          $entity_type = $this
            ->getEntityType($resource_config);
          $path_method_spec['tags'] = [
            $entity_type
              ->id(),
          ];
          $path_method_spec['summary'] = $this
            ->t('@method a @entity_type', [
            '@method' => ucfirst($open_api_method),
            '@entity_type' => $entity_type
              ->getLabel(),
          ]);
          $path_method_spec['parameters'] = array_merge($path_method_spec['parameters'], $this
            ->getEntityParameters($entity_type, $method, $bundle_name));
          $path_method_spec['responses'] = $this
            ->getEntityResponses($entity_type
            ->id(), $method, $bundle_name) + $path_method_spec['responses'];
        }
        else {
          $path_method_spec['responses']['200'] = [
            'description' => 'successful operation',
          ];
          $path_method_spec['summary'] = $resource_plugin
            ->getPluginDefinition()['label'];
          $path_method_spec['parameters'] = array_merge($path_method_spec['parameters'], $this
            ->getRouteParameters($route));
        }
        $path_method_spec['operationId'] = $resource_plugin
          ->getPluginId() . ":" . $method;
        $path_method_spec['schemes'] = [
          $this->request
            ->getScheme(),
        ];
        $path_method_spec['security'] = $this
          ->getResourceSecurity($resource_config, $method, $formats);
        $api_paths[$path][$open_api_method] = $path_method_spec;
      }
    }
  }
  return $api_paths;
}