From 394033c4a17c2359a0dbbfea7a4bcccd052b1233 Mon Sep 17 00:00:00 2001 From: "cristian.almohalla" Date: Tue, 29 Dec 2020 11:17:03 +0100 Subject: [PATCH] allow nullable response parameters --- src/OpenApi/OpenApiSchemaParser.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/OpenApi/OpenApiSchemaParser.php b/src/OpenApi/OpenApiSchemaParser.php index 32b01e9..743dead 100644 --- a/src/OpenApi/OpenApiSchemaParser.php +++ b/src/OpenApi/OpenApiSchemaParser.php @@ -44,6 +44,11 @@ public function fromResponse(string $path, string $method, int $statusCode, stri private function extractData(array $data): array { $aux = []; + + if (\array_key_exists('nullable', $data)) { + $data = $this->convertNullableTypeToJsonSchema($data); + } + foreach ($data as $key => $elem) { if ('$ref' === $key) { $aux = $this->findDefinition($elem); @@ -116,4 +121,20 @@ private function assertContentTypeRoot( ); } } + + private function convertNullableTypeToJsonSchema(array $data): array + { + $data['oneOf'] = [ + ['type' => null], + ]; + + if (\array_key_exists('type', $data)) { + $data['oneOf'][] = ['type' => $data['type']]; + unset($data['type']); + } + + unset($data['nullable']); + + return $data; + } }