You are here

protected function JsonApiGenerator::getMethodParameters in OpenAPI 8

Get the parameters array for a method on a route.

Parameters

\Symfony\Component\Routing\Route $route: The route.

string $route_name: The route name.

\Drupal\jsonapi\ResourceType\ResourceType $resource_type: The JSON API resource type.

string $method: The HTTP method.

Return value

array The parameters.

Throws

\Drupal\Component\Plugin\Exception\PluginNotFoundException

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

File

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

Class

JsonApiGenerator
Defines an OpenApi Schema Generator for the JsonApi module.

Namespace

Drupal\openapi\Plugin\openapi\OpenApiGenerator

Code

protected function getMethodParameters(Route $route, $route_name, ResourceType $resource_type, $method) {
  $parameters = [];
  if ($method === 'get' && $resource_type
    ->isVersionable()) {
    $parameters[] = [
      'name' => 'resourceVersion',
      'in' => 'query',
      'type' => 'string',
      'required' => FALSE,
      'description' => $this
        ->t('The JSON:API module exposes entity revisions as resource versions. @link.', [
        '@link' => Link::fromTextAndUrl('Learn more in the documentation', Url::fromUri('https://www.drupal.org/docs/8/modules/jsonapi/revisions'))
          ->toString(),
      ]),
    ];
  }
  $entity_type_id = $resource_type
    ->getEntityTypeId();
  $bundle_name = $resource_type
    ->getBundle();
  $option_parameters = $route
    ->getOption('parameters');
  if (!empty($option_parameters) && ($filtered_parameters = $this
    ->filterParameters($option_parameters))) {
    foreach ($filtered_parameters as $parameter_name => $parameter_info) {
      $parameter = [
        'name' => $parameter_name,
        'required' => TRUE,
        'in' => 'path',
      ];
      if ($parameter_info['converter'] === static::JSON_API_UUID_CONVERTER) {
        $parameter['type'] = 'uuid';
        $parameter['description'] = $this
          ->t('The uuid of the @entity @bundle', [
          '@entity' => $entity_type_id,
          '@bundle' => $bundle_name,
        ]);
      }
      $parameters[] = $parameter;
    }
    if ($this
      ->jsonApiPathHasRelated($route
      ->getPath())) {
      $parameters[] = [
        'name' => 'related',
        'required' => TRUE,
        'in' => 'path',
        'type' => 'string',
        'description' => $this
          ->t('The relationship field name'),
      ];
    }
  }
  $route_type = $this
    ->getRoutTypeFromName($route_name);
  if ($method == 'get' && $route_type === 'collection' && $resource_type
    ->isLocatable()) {

    // If no route parameters and GET then this is collection route.
    // @todo Add descriptions or link to documentation.
    $parameters[] = [
      'name' => 'filter',
      'in' => 'query',
      'type' => 'array',
      'required' => FALSE,
      'description' => $this
        ->t('The JSON:API module has some of the most robust and feature-rich filtering features around. All of that power comes with a bit of a learning curve though. @link.', [
        '@link' => Link::fromTextAndUrl('Learn more in the documentation', Url::fromUri('https://www.drupal.org/docs/8/modules/jsonapi/filtering'))
          ->toString(),
      ]),
    ];
    $parameters[] = [
      'name' => 'sort',
      'in' => 'query',
      'type' => 'array',
      'required' => FALSE,
      'description' => $this
        ->t('The JSON:API module allows you to sort collections based on properties in the resource or in nested resources. @link.', [
        '@link' => Link::fromTextAndUrl('Learn more in the documentation', Url::fromUri('https://www.drupal.org/docs/8/modules/jsonapi/sorting'))
          ->toString(),
      ]),
    ];
    $parameters[] = [
      'name' => 'page',
      'in' => 'query',
      'type' => 'array',
      'required' => FALSE,
      'description' => $this
        ->t('Pagination can be a deceptively complex topic. It\'s easy to fall into traps and not follow best-practices. @link.', [
        '@link' => Link::fromTextAndUrl('Learn more in the documentation', Url::fromUri('https://www.drupal.org/docs/8/modules/jsonapi/pagination'))
          ->toString(),
      ]),
    ];
    $parameters[] = [
      'name' => 'include',
      'in' => 'query',
      'type' => 'string',
      'required' => FALSE,
      'description' => $this
        ->t('Embed related entities in the response. For example: use a query string like <code>?include=comments.author</code> to include all the entities referenced by <code>comments</code> and all the entities referenced by <code>author</code> on those entities!. @link.', [
        '@link' => Link::fromTextAndUrl('Learn more in the documentation', Url::fromUri('https://www.drupal.org/docs/8/modules/jsonapi/includes'))
          ->toString(),
      ]),
    ];
  }
  elseif ($method == 'post' || $method == 'patch') {

    // We need a parameter for the body.
    $body_entity_type_id = $entity_type_id;
    $body_bundle_name = $bundle_name;
    if (in_array($route_type, [
      'related',
      'relationship',
    ])) {
      $target_resource_type = $this
        ->relatedResourceType($route_name, $route);
      $body_entity_type_id = $target_resource_type
        ->getEntityTypeId();
      $body_bundle_name = $target_resource_type
        ->getBundle();
    }

    // Determine if it is mutable.
    if ($resource_type
      ->isMutable()) {
      if ($route_type === 'relationship') {
        $is_multiple = $this
          ->isToManyRelationship($route_name, $resource_type);

        // Relationships are completely different.
        $parameters[] = [
          'name' => 'body',
          'in' => 'body',
          'description' => $this
            ->t('The resource identifier object'),
          'required' => TRUE,
          'schema' => static::buildRelationshipSchema($is_multiple, $target_resource_type
            ->getTypeName()),
        ];
      }
      else {
        $parameters[] = [
          'name' => 'body',
          'in' => 'body',
          'description' => $this
            ->t('The %label object', [
            '%label' => $body_entity_type_id,
          ]),
          'required' => TRUE,
          'schema' => [
            '$ref' => $this
              ->getDefinitionReference($body_entity_type_id, $body_bundle_name),
          ],
        ];
      }
    }
  }
  return $parameters;
}