From c3cd0788713dc5ca384100180914aa603dd2ba2c Mon Sep 17 00:00:00 2001 From: soyuka Date: Sun, 31 Jan 2021 14:56:00 +0100 Subject: [PATCH] Openapi context: parameters, requestBody missing --- src/OpenApi/Factory/OpenApiFactory.php | 10 ++++- tests/OpenApi/Factory/OpenApiFactoryTest.php | 39 +++++++++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/OpenApi/Factory/OpenApiFactory.php b/src/OpenApi/Factory/OpenApiFactory.php index f65231a2dfb..6e878b8f9db 100644 --- a/src/OpenApi/Factory/OpenApiFactory.php +++ b/src/OpenApi/Factory/OpenApiFactory.php @@ -162,6 +162,12 @@ private function collectPaths(ResourceMetadata $resourceMetadata, string $resour $parameters = []; $responses = []; + if ($operation['openapi_context']['parameters'] ?? false) { + foreach ($operation['openapi_context']['parameters'] as $parameter) { + $parameters[] = new Model\Parameter($parameter['name'], $parameter['in'], $parameter['description'] ?? '', $parameter['required'] ?? false, $parameter['deprecated'] ?? false, $parameter['allowEmptyValue'] ?? false, $parameter['schema'] ?? []); + } + } + // Set up parameters if (OperationType::ITEM === $operationType) { foreach ($identifiers as $parameterName => $identifier) { @@ -218,7 +224,9 @@ private function collectPaths(ResourceMetadata $resourceMetadata, string $resour } $requestBody = null; - if ('PUT' === $method || 'POST' === $method || 'PATCH' === $method) { + if ($contextRequestBody = $operation['openapi_context']['requestBody'] ?? false) { + $requestBody = new Model\RequestBody($contextRequestBody['description'] ?? '', new \ArrayObject($contextRequestBody['content']), $contextRequestBody['required'] ?? false); + } elseif ('PUT' === $method || 'POST' === $method || 'PATCH' === $method) { $operationInputSchemas = []; foreach ($requestMimeTypes as $operationFormat) { $operationInputSchema = $this->jsonSchemaFactory->buildSchema($resourceClass, $operationFormat, Schema::TYPE_INPUT, $operationType, $operationName, new Schema('openapi'), null, $forceSchemaCollection); diff --git a/tests/OpenApi/Factory/OpenApiFactoryTest.php b/tests/OpenApi/Factory/OpenApiFactoryTest.php index 52cabc750e4..7f9843e5fd5 100644 --- a/tests/OpenApi/Factory/OpenApiFactoryTest.php +++ b/tests/OpenApi/Factory/OpenApiFactoryTest.php @@ -67,7 +67,29 @@ public function testInvoke(): void 'get' => ['method' => 'GET'] + self::OPERATION_FORMATS, 'put' => ['method' => 'PUT'] + self::OPERATION_FORMATS, 'delete' => ['method' => 'DELETE'] + self::OPERATION_FORMATS, - 'custom' => ['method' => 'HEAD', 'path' => '/foo/{id}', 'openapi_context' => ['description' => 'Custom description']] + self::OPERATION_FORMATS, + 'custom' => ['method' => 'HEAD', 'path' => '/foo/{id}', 'openapi_context' => [ + 'description' => 'Custom description', + 'parameters' => [ + ['description' => 'Test parameter', 'name' => 'param', 'in' => 'path', 'type' => 'string', 'required' => true, 'default' => 'BOTH'], + ], + 'requestBody' => [ + 'required' => true, + 'description' => 'Custom request body', + 'content' => [ + 'multipart/form-data' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'file' => [ + 'type' => 'string', + 'format' => 'binary', + ], + ], + ], + ], + ], + ], + ]] + self::OPERATION_FORMATS, 'formats' => ['method' => 'PUT', 'path' => '/formatted/{id}', 'output_formats' => ['json' => ['application/json'], 'csv' => ['text/csv']], 'input_formats' => ['json' => ['application/json'], 'csv' => ['text/csv']]], ], [ @@ -377,7 +399,20 @@ public function testInvoke(): void 'Dummy', 'Custom description', null, - [new Model\Parameter('id', 'path', 'Resource identifier', true, false, false, ['type' => 'string'])] + [new Model\Parameter('param', 'path', 'Test parameter', true), new Model\Parameter('id', 'path', 'Resource identifier', true, false, false, ['type' => 'string'])], + new Model\RequestBody('Custom request body', new \ArrayObject([ + 'multipart/form-data' => [ + 'schema' => [ + 'type' => 'object', + 'properties' => [ + 'file' => [ + 'type' => 'string', + 'format' => 'binary', + ], + ], + ], + ], + ]), true) )); $formattedPath = $paths->getPath('/formatted/{id}');