Skip to content

Commit 57f930c

Browse files
authored
fix(openapi): parameters can disable openapi (#6440)
1 parent b42e25f commit 57f930c

File tree

6 files changed

+27
-7
lines changed

6 files changed

+27
-7
lines changed

src/Metadata/Parameter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ abstract class Parameter
3232
public function __construct(
3333
protected ?string $key = null,
3434
protected ?array $schema = null,
35-
protected ?OpenApi\Model\Parameter $openApi = null,
35+
protected OpenApi\Model\Parameter|bool|null $openApi = null, // TODO: use false as type instead of bool
3636
protected mixed $provider = null,
3737
protected mixed $filter = null,
3838
protected ?string $property = null,
@@ -57,7 +57,7 @@ public function getSchema(): ?array
5757
return $this->schema;
5858
}
5959

60-
public function getOpenApi(): ?OpenApi\Model\Parameter
60+
public function getOpenApi(): OpenApi\Model\Parameter|bool|null
6161
{
6262
return $this->openApi;
6363
}

src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ private function setDefaults(string $key, Parameter $parameter, string $resource
142142
}
143143
}
144144

145-
$schema = $parameter->getSchema() ?? $parameter->getOpenApi()?->getSchema();
145+
$schema = $parameter->getSchema() ?? (($openApi = $parameter->getOpenApi()) ? $openApi->getSchema() : null);
146146

147147
// Only add validation if the Symfony Validator is installed
148148
if (interface_exists(ValidatorInterface::class) && !$parameter->getConstraints()) {
149-
$parameter = $this->addSchemaValidation($parameter, $schema, $parameter->getRequired() ?? $description['required'] ?? false, $parameter->getOpenApi());
149+
$parameter = $this->addSchemaValidation($parameter, $schema, $parameter->getRequired() ?? $description['required'] ?? false, $parameter->getOpenApi() ?: null);
150150
}
151151

152152
return $parameter;

src/OpenApi/Factory/OpenApiFactory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,10 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
309309

310310
$openapiParameters = $openapiOperation->getParameters();
311311
foreach ($operation->getParameters() ?? [] as $key => $p) {
312+
if (false === $p->getOpenApi()) {
313+
continue;
314+
}
315+
312316
$in = $p instanceof HeaderParameterInterface ? 'header' : 'query';
313317
$parameter = new Parameter($key, $in, $p->getDescription() ?? "$resourceShortName $key", $p->getRequired() ?? false, false, false, $p->getSchema() ?? ['type' => 'string']);
314318

src/OpenApi/Model/PathItem.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ final class PathItem
1919

2020
public static array $methods = ['GET', 'PUT', 'POST', 'DELETE', 'OPTIONS', 'HEAD', 'PATCH', 'TRACE'];
2121

22-
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 = [])
22+
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)
2323
{
2424
}
2525

@@ -184,7 +184,7 @@ public function withServers(?array $servers = null): self
184184
return $clone;
185185
}
186186

187-
public function withParameters(array $parameters): self
187+
public function withParameters(?array $parameters = null): self
188188
{
189189
$clone = clone $this;
190190
$clone->parameters = $parameters;

tests/Fixtures/TestBundle/ApiResource/WithParameter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
'auth' => new HeaderParameter(provider: [self::class, 'restrictAccess']),
3939
'priority' => new QueryParameter(provider: [self::class, 'assertSecond'], priority: 10),
4040
'priorityb' => new QueryParameter(provider: [self::class, 'assertFirst'], priority: 20),
41-
'array' => new QueryParameter(provider: [self::class, 'assertArray']),
41+
'array' => new QueryParameter(provider: [self::class, 'assertArray'], openApi: false),
4242
],
4343
provider: [self::class, 'provide']
4444
)]

tests/Functional/Parameters/ParameterTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,20 @@ public function testWithHeader(): void
4848
self::createClient()->request('GET', 'with_parameters/1?service=blabla', ['headers' => ['auth' => 'foo']]);
4949
$this->assertResponseStatusCodeSame(403);
5050
}
51+
52+
/**
53+
* Because of the openapiContext deprecation.
54+
*
55+
* @group legacy
56+
*/
57+
public function testDisableOpenApi(): void
58+
{
59+
$response = self::createClient()->request('GET', 'docs', ['headers' => ['accept' => 'application/vnd.openapi+json']]);
60+
$keys = [];
61+
foreach ($response->toArray(false)['paths']['/with_parameters/{id}']['get']['parameters'] as $parameter) {
62+
$keys[] = $parameter['name'];
63+
}
64+
65+
$this->assertNotContains('array', $keys);
66+
}
5167
}

0 commit comments

Comments
 (0)