Skip to content

Commit

Permalink
fix(openapi): parameters can disable openapi (#6440)
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka authored Jun 28, 2024
1 parent b42e25f commit 57f930c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Metadata/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/OpenApi/Factory/OpenApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

Expand Down
4 changes: 2 additions & 2 deletions src/OpenApi/Model/PathItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/TestBundle/ApiResource/WithParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
)]
Expand Down
16 changes: 16 additions & 0 deletions tests/Functional/Parameters/ParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 57f930c

Please sign in to comment.