Skip to content

Commit 20baa61

Browse files
authored
fix(openapi): throw clear error for openapi parameter missing name in yaml config (#8297)
Fixes #8013
1 parent 098d527 commit 20baa61

3 files changed

Lines changed: 25 additions & 0 deletions

File tree

src/Metadata/Extractor/YamlResourceExtractor.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,14 @@ private function buildOpenapi(array $resource): bool|OpenApiOperation|null
258258
if (\array_key_exists('parameters', $resource['openapi']) && \is_array($openapiParameters = $resource['openapi']['parameters'] ?? [])) {
259259
$parameters = [];
260260
foreach ($openapiParameters as $parameter) {
261+
if (!isset($parameter['name'])) {
262+
throw new InvalidArgumentException('OpenAPI parameter is missing the required "name" key.');
263+
}
264+
265+
if (!isset($parameter['in'])) {
266+
throw new InvalidArgumentException(\sprintf('OpenAPI parameter "%s" is missing the required "in" key.', $parameter['name']));
267+
}
268+
261269
$parameters[] = new Parameter(
262270
name: $parameter['name'],
263271
in: $parameter['in'],

src/Metadata/Tests/Extractor/YamlExtractorTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,14 @@ public function testOpenApiParameters(): void
518518
$this->assertEquals('john-doe', $operation->getParameters()[0]->getExample());
519519
}
520520

521+
public function testOpenApiParameterWithoutNameThrowsClearException(): void
522+
{
523+
$this->expectException(InvalidArgumentException::class);
524+
$this->expectExceptionMessage('OpenAPI parameter is missing the required "name" key');
525+
526+
(new YamlResourceExtractor([__DIR__.'/yaml/openapi-parameter-without-name.yaml']))->getResources();
527+
}
528+
521529
public function testInputAndOutputAreBooleans(): void
522530
{
523531
$extractor = new YamlResourceExtractor([__DIR__.'/yaml/input-and-output-are-booleans.yaml']);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
resources:
2+
ApiPlatform\Metadata\Tests\Fixtures\ApiResource\Program:
3+
- uriTemplate: /programs.{_format}
4+
operations:
5+
ApiPlatform\Metadata\GetCollection:
6+
openapi:
7+
parameters:
8+
- in: query
9+
description: A parameter without a name

0 commit comments

Comments
 (0)