From f329ec6b1ea5824bc3f905670267d3b454b0c1c7 Mon Sep 17 00:00:00 2001 From: Ronan McCarter Date: Thu, 17 Aug 2023 10:14:12 -0700 Subject: [PATCH 1/2] add empty check for properties --- src/Writing/OpenAPISpecWriter.php | 46 +++++++++++++++---------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/Writing/OpenAPISpecWriter.php b/src/Writing/OpenAPISpecWriter.php index 4d593bb8..b81201c4 100644 --- a/src/Writing/OpenAPISpecWriter.php +++ b/src/Writing/OpenAPISpecWriter.php @@ -22,17 +22,9 @@ class OpenAPISpecWriter private DocumentationConfig $config; - /** - * Object to represent empty values, since empty arrays get serialised as objects. - * Can't use a constant because of initialisation expression. - * - */ - public \stdClass $EMPTY; - public function __construct(DocumentationConfig $config = null) { $this->config = $config ?: new DocumentationConfig(config('scribe', [])); - $this->EMPTY = new \stdClass(); } /** @@ -241,6 +233,9 @@ protected function generateEndpointRequestBodySpec(OutputEndpointData $endpoint) $schema['properties'][$name] = $fieldData; } + if (array_key_exists('properties', $schema)) { + $schema['properties'] = $this->objectIfEmpty($schema['properties']); + } $body['required'] = $hasRequiredParameter; if ($hasFileParameter) { @@ -257,7 +252,7 @@ protected function generateEndpointRequestBodySpec(OutputEndpointData $endpoint) } // return object rather than empty array, so can get properly serialised as object - return count($body) > 0 ? $body : $this->EMPTY; + return $this->objectIfEmpty($body); } protected function generateEndpointResponsesSpec(OutputEndpointData $endpoint) @@ -282,7 +277,7 @@ protected function generateEndpointResponsesSpec(OutputEndpointData $endpoint) } // return object rather than empty array, so can get properly serialised as object - return count($responses) > 0 ? $responses : $this->EMPTY; + return $this->objectIfEmpty($responses); } protected function getResponseDescription(Response $response): string @@ -386,16 +381,12 @@ protected function generateResponseContentSpec(?string $responseContent, OutputE return [$key => $this->generateSchemaForValue($value, $endpoint, $key)]; })->toArray(); - if (!count($properties)) { - $properties = $this->EMPTY; - } - return [ 'application/json' => [ 'schema' => [ 'type' => 'object', 'example' => $decoded, - 'properties' => $properties, + 'properties' => $this->objectIfEmpty($properties), ], ], ]; @@ -513,9 +504,9 @@ public function generateFieldData($field): array 'type' => 'object', 'description' => $field->description ?: '', 'example' => $field->example, - 'properties' => collect($field->__fields)->mapWithKeys(function ($subfield, $subfieldName) { + 'properties' => $this->objectIfEmpty(collect($field->__fields)->mapWithKeys(function ($subfield, $subfieldName) { return [$subfieldName => $this->generateFieldData($subfield)]; - })->all(), + })->all()), ]; } else { return [ @@ -534,6 +525,15 @@ protected function operationId(OutputEndpointData $endpoint): string return Str::lower($endpoint->httpMethods[0]) . join('', array_map(fn ($part) => ucfirst($part), $parts)); } + /** + * Given an array, return an object if the array is empty. To be used with fields that are + * required by OpenAPI spec to be objects, since empty arrays get serialised as []. + */ + protected function objectIfEmpty(array $field): array | \stdClass + { + return count($field) > 0 ? $field : new \stdClass(); + } + /** * Given a value, generate the schema for it. The schema consists of: {type:, example:, properties: (if value is an object)}, * and possibly a description for each property. @@ -543,17 +543,17 @@ public function generateSchemaForValue(mixed $value, OutputEndpointData $endpoin { if ($value instanceof \stdClass) { $value = (array) $value; - $schema = [ - 'type' => 'object', - 'properties' => [], - ]; + $properties = []; // Recurse into the object foreach($value as $subField => $subValue){ $subFieldPath = sprintf('%s.%s', $path, $subField); - $schema['properties'][$subField] = $this->generateSchemaForValue($subValue, $endpoint, $subFieldPath); + $properties[$subField] = $this->generateSchemaForValue($subValue, $endpoint, $subFieldPath); } - return $schema; + return [ + 'type' => 'object', + 'properties' => $this->objectIfEmpty($properties), + ]; } $schema = [ From 9165ff6038df13e084bf1c9247ffd0064381ecca Mon Sep 17 00:00:00 2001 From: Shalvah Date: Sat, 19 Aug 2023 12:08:55 +0200 Subject: [PATCH 2/2] Update src/Writing/OpenAPISpecWriter.php --- src/Writing/OpenAPISpecWriter.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Writing/OpenAPISpecWriter.php b/src/Writing/OpenAPISpecWriter.php index b81201c4..4c9a6cd0 100644 --- a/src/Writing/OpenAPISpecWriter.php +++ b/src/Writing/OpenAPISpecWriter.php @@ -233,6 +233,7 @@ protected function generateEndpointRequestBodySpec(OutputEndpointData $endpoint) $schema['properties'][$name] = $fieldData; } + // We remove 'properties' if the request body is an array, so we need to check if it's still there if (array_key_exists('properties', $schema)) { $schema['properties'] = $this->objectIfEmpty($schema['properties']); }