mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-09 02:59:27 +03:00
Add support of inline path parameters (#1973)
* Add support of inline path parameters * Fix CS
This commit is contained in:
parent
8111ce645b
commit
2295f68b89
@ -17,6 +17,7 @@ use Nelmio\ApiDocBundle\Annotation\Security;
|
|||||||
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
|
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
|
||||||
use Nelmio\ApiDocBundle\Util\ControllerReflector;
|
use Nelmio\ApiDocBundle\Util\ControllerReflector;
|
||||||
use Nelmio\ApiDocBundle\Util\SetsContextTrait;
|
use Nelmio\ApiDocBundle\Util\SetsContextTrait;
|
||||||
|
use OpenApi\Analysers\AttributeAnnotationFactory;
|
||||||
use OpenApi\Annotations as OA;
|
use OpenApi\Annotations as OA;
|
||||||
use OpenApi\Generator;
|
use OpenApi\Generator;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
@ -67,14 +68,14 @@ final class OpenApiPhpDescriber
|
|||||||
$classAnnotations = array_filter($this->annotationReader->getClassAnnotations($declaringClass), function ($v) {
|
$classAnnotations = array_filter($this->annotationReader->getClassAnnotations($declaringClass), function ($v) {
|
||||||
return $v instanceof OA\AbstractAnnotation;
|
return $v instanceof OA\AbstractAnnotation;
|
||||||
});
|
});
|
||||||
$classAnnotations = array_merge($classAnnotations, $this->getAttributesAsAnnotation($declaringClass, OA\AbstractAnnotation::class));
|
$classAnnotations = array_merge($classAnnotations, $this->getAttributesAsAnnotation($declaringClass, $context));
|
||||||
$classAnnotations[$declaringClass->getName()] = $classAnnotations;
|
$classAnnotations[$declaringClass->getName()] = $classAnnotations;
|
||||||
}
|
}
|
||||||
|
|
||||||
$annotations = array_filter($this->annotationReader->getMethodAnnotations($method), function ($v) {
|
$annotations = array_filter($this->annotationReader->getMethodAnnotations($method), function ($v) {
|
||||||
return $v instanceof OA\AbstractAnnotation;
|
return $v instanceof OA\AbstractAnnotation;
|
||||||
});
|
});
|
||||||
$annotations = array_merge($annotations, $this->getAttributesAsAnnotation($method, OA\AbstractAnnotation::class));
|
$annotations = array_merge($annotations, $this->getAttributesAsAnnotation($method, $context));
|
||||||
|
|
||||||
if (0 === count($annotations) && 0 === count($classAnnotations[$declaringClass->getName()])) {
|
if (0 === count($annotations) && 0 === count($classAnnotations[$declaringClass->getName()])) {
|
||||||
continue;
|
continue;
|
||||||
@ -205,17 +206,18 @@ final class OpenApiPhpDescriber
|
|||||||
*
|
*
|
||||||
* @return OA\AbstractAnnotation[]
|
* @return OA\AbstractAnnotation[]
|
||||||
*/
|
*/
|
||||||
private function getAttributesAsAnnotation($reflection, string $className): array
|
private function getAttributesAsAnnotation($reflection, \OpenApi\Context $context): array
|
||||||
{
|
{
|
||||||
$annotations = [];
|
// BC zircote/swagger-php < 4.0
|
||||||
if (\PHP_VERSION_ID < 80100) {
|
if (!class_exists(AttributeAnnotationFactory::class)) {
|
||||||
return $annotations;
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($reflection->getAttributes($className, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
|
$attributesFactory = new AttributeAnnotationFactory();
|
||||||
$annotations[] = $attribute->newInstance();
|
$attributes = $attributesFactory->build($reflection, $context);
|
||||||
}
|
// The attributes factory removes the context after executing so we need to set it back...
|
||||||
|
$this->setContext($context);
|
||||||
|
|
||||||
return $annotations;
|
return $attributes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,4 +58,11 @@ class ApiController81 extends ApiController80
|
|||||||
public function securityOverrideActionAttributes()
|
public function securityOverrideActionAttributes()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[Route('/inline_path_parameters')]
|
||||||
|
#[OA\Response(response: '200', description: '')]
|
||||||
|
public function inlinePathParameters(
|
||||||
|
#[OA\PathParameter] string $product_id
|
||||||
|
) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -387,6 +387,19 @@ class FunctionalTest extends WebTestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testInlinePHP81Parameters()
|
||||||
|
{
|
||||||
|
if (\PHP_VERSION_ID < 80100) {
|
||||||
|
$this->markTestSkipped('Attributes require PHP 8.1');
|
||||||
|
}
|
||||||
|
|
||||||
|
$operation = $this->getOperation('/api/inline_path_parameters', 'get');
|
||||||
|
$this->assertCount(1, $operation->parameters);
|
||||||
|
$this->assertInstanceOf(OA\PathParameter::class, $operation->parameters[0]);
|
||||||
|
$this->assertSame($operation->parameters[0]->name, 'product_id');
|
||||||
|
$this->assertSame($operation->parameters[0]->schema->type, 'string');
|
||||||
|
}
|
||||||
|
|
||||||
public function testClassSecurityAction()
|
public function testClassSecurityAction()
|
||||||
{
|
{
|
||||||
$operation = $this->getOperation('/api/security/class', 'get');
|
$operation = $this->getOperation('/api/security/class', 'get');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user