You are here

public function OpenApiGeneratorBase::getSpecification in OpenAPI 8.2

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

Generates OpenAPI specification.

Return value

array The specification output.

Overrides OpenApiGeneratorInterface::getSpecification

File

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

Class

OpenApiGeneratorBase
Defines base class for OpenApi Generator plugins.

Namespace

Drupal\openapi\Plugin\openapi

Code

public function getSpecification() {
  $basePath = $this
    ->getBasePath();
  $spec = [
    'swagger' => "2.0",
    'schemes' => [
      $this->request
        ->getScheme(),
    ],
    'info' => $this
      ->getInfo(),
    'host' => $this->request
      ->getHttpHost(),
    'basePath' => empty($basePath) ? '/' : $basePath,
    'securityDefinitions' => $this
      ->getSecurityDefinitions(),
    'security' => $this
      ->getSecurity(),
    'tags' => $this
      ->getTags(),
    'definitions' => $this
      ->getDefinitions(),
    'consumes' => $this
      ->getConsumes(),
    'produces' => $this
      ->getProduces(),
    'paths' => $this
      ->getPaths(),
  ];

  // Strip any empty arrays which aren't required.
  $required = [
    'swagger',
    'info',
    'paths',
  ];
  foreach ($spec as $key => $item) {
    if (!in_array($key, $required) && is_array($item) && !count($item)) {
      unset($spec[$key]);
    }
  }
  return $spec;
}