Skip to content

Commit a32b677

Browse files
authored
OpenAPI fixes (#4116)
1 parent d362120 commit a32b677

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* OpenAPI: Fix error when schema is empty (#4051)
2121
* OpenAPI: Do not set scheme to oauth2 when generating securitySchemes (#4073)
2222
* OpenAPI: Fix missing `$ref` when no `type` is used in context (#4076)
23+
* OpenAPI: Fix `response` support via the `openapi_context` (#4116)
24+
* OpenAPI: Fix `Link->requestBody` default value (#4116)
2325
* GraphQL: Fix "Resource class cannot be determined." error when a null iterable field is returned (#4092)
2426
* Metadata: Check the output class when calculating serializer groups (#3696)
2527

src/OpenApi/Factory/OpenApiFactory.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ private function collectPaths(ResourceMetadata $resourceMetadata, string $resour
229229
$responses['default'] = new Model\Response('Unexpected error');
230230
}
231231

232+
if ($contextResponses = $operation['openapi_context']['responses'] ?? false) {
233+
foreach ($contextResponses as $statusCode => $contextResponse) {
234+
$responses[$statusCode] = new Model\Response($contextResponse['description'] ?? '', new \ArrayObject($contextResponse['content']), isset($contextResponse['headers']) ? new \ArrayObject($contextResponse['headers']) : null, isset($contextResponse['links']) ? new \ArrayObject($contextResponse['links']) : null);
235+
}
236+
}
237+
232238
$requestBody = null;
233239
if ($contextRequestBody = $operation['openapi_context']['requestBody'] ?? false) {
234240
$requestBody = new Model\RequestBody($contextRequestBody['description'] ?? '', new \ArrayObject($contextRequestBody['content']), $contextRequestBody['required'] ?? false);
@@ -360,7 +366,7 @@ private function getLink(string $resourceClass, string $operationId, string $pat
360366
return new Model\Link(
361367
$operationId,
362368
new \ArrayObject($parameters),
363-
[],
369+
null,
364370
1 === \count($parameters) ? sprintf('The `%1$s` value returned in the response can be used as the `%1$s` parameter in `GET %2$s`.', key($parameters), $path) : sprintf('The values returned in the response can be used in `GET %s`.', $path)
365371
);
366372
}

tests/OpenApi/Factory/OpenApiFactoryTest.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,23 @@ public function testInvoke(): void
7979
'parameters' => [
8080
['description' => 'Test parameter', 'name' => 'param', 'in' => 'path', 'type' => 'string', 'required' => true, 'default' => 'BOTH'],
8181
],
82+
'tags' => ['Dummy', 'Profile'],
83+
'responses' => [
84+
'202' => [
85+
'description' => 'Success',
86+
'content' => [
87+
'application/json' => [
88+
'schema' => ['$ref' => '#/components/schemas/Dummy'],
89+
],
90+
],
91+
'headers' => [
92+
'Foo' => ['description' => 'A nice header', 'schema' => ['type' => 'integer']],
93+
],
94+
'links' => [
95+
'Foo' => ['$ref' => '#/components/schemas/Dummy'],
96+
],
97+
],
98+
],
8299
'requestBody' => [
83100
'required' => true,
84101
'description' => 'Custom request body',
@@ -314,7 +331,7 @@ public function testInvoke(): void
314331
'application/ld+json' => new Model\MediaType(new \ArrayObject(new \ArrayObject(['$ref' => '#/components/schemas/Dummy']))),
315332
]),
316333
null,
317-
new \ArrayObject(['GetDummyItem' => new Model\Link('getDummyItem', new \ArrayObject(['id' => '$response.body#/id']), [], 'The `id` value returned in the response can be used as the `id` parameter in `GET /dummies/{id}`.')])
334+
new \ArrayObject(['GetDummyItem' => new Model\Link('getDummyItem', new \ArrayObject(['id' => '$response.body#/id']), null, 'The `id` value returned in the response can be used as the `id` parameter in `GET /dummies/{id}`.')])
318335
),
319336
'400' => new Model\Response('Invalid input'),
320337
'422' => new Model\Response('Unprocessable entity'),
@@ -366,7 +383,7 @@ public function testInvoke(): void
366383
'application/ld+json' => new Model\MediaType(new \ArrayObject(['$ref' => '#/components/schemas/Dummy'])),
367384
]),
368385
null,
369-
new \ArrayObject(['GetDummyItem' => new Model\Link('getDummyItem', new \ArrayObject(['id' => '$response.body#/id']), [], 'The `id` value returned in the response can be used as the `id` parameter in `GET /dummies/{id}`.')])
386+
new \ArrayObject(['GetDummyItem' => new Model\Link('getDummyItem', new \ArrayObject(['id' => '$response.body#/id']), null, 'The `id` value returned in the response can be used as the `id` parameter in `GET /dummies/{id}`.')])
370387
),
371388
'400' => new Model\Response('Invalid input'),
372389
'422' => new Model\Response('Unprocessable entity'),
@@ -401,8 +418,17 @@ public function testInvoke(): void
401418
$customPath = $paths->getPath('/foo/{id}');
402419
$this->assertEquals($customPath->getHead(), new Model\Operation(
403420
'customDummyItem',
404-
['Dummy'],
421+
['Dummy', 'Profile'],
405422
[
423+
'202' => new Model\Response('Success', new \ArrayObject([
424+
'application/json' => [
425+
'schema' => ['$ref' => '#/components/schemas/Dummy'],
426+
],
427+
]), new \ArrayObject([
428+
'Foo' => ['description' => 'A nice header', 'schema' => ['type' => 'integer']],
429+
]), new \ArrayObject([
430+
'Foo' => ['$ref' => '#/components/schemas/Dummy'],
431+
])),
406432
'404' => new Model\Response('Resource not found'),
407433
],
408434
'Dummy',
@@ -436,7 +462,7 @@ public function testInvoke(): void
436462
'text/csv' => new Model\MediaType(new \ArrayObject(['$ref' => '#/components/schemas/Dummy'])),
437463
]),
438464
null,
439-
new \ArrayObject(['GetDummyItem' => new Model\Link('getDummyItem', new \ArrayObject(['id' => '$response.body#/id']), [], 'The `id` value returned in the response can be used as the `id` parameter in `GET /dummies/{id}`.')])
465+
new \ArrayObject(['GetDummyItem' => new Model\Link('getDummyItem', new \ArrayObject(['id' => '$response.body#/id']), null, 'The `id` value returned in the response can be used as the `id` parameter in `GET /dummies/{id}`.')])
440466
),
441467
'400' => new Model\Response('Invalid input'),
442468
'422' => new Model\Response('Unprocessable entity'),

0 commit comments

Comments
 (0)