NelmioApiDocBundle/RouteDescriber/FosRestDescriber.php

171 lines
5.8 KiB
PHP
Raw Normal View History

2016-08-04 22:27:10 +02:00
<?php
/*
2016-12-29 12:09:26 +01:00
* This file is part of the NelmioApiDocBundle package.
2016-08-04 22:27:10 +02:00
*
2016-12-29 12:09:26 +01:00
* (c) Nelmio
2016-08-04 22:27:10 +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-08-04 22:27:10 +02:00
use Doctrine\Common\Annotations\Reader;
use FOS\RestBundle\Controller\Annotations\QueryParam;
2016-08-04 22:34:34 +02:00
use FOS\RestBundle\Controller\Annotations\RequestParam;
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use OpenApi\Annotations as OA;
2016-08-04 22:27:10 +02:00
use Symfony\Component\Routing\Route;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Regex;
2016-11-30 15:04:53 +01:00
final class FosRestDescriber implements RouteDescriberInterface
2016-08-04 22:27:10 +02:00
{
use RouteDescriberTrait;
/** @var Reader */
2016-08-04 22:27:10 +02:00
private $annotationReader;
/** @var string[] */
private $mediaTypes;
public function __construct(Reader $annotationReader, array $mediaTypes)
2016-08-04 22:27:10 +02:00
{
$this->annotationReader = $annotationReader;
$this->mediaTypes = $mediaTypes;
2016-08-04 22:27:10 +02:00
}
public function describe(OA\OpenApi $api, Route $route, \ReflectionMethod $reflectionMethod)
2016-08-04 22:27:10 +02:00
{
2016-08-04 22:36:20 +02:00
$annotations = $this->annotationReader->getMethodAnnotations($reflectionMethod);
$annotations = array_filter($annotations, static function ($value) {
2016-08-04 22:27:10 +02:00
return $value instanceof RequestParam || $value instanceof QueryParam;
});
foreach ($this->getOperations($api, $route) as $operation) {
foreach ($annotations as $annotation) {
2020-02-21 10:42:13 +01:00
$parameterName = $annotation->key ?? $annotation->getName(); // the key used by fosrest
if ($annotation instanceof QueryParam) {
2020-02-21 10:42:13 +01:00
$name = $parameterName.($annotation->map ? '[]' : '');
$parameter = Util::getOperationParameter($operation, $name, 'query');
$parameter->allowEmptyValue = $annotation->nullable && $annotation->allowBlank;
$parameter->required = !$annotation->nullable && $annotation->strict;
if (OA\UNDEFINED === $parameter->description) {
$parameter->description = $annotation->description;
}
2019-04-20 13:21:54 +02:00
$schema = Util::getChild($parameter, OA\Schema::class);
$this->describeCommonSchemaFromAnnotation($schema, $annotation);
} else {
/** @var OA\RequestBody $requestBody */
$requestBody = Util::getChild($operation, OA\RequestBody::class);
foreach ($this->mediaTypes as $mediaType) {
$contentSchema = $this->getContentSchemaForType($requestBody, $mediaType);
$schema = Util::getProperty($contentSchema, $parameterName);
if (!$annotation->nullable && $annotation->strict) {
$requiredParameters = is_array($contentSchema->required) ? $contentSchema->required : [];
$requiredParameters[] = $parameterName;
$contentSchema->required = array_values(array_unique($requiredParameters));
}
$this->describeCommonSchemaFromAnnotation($schema, $annotation);
}
2016-08-04 22:27:10 +02:00
}
}
}
}
private function getPattern($requirements)
2016-08-04 22:27:10 +02:00
{
if (is_array($requirements) && isset($requirements['rule'])) {
2016-08-04 22:27:10 +02:00
return (string) $requirements['rule'];
}
2016-08-04 22:27:10 +02:00
if (is_string($requirements)) {
return $requirements;
}
if ($requirements instanceof Regex) {
return $requirements->getHtmlPattern();
}
return null;
}
private function getFormat($requirements)
{
if ($requirements instanceof Constraint && !$requirements instanceof Regex) {
$reflectionClass = new \ReflectionClass($requirements);
return $reflectionClass->getShortName();
2016-08-04 22:27:10 +02:00
}
return null;
2016-08-04 22:27:10 +02:00
}
private function getContentSchemaForType(OA\RequestBody $requestBody, string $type): OA\Schema
{
$requestBody->content = OA\UNDEFINED !== $requestBody->content ? $requestBody->content : [];
switch ($type) {
case 'json':
$contentType = 'application\json';
break;
case 'xml':
$contentType = 'application\xml';
break;
default:
throw new \InvalidArgumentException('Unsupported media type');
}
if (!isset($requestBody->content[$contentType])) {
$requestBody->content[$contentType] = new OA\MediaType(
[
'mediaType' => $contentType,
]
);
/** @var OA\Schema $schema */
$schema = Util::getChild(
$requestBody->content[$contentType],
OA\Schema::class
);
$schema->type = 'object';
}
return Util::getChild(
$requestBody->content[$contentType],
OA\Schema::class
);
}
private function describeCommonSchemaFromAnnotation(OA\Schema $schema, $annotation)
{
$schema->default = $annotation->getDefault();
if (OA\UNDEFINED === $schema->type) {
$schema->type = $annotation->map ? 'array' : 'string';
}
if ($annotation->map) {
$schema->type = 'array';
$schema->collectionFormat = 'multi';
$schema->items = Util::getChild($schema, OA\Items::class);
}
$pattern = $this->getPattern($annotation->requirements);
if (null !== $pattern) {
$schema->pattern = $pattern;
}
$format = $this->getFormat($annotation->requirements);
if (null !== $format) {
$schema->format = $format;
}
}
2016-08-04 22:27:10 +02:00
}