2016-06-30 23:30:37 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2016-12-29 12:09:26 +01:00
|
|
|
* This file is part of the NelmioApiDocBundle package.
|
2016-06-30 23:30:37 +02:00
|
|
|
*
|
2016-12-29 12:09:26 +01:00
|
|
|
* (c) Nelmio
|
2016-06-30 23:30:37 +02:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2016-12-29 12:09:26 +01:00
|
|
|
namespace Nelmio\ApiDocBundle\RouteDescriber;
|
2016-06-30 23:30:37 +02:00
|
|
|
|
2020-01-10 11:53:34 -05:00
|
|
|
use LogicException;
|
2020-05-28 13:19:11 +02:00
|
|
|
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
|
|
|
|
use OpenApi\Annotations as OA;
|
2021-12-11 16:39:04 +03:00
|
|
|
use OpenApi\Generator;
|
2016-06-30 23:30:37 +02:00
|
|
|
use Symfony\Component\Routing\Route;
|
|
|
|
|
2019-04-10 20:52:45 +02:00
|
|
|
/**
|
|
|
|
* Should be last route describer executed to make sure all params are set.
|
|
|
|
*/
|
2016-11-30 15:04:53 +01:00
|
|
|
final class RouteMetadataDescriber implements RouteDescriberInterface
|
2016-06-30 23:30:37 +02:00
|
|
|
{
|
2016-07-15 00:40:30 +02:00
|
|
|
use RouteDescriberTrait;
|
2016-06-30 23:30:37 +02:00
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
public function describe(OA\OpenApi $api, Route $route, \ReflectionMethod $reflectionMethod)
|
2016-06-30 23:30:37 +02:00
|
|
|
{
|
|
|
|
foreach ($this->getOperations($api, $route) as $operation) {
|
2016-08-04 22:27:10 +02:00
|
|
|
$requirements = $route->getRequirements();
|
|
|
|
$compiledRoute = $route->compile();
|
2020-01-10 11:53:34 -05:00
|
|
|
$existingParams = $this->getRefParams($api, $operation);
|
2019-04-10 20:52:45 +02:00
|
|
|
|
2016-12-01 17:19:33 +01:00
|
|
|
// Don't include host requirements
|
2016-08-04 22:27:10 +02:00
|
|
|
foreach ($compiledRoute->getPathVariables() as $pathVariable) {
|
2016-12-01 17:19:33 +01:00
|
|
|
if ('_format' === $pathVariable) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-10 11:53:34 -05:00
|
|
|
$paramId = $pathVariable.'/path';
|
2020-05-28 13:19:11 +02:00
|
|
|
/** @var OA\Parameter $parameter */
|
2020-01-10 11:53:34 -05:00
|
|
|
$parameter = $existingParams[$paramId] ?? null;
|
|
|
|
if (null !== $parameter) {
|
2021-12-11 16:39:04 +03:00
|
|
|
if (!$parameter->required || Generator::UNDEFINED === $parameter->required) {
|
2020-01-10 11:53:34 -05:00
|
|
|
throw new LogicException(\sprintf('Global parameter "%s" is used as part of route "%s" and must be set as "required"', $pathVariable, $route->getPath()));
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
2019-04-10 20:52:45 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
$parameter = Util::getOperationParameter($operation, $pathVariable, 'path');
|
|
|
|
$parameter->required = true;
|
2017-07-05 23:41:25 +02:00
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
$parameter->schema = Util::getChild($parameter, OA\Schema::class);
|
|
|
|
|
2021-12-11 16:39:04 +03:00
|
|
|
if (Generator::UNDEFINED === $parameter->schema->type) {
|
2020-05-28 13:19:11 +02:00
|
|
|
$parameter->schema->type = 'string';
|
2017-07-03 23:39:42 +02:00
|
|
|
}
|
2016-08-04 22:27:10 +02:00
|
|
|
|
2021-12-11 16:39:04 +03:00
|
|
|
if (isset($requirements[$pathVariable]) && Generator::UNDEFINED === $parameter->schema->pattern) {
|
2020-05-28 13:19:11 +02:00
|
|
|
$parameter->schema->pattern = $requirements[$pathVariable];
|
2016-08-04 22:27:10 +02:00
|
|
|
}
|
2016-06-30 23:30:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-10 11:53:34 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The '$ref' parameters need special handling, since their objects are missing 'name' and 'in'.
|
|
|
|
*
|
2020-05-28 13:19:11 +02:00
|
|
|
* @return OA\Parameter[] existing $ref parameters
|
2020-01-10 11:53:34 -05:00
|
|
|
*/
|
2020-05-28 13:19:11 +02:00
|
|
|
private function getRefParams(OA\OpenApi $api, OA\Operation $operation): array
|
2020-01-10 11:53:34 -05:00
|
|
|
{
|
2020-05-28 13:19:11 +02:00
|
|
|
/** @var OA\Parameter[] $globalParams */
|
2021-12-11 16:39:04 +03:00
|
|
|
$globalParams = Generator::UNDEFINED !== $api->components && Generator::UNDEFINED !== $api->components->parameters ? $api->components->parameters : [];
|
2020-11-01 11:41:49 +01:00
|
|
|
$globalParams = array_column($globalParams, null, 'parameter'); // update the indexes of the array with the reference names actually used
|
2020-01-10 11:53:34 -05:00
|
|
|
$existingParams = [];
|
|
|
|
|
2021-12-11 16:39:04 +03:00
|
|
|
$operationParameters = Generator::UNDEFINED !== $operation->parameters ? $operation->parameters : [];
|
2020-05-28 13:19:11 +02:00
|
|
|
/** @var OA\Parameter $parameter */
|
|
|
|
foreach ($operationParameters as $id => $parameter) {
|
|
|
|
$ref = $parameter->ref;
|
2021-12-11 16:39:04 +03:00
|
|
|
if (Generator::UNDEFINED === $ref) {
|
2020-01-10 11:53:34 -05:00
|
|
|
// we only concern ourselves with '$ref' parameters, so continue the loop
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
$ref = \mb_substr($ref, 24); // trim the '#/components/parameters/' part of ref
|
2020-01-10 11:53:34 -05:00
|
|
|
if (!isset($globalParams[$ref])) {
|
|
|
|
// this shouldn't happen during proper configs, but in case of bad config, just ignore it here
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$refParameter = $globalParams[$ref];
|
|
|
|
|
|
|
|
// param ids are in form {name}/{in}
|
2020-05-28 13:19:11 +02:00
|
|
|
$existingParams[\sprintf('%s/%s', $refParameter->name, $refParameter->in)] = $refParameter;
|
2020-01-10 11:53:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $existingParams;
|
|
|
|
}
|
2016-06-30 23:30:37 +02:00
|
|
|
}
|