diff --git a/src/Metadata/Parameter.php b/src/Metadata/Parameter.php index fd340e4e193..ff0667f3d3b 100644 --- a/src/Metadata/Parameter.php +++ b/src/Metadata/Parameter.php @@ -32,7 +32,7 @@ abstract class Parameter public function __construct( protected ?string $key = null, protected ?array $schema = null, - protected ?OpenApi\Model\Parameter $openApi = null, + protected OpenApi\Model\Parameter|bool|null $openApi = null, // TODO: use false as type instead of bool protected mixed $provider = null, protected mixed $filter = null, protected ?string $property = null, @@ -57,7 +57,7 @@ public function getSchema(): ?array return $this->schema; } - public function getOpenApi(): ?OpenApi\Model\Parameter + public function getOpenApi(): OpenApi\Model\Parameter|bool|null { return $this->openApi; } diff --git a/src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.php b/src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.php index b4d0c8e973f..f06cd87dcb0 100644 --- a/src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.php +++ b/src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.php @@ -142,11 +142,11 @@ private function setDefaults(string $key, Parameter $parameter, string $resource } } - $schema = $parameter->getSchema() ?? $parameter->getOpenApi()?->getSchema(); + $schema = $parameter->getSchema() ?? (($openApi = $parameter->getOpenApi()) ? $openApi->getSchema() : null); // Only add validation if the Symfony Validator is installed if (interface_exists(ValidatorInterface::class) && !$parameter->getConstraints()) { - $parameter = $this->addSchemaValidation($parameter, $schema, $parameter->getRequired() ?? $description['required'] ?? false, $parameter->getOpenApi()); + $parameter = $this->addSchemaValidation($parameter, $schema, $parameter->getRequired() ?? $description['required'] ?? false, $parameter->getOpenApi() ?: null); } return $parameter; diff --git a/src/OpenApi/Factory/OpenApiFactory.php b/src/OpenApi/Factory/OpenApiFactory.php index b059d9c183d..eb8bb5a333f 100644 --- a/src/OpenApi/Factory/OpenApiFactory.php +++ b/src/OpenApi/Factory/OpenApiFactory.php @@ -309,6 +309,10 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection $openapiParameters = $openapiOperation->getParameters(); foreach ($operation->getParameters() ?? [] as $key => $p) { + if (false === $p->getOpenApi()) { + continue; + } + $in = $p instanceof HeaderParameterInterface ? 'header' : 'query'; $parameter = new Parameter($key, $in, $p->getDescription() ?? "$resourceShortName $key", $p->getRequired() ?? false, false, false, $p->getSchema() ?? ['type' => 'string']); diff --git a/src/OpenApi/Model/PathItem.php b/src/OpenApi/Model/PathItem.php index 42a6d883e3f..9d84173d200 100644 --- a/src/OpenApi/Model/PathItem.php +++ b/src/OpenApi/Model/PathItem.php @@ -19,7 +19,7 @@ final class PathItem public static array $methods = ['GET', 'PUT', 'POST', 'DELETE', 'OPTIONS', 'HEAD', 'PATCH', 'TRACE']; - public function __construct(private ?string $ref = null, private ?string $summary = null, private ?string $description = null, private ?Operation $get = null, private ?Operation $put = null, private ?Operation $post = null, private ?Operation $delete = null, private ?Operation $options = null, private ?Operation $head = null, private ?Operation $patch = null, private ?Operation $trace = null, private ?array $servers = null, private array $parameters = []) + public function __construct(private ?string $ref = null, private ?string $summary = null, private ?string $description = null, private ?Operation $get = null, private ?Operation $put = null, private ?Operation $post = null, private ?Operation $delete = null, private ?Operation $options = null, private ?Operation $head = null, private ?Operation $patch = null, private ?Operation $trace = null, private ?array $servers = null, private ?array $parameters = null) { } @@ -184,7 +184,7 @@ public function withServers(?array $servers = null): self return $clone; } - public function withParameters(array $parameters): self + public function withParameters(?array $parameters = null): self { $clone = clone $this; $clone->parameters = $parameters; diff --git a/tests/Fixtures/TestBundle/ApiResource/WithParameter.php b/tests/Fixtures/TestBundle/ApiResource/WithParameter.php index a2dbf296ceb..6a3af09b1bb 100644 --- a/tests/Fixtures/TestBundle/ApiResource/WithParameter.php +++ b/tests/Fixtures/TestBundle/ApiResource/WithParameter.php @@ -38,7 +38,7 @@ 'auth' => new HeaderParameter(provider: [self::class, 'restrictAccess']), 'priority' => new QueryParameter(provider: [self::class, 'assertSecond'], priority: 10), 'priorityb' => new QueryParameter(provider: [self::class, 'assertFirst'], priority: 20), - 'array' => new QueryParameter(provider: [self::class, 'assertArray']), + 'array' => new QueryParameter(provider: [self::class, 'assertArray'], openApi: false), ], provider: [self::class, 'provide'] )] diff --git a/tests/Functional/Parameters/ParameterTest.php b/tests/Functional/Parameters/ParameterTest.php index c0b169e0c71..f14309c6460 100644 --- a/tests/Functional/Parameters/ParameterTest.php +++ b/tests/Functional/Parameters/ParameterTest.php @@ -48,4 +48,20 @@ public function testWithHeader(): void self::createClient()->request('GET', 'with_parameters/1?service=blabla', ['headers' => ['auth' => 'foo']]); $this->assertResponseStatusCodeSame(403); } + + /** + * Because of the openapiContext deprecation. + * + * @group legacy + */ + public function testDisableOpenApi(): void + { + $response = self::createClient()->request('GET', 'docs', ['headers' => ['accept' => 'application/vnd.openapi+json']]); + $keys = []; + foreach ($response->toArray(false)['paths']['/with_parameters/{id}']['get']['parameters'] as $parameter) { + $keys[] = $parameter['name']; + } + + $this->assertNotContains('array', $keys); + } }