From 87e7781f8c4f3bebae19bb63d425c0f071a497a4 Mon Sep 17 00:00:00 2001 From: soyuka Date: Fri, 5 Mar 2021 10:38:58 +0100 Subject: [PATCH 1/3] Fix #4080 --- src/OpenApi/Factory/OpenApiFactory.php | 2 +- tests/OpenApi/Factory/OpenApiFactoryTest.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/OpenApi/Factory/OpenApiFactory.php b/src/OpenApi/Factory/OpenApiFactory.php index d76d7d7b65c..63686443868 100644 --- a/src/OpenApi/Factory/OpenApiFactory.php +++ b/src/OpenApi/Factory/OpenApiFactory.php @@ -360,7 +360,7 @@ private function getLink(string $resourceClass, string $operationId, string $pat return new Model\Link( $operationId, new \ArrayObject($parameters), - [], + null, 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) ); } diff --git a/tests/OpenApi/Factory/OpenApiFactoryTest.php b/tests/OpenApi/Factory/OpenApiFactoryTest.php index bf51df5bf6d..6b04cb8376a 100644 --- a/tests/OpenApi/Factory/OpenApiFactoryTest.php +++ b/tests/OpenApi/Factory/OpenApiFactoryTest.php @@ -314,7 +314,7 @@ public function testInvoke(): void 'application/ld+json' => new Model\MediaType(new \ArrayObject(new \ArrayObject(['$ref' => '#/components/schemas/Dummy']))), ]), null, - 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}`.')]) + 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}`.')]) ), '400' => new Model\Response('Invalid input'), '422' => new Model\Response('Unprocessable entity'), @@ -366,7 +366,7 @@ public function testInvoke(): void 'application/ld+json' => new Model\MediaType(new \ArrayObject(['$ref' => '#/components/schemas/Dummy'])), ]), null, - 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}`.')]) + 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}`.')]) ), '400' => new Model\Response('Invalid input'), '422' => new Model\Response('Unprocessable entity'), @@ -436,7 +436,7 @@ public function testInvoke(): void 'text/csv' => new Model\MediaType(new \ArrayObject(['$ref' => '#/components/schemas/Dummy'])), ]), null, - 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}`.')]) + 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}`.')]) ), '400' => new Model\Response('Invalid input'), '422' => new Model\Response('Unprocessable entity'), From 102699c31ebe2a303b11322b2ed9e46a32790527 Mon Sep 17 00:00:00 2001 From: soyuka Date: Fri, 5 Mar 2021 15:12:21 +0100 Subject: [PATCH 2/3] Fix #4106 --- src/OpenApi/Factory/OpenApiFactory.php | 6 +++++ tests/OpenApi/Factory/OpenApiFactoryTest.php | 28 +++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/OpenApi/Factory/OpenApiFactory.php b/src/OpenApi/Factory/OpenApiFactory.php index 63686443868..8dbb40492f2 100644 --- a/src/OpenApi/Factory/OpenApiFactory.php +++ b/src/OpenApi/Factory/OpenApiFactory.php @@ -229,6 +229,12 @@ private function collectPaths(ResourceMetadata $resourceMetadata, string $resour $responses['default'] = new Model\Response('Unexpected error'); } + if ($contextResponses = $operation['openapi_context']['responses'] ?? false) { + foreach ($contextResponses as $statusCode => $contextResponse) { + $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); + } + } + $requestBody = null; if ($contextRequestBody = $operation['openapi_context']['requestBody'] ?? false) { $requestBody = new Model\RequestBody($contextRequestBody['description'] ?? '', new \ArrayObject($contextRequestBody['content']), $contextRequestBody['required'] ?? false); diff --git a/tests/OpenApi/Factory/OpenApiFactoryTest.php b/tests/OpenApi/Factory/OpenApiFactoryTest.php index 6b04cb8376a..d8b398d60fe 100644 --- a/tests/OpenApi/Factory/OpenApiFactoryTest.php +++ b/tests/OpenApi/Factory/OpenApiFactoryTest.php @@ -79,6 +79,23 @@ public function testInvoke(): void 'parameters' => [ ['description' => 'Test parameter', 'name' => 'param', 'in' => 'path', 'type' => 'string', 'required' => true, 'default' => 'BOTH'], ], + 'tags' => ['Dummy', 'Profile'], + 'responses' => [ + '202' => [ + 'description' => 'Success', + 'content' => [ + 'application/json' => [ + 'schema' => ['$ref' => '#/components/schemas/Dummy'], + ], + ], + 'headers' => [ + 'Foo' => ['description' => 'A nice header', 'schema' => ['type' => 'integer']], + ], + 'links' => [ + 'Foo' => ['$ref' => '#/components/schemas/Dummy'], + ], + ], + ], 'requestBody' => [ 'required' => true, 'description' => 'Custom request body', @@ -401,8 +418,17 @@ public function testInvoke(): void $customPath = $paths->getPath('/foo/{id}'); $this->assertEquals($customPath->getHead(), new Model\Operation( 'customDummyItem', - ['Dummy'], + ['Dummy', 'Profile'], [ + '202' => new Model\Response('Success', new \ArrayObject([ + 'application/json' => [ + 'schema' => ['$ref' => '#/components/schemas/Dummy'], + ], + ]), new \ArrayObject([ + 'Foo' => ['description' => 'A nice header', 'schema' => ['type' => 'integer']], + ]), new \ArrayObject([ + 'Foo' => ['$ref' => '#/components/schemas/Dummy'], + ])), '404' => new Model\Response('Resource not found'), ], 'Dummy', From f8f51664751ef5320ea47fb96db844c9a1709774 Mon Sep 17 00:00:00 2001 From: soyuka Date: Fri, 5 Mar 2021 17:34:04 +0100 Subject: [PATCH 3/3] changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73db78de70a..e590147558b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,8 @@ * OpenAPI: Fix error when schema is empty (#4051) * OpenAPI: Do not set scheme to oauth2 when generating securitySchemes (#4073) * OpenAPI: Fix missing `$ref` when no `type` is used in context (#4076) +* OpenAPI: Fix `response` support via the `openapi_context` (#4116) +* OpenAPI: Fix `Link->requestBody` default value (#4116) * GraphQL: Fix "Resource class cannot be determined." error when a null iterable field is returned (#4092) * Metadata: Check the output class when calculating serializer groups (#3696)