Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file.
Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## [Unreleased][unreleased]
### Added
* Allowed endpoints filter for OpenApiHandler

## 3.0.0

Expand Down
61 changes: 52 additions & 9 deletions src/Handlers/OpenApiHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,30 @@ class OpenApiHandler extends BaseHandler

private $definitions = [];

private $allowedEndpoints = [];

/**
* OpenApiHandler constructor.
* @param ApiDecider $apiDecider
* @param ApiLink $apiLink
* @param Request $request
* @param array $initData - structured data for initialization response
* @param array $allowedEndpoints - list of endpoint (path and methods) which should be included in output, empty means all, wildcard * is supported for path
* Example: ['user/*' => ['get', 'post', 'delete'], 'user/list' => 'post', 'user/list' => [], 'user/list']
*/
public function __construct(
ApiDecider $apiDecider,
ApiLink $apiLink,
Request $request,
array $initData = []
array $initData = [],
array $allowedEndpoints = []
) {
parent::__construct();
$this->apiDecider = $apiDecider;
$this->apiLink = $apiLink;
$this->request = $request;
$this->initData = $initData;
$this->allowedEndpoints = $allowedEndpoints;
}

public function params(): array
Expand Down Expand Up @@ -248,6 +254,10 @@ private function getPaths(array $versionApis, string $baseUrl, string $basePath)
$handler = $api->getHandler();
$path = str_replace([$baseUrl, $basePath], '', $this->apiLink->link($api->getEndpoint()));
$responses = [];
$endpointPath = str_replace('v' . $api->getEndpoint()->getVersion() . '/', '', $api->getEndpoint()->getUrl());
if (!$this->isPathAllowed($endpointPath, $api->getEndpoint()->getMethod())) {
continue;
}

$settings = [
'summary' => $handler->summary(),
Expand All @@ -270,7 +280,7 @@ private function getPaths(array $versionApis, string $baseUrl, string $basePath)
'schema' => [
'$ref' => '#/components/schemas/ErrorWrongInput',
],
]
],
],
];
}
Expand Down Expand Up @@ -311,15 +321,15 @@ private function getPaths(array $versionApis, string $baseUrl, string $basePath)
'application/json; charset=utf-8' => [
'schema' => $schema,
],
]
],
];
if (!empty($examples = $output->getExamples())) {
if (count($examples) === 1) {
$example = is_array($output->getExample())? $output->getExample() : json_decode($output->getExample(), true);
$example = is_array($output->getExample()) ? $output->getExample() : json_decode($output->getExample(), true);
$responses[$output->getCode()]['content']['application/json; charset=utf-8']['example'] = $example;
} else {
foreach ($examples as $exampleKey => $example) {
$example = is_array($example)? $example : json_decode($example, true);
$example = is_array($example) ? $example : json_decode($example, true);
$responses[$output->getCode()]['content']['application/json; charset=utf-8']['examples'][$exampleKey] = $example;
}
}
Expand All @@ -345,9 +355,9 @@ private function getPaths(array $versionApis, string $baseUrl, string $basePath)
'description' => $output->getDescription(),
'schema' => [
'type' => 'string',
]
],
],
]
],
];
}
}
Expand Down Expand Up @@ -397,6 +407,39 @@ private function getPaths(array $versionApis, string $baseUrl, string $basePath)
return $list;
}

private function isPathAllowed($path, $method): bool
{
if (empty($this->allowedEndpoints)) {
return true;
}

foreach ($this->allowedEndpoints as $allowedPath => $allowedMethods) {
if (is_int($allowedPath)) { // ['user/list'] format
$pattern = str_replace('*', '.*?', preg_quote(strtolower($allowedMethods)));
if (preg_match("#$pattern#", strtolower($path))) {
return true;
}

continue;
}

// ['user/*' => ['get', 'post'], 'user/list' => 'post', 'user/list' => [], 'user/list' => '*'] format
$pattern = str_replace('*', '.*?', preg_quote(strtolower($allowedPath)));
if (preg_match("#$pattern#", strtolower($path))) {
if (empty($allowedMethods)) {
return true;
}
if (is_string($allowedMethods) && strtolower($allowedMethods) === strtolower($method)) {
return true;
}
if (is_array($allowedMethods) && in_array(strtolower($method), array_map('strtolower', $allowedMethods), true)) {
return true;
}
}
}
return false;
}

private function getBasePath(array $apis, string $baseUrl): string
{
$basePath = '';
Expand Down Expand Up @@ -489,10 +532,10 @@ private function createRequestBody(ApiHandlerInterface $handler)
$schema = json_decode($param->getSchema(), true);
if (!empty($examples = $param->getExamples())) {
if (count($examples) === 1) {
$schema['example'] = is_array($param->getExample())? $param->getExample() : json_decode($param->getExample(), true);
$schema['example'] = is_array($param->getExample()) ? $param->getExample() : json_decode($param->getExample(), true);
} else {
foreach ($examples as $exampleKey => $example) {
$schema['examples'][$exampleKey] = is_array($example)? $example : json_decode($example, true);
$schema['examples'][$exampleKey] = is_array($example) ? $example : json_decode($example, true);
}
}
}
Expand Down