NelmioApiDocBundle/RouteDescriber/FosRestDescriber.php
Filip Benčo 78664ef9ec
OpenApi 3 Support (#1623)
* Initial pass for OA3 upgrade

* Fix Util Tests

* Fix first batch of Unit Tests. Up to Model

* Another batch of fixed tests

* Update annotations

* Convert Model & Property Describers

* Update tests, Fix RouteDescribers, FIx additional bugs

* Another batch of updates

* Another batch of fixed Functional Tests

* Fix FunctionalTest tests

* Fix Bazinga Tests

* FIx FOS Rest

* Fix JMS TEsts & describers

* Fix all Tests

* Fix few stuff from own CR

* CS Fixes

* CS Fixes 2

* CS Fixes 3

* CS Fixes 4

* Remove collection bug

* Updates after first CRs

* CS

* Drop support for SF3

* Update the docs

* Add an upgrade guide

* misc doc fixes

* Configurable media types

* Code Style Fixes

* Don't use ::$ref for @Response and @RequestBody

* Fix upgrading guide

* Fix OA case

Co-authored-by: Filip Benčo <filip.benco@websupport.sk>
Co-authored-by: Guilhem Niot <guilhem.niot@gmail.com>
Co-authored-by: Mantis Development <mantis@users.noreply.github.com>
2020-05-28 13:19:11 +02:00

171 lines
5.8 KiB
PHP

<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\RouteDescriber;
use Doctrine\Common\Annotations\Reader;
use FOS\RestBundle\Controller\Annotations\QueryParam;
use FOS\RestBundle\Controller\Annotations\RequestParam;
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use OpenApi\Annotations as OA;
use Symfony\Component\Routing\Route;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Regex;
final class FosRestDescriber implements RouteDescriberInterface
{
use RouteDescriberTrait;
/** @var Reader */
private $annotationReader;
/** @var string[] */
private $mediaTypes;
public function __construct(Reader $annotationReader, array $mediaTypes)
{
$this->annotationReader = $annotationReader;
$this->mediaTypes = $mediaTypes;
}
public function describe(OA\OpenApi $api, Route $route, \ReflectionMethod $reflectionMethod)
{
$annotations = $this->annotationReader->getMethodAnnotations($reflectionMethod);
$annotations = array_filter($annotations, static function ($value) {
return $value instanceof RequestParam || $value instanceof QueryParam;
});
foreach ($this->getOperations($api, $route) as $operation) {
foreach ($annotations as $annotation) {
$parameterName = $annotation->key ?? $annotation->getName(); // the key used by fosrest
if ($annotation instanceof QueryParam) {
$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;
}
$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);
}
}
}
}
}
private function getPattern($requirements)
{
if (is_array($requirements) && isset($requirements['rule'])) {
return (string) $requirements['rule'];
}
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();
}
return null;
}
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;
}
}
}