Skip to content

Commit

Permalink
fix(openapi): yaml parameters extractor (#5487)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorissae authored Mar 28, 2023
1 parent a7f4e0b commit f128e3b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Metadata/Extractor/YamlResourceExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use ApiPlatform\Metadata\Tests\Fixtures\StateOptions;
use ApiPlatform\OpenApi\Model\ExternalDocumentation;
use ApiPlatform\OpenApi\Model\Operation as OpenApiOperation;
use ApiPlatform\OpenApi\Model\Parameter;
use ApiPlatform\OpenApi\Model\RequestBody;
use ApiPlatform\State\OptionsInterface;
use Symfony\Component\Yaml\Exception\ParseException;
Expand Down Expand Up @@ -235,6 +236,28 @@ private function buildOpenapi(array $resource): bool|OpenApiOperation|null
unset($resource['openapi'][$key]);
}

if (\array_key_exists('parameters', $resource['openapi']) && \is_array($openapiParameters = $resource['openapi']['parameters'] ?? [])) {
$parameters = [];
foreach ($openapiParameters as $parameter) {
$parameters[] = new Parameter(
name: $parameter['name'],
in: $parameter['in'],
description: $parameter['description'] ?? '',
required: $parameter['required'] ?? false,
deprecated: $parameter['deprecated'] ?? false,
allowEmptyValue: $parameter['allowEmptyValue'] ?? false,
schema: $parameter['schema'] ?? [],
style: $parameter['style'] ?? null,
explode: $parameter['explode'] ?? false,
allowReserved: $parameter['allowReserved '] ?? false,
example: $parameter['example'] ?? null,
examples: isset($parameter['examples']) ? new \ArrayObject($parameter['examples']) : null,
content: isset($parameter['content']) ? new \ArrayObject($parameter['content']) : null
);
}
$resource['openapi']['parameters'] = $parameters;
}

return new OpenApiOperation(...$resource['openapi']);
}

Expand Down
18 changes: 18 additions & 0 deletions src/Metadata/Tests/Extractor/YamlExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,24 @@ public function testValidYaml(): void
], $extractor->getResources());
}

public function testOpenApiParameters(): void
{
$extractor = new YamlResourceExtractor([__DIR__.'/yaml/openapi.yaml']);
$resources = $extractor->getResources();

$this->assertArrayHasKey(Program::class, $resources);
$this->assertArrayHasKey('openapi', $resources[Program::class][0]);

$this->assertIsObject($resources[Program::class][0]['operations'][0]['openapi']);

$operation = $resources[Program::class][0]['operations'][0]['openapi'];
$this->assertIsArray($operation->getParameters());

$this->assertEquals('author', $operation->getParameters()[0]->getName());
$this->assertEquals('path', $operation->getParameters()[0]->getIn());
$this->assertEquals('john-doe', $operation->getParameters()[0]->getExample());
}

public function testInputAndOutputAreBooleans(): void
{
$extractor = new YamlResourceExtractor([__DIR__.'/yaml/input-and-output-are-booleans.yaml']);
Expand Down
14 changes: 14 additions & 0 deletions src/Metadata/Tests/Extractor/yaml/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
resources:
ApiPlatform\Metadata\Tests\Fixtures\ApiResource\Program:
- uriTemplate: /users/{author}/programs.{_format}
uriVariables: ['author']
operations:
ApiPlatform\Metadata\Post:
openapi:
parameters:
username:
name: author
in: path
example: "john-doe"
examples: ["John-doe"]
foo: "bar"

0 comments on commit f128e3b

Please sign in to comment.