You are here

public function JsonApiGenerator::getPaths 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::getPaths()

File

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

Class

JsonApiGenerator
Defines an OpenApi Schema Generator for the JsonApi module.

Namespace

Drupal\openapi_jsonapi\Plugin\openapi\OpenApiGenerator

Code

public function getPaths() {
  $routes = $this
    ->getJsonApiRoutes();
  $api_paths = [];
  foreach ($routes as $route_name => $route) {

    /** @var \Drupal\jsonapi\ResourceType\ResourceType $resource_type */
    $resource_type = $this
      ->getResourceType($route_name, $route);
    $entity_type_id = $resource_type
      ->getEntityTypeId();
    $bundle_name = $resource_type
      ->getBundle();
    if (!$this
      ->includeEntityTypeBundle($entity_type_id, $bundle_name)) {
      continue;
    }
    $api_path = [];
    $methods = $route
      ->getMethods();
    foreach ($methods as $method) {
      $method = strtolower($method);
      $path_method = [
        'summary' => $this
          ->getRouteMethodSummary($route, $route_name, $method),
        'description' => $this
          ->getRouteMethodDescription($route, $route_name, $method, $resource_type
          ->getTypeName()),
        'parameters' => $this
          ->getMethodParameters($route, $route_name, $resource_type, $method),
        'tags' => [
          $this
            ->getBundleTag($entity_type_id, $bundle_name),
        ],
        'responses' => $this
          ->getEntityResponsesJsonApi($entity_type_id, $method, $bundle_name, $route_name, $route),
      ];

      /*
       * @TODO: #2977109 - Calculate oauth scopes required.
       *
       * if (array_key_exists('oauth2', $path_method['security'])) {
       *   ...
       * }
       */
      $api_path[$method] = $path_method;
    }

    // Each path contains the "base path" from a OpenAPI perspective.
    $path = str_replace($this
      ->getJsonApiBase(), '', $route
      ->getPath());
    $api_paths[$path] = NestedArray::mergeDeep(empty($api_paths[$path]) ? [] : $api_paths[$path], $api_path);
  }
  return $api_paths;
}