Skip to content

Commit

Permalink
add test and rework
Browse files Browse the repository at this point in the history
  • Loading branch information
jorissae committed Mar 23, 2023
1 parent 0f1c08c commit e3f89ef
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/Metadata/Extractor/YamlResourceExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,23 @@ private function buildOpenapi(array $resource): bool|OpenApiOperation|null
unset($resource['openapi'][$key]);
}

$parameters = [];
if (\array_key_exists('parameters', $resource['openapi'])) {
foreach ($resource['openapi']['parameters'] as $parameter) {
$parameters[] = new Parameter($parameter['name'], $parameter['in'], $parameter['description'] ?? '', $parameter['required'] ?? false, $parameter['deprecated'] ?? false, $parameter['allowEmptyValue'] ?? false, $parameter['schema'] ?? [], $parameter['style'] ?? null, $parameter['explode'] ?? false, $parameter['allowReserved '] ?? false, $parameter['example'] ?? null, isset($parameter['examples']) ? new \ArrayObject($parameter['examples']) : null, isset($parameter['content']) ? new \ArrayObject($parameter['content']) : null);
if (\array_key_exists('parameters', $resource['openapi']) && \is_array($openapiParameters = $resource['openapi']['parameters'])) {
$parameters = [];
$allowedProperties = array_map(fn (\ReflectionProperty $reflProperty): string => $reflProperty->getName(), (new \ReflectionClass(Parameter::class))->getProperties());
foreach ($openapiParameters as $parameter) {
foreach ($parameter as $key => $value) {
// prevent extra property not allowed on Parameter
if (!\in_array($key, $allowedProperties, true)) {
unset($parameter[$key]);
continue;
}
$parameter[$key] = match ($key) {
'examples' => new \ArrayObject($value),
'content' => new \ArrayObject($value),
default => $value,
};
}
$parameters[] = new Parameter(...$parameter);
}
$resource['openapi']['parameters'] = $parameters;
}
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 e3f89ef

Please sign in to comment.