From e6e014c929ca3654f8d59644fb63567e4baff3ad Mon Sep 17 00:00:00 2001 From: "tuna.yu" Date: Wed, 2 Oct 2024 03:18:04 +0800 Subject: [PATCH 1/2] refactor(OpenAPISpecWriter): extract description setting to a separate method --- src/Writing/OpenAPISpecWriter.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Writing/OpenAPISpecWriter.php b/src/Writing/OpenAPISpecWriter.php index fc89ace4..27bcde10 100644 --- a/src/Writing/OpenAPISpecWriter.php +++ b/src/Writing/OpenAPISpecWriter.php @@ -596,9 +596,7 @@ public function generateSchemaForValue(mixed $value, OutputEndpointData $endpoin 'type' => $this->convertScribeOrPHPTypeToOpenAPIType(gettype($value)), 'example' => $value, ]; - if (isset($endpoint->responseFields[$path]->description)) { - $schema['description'] = $endpoint->responseFields[$path]->description; - } + $this->setDescription($schema, $endpoint, $path); if ($schema['type'] === 'array' && !empty($value)) { $schema['example'] = json_decode(json_encode($schema['example']), true); // Convert stdClass to array @@ -616,4 +614,11 @@ public function generateSchemaForValue(mixed $value, OutputEndpointData $endpoin return $schema; } + + private function setDescription(array &$schema, OutputEndpointData $endpoint, string $path): void + { + if (isset($endpoint->responseFields[$path]->description)) { + $schema['description'] = $endpoint->responseFields[$path]->description; + } + } } From db0d315c918cdac3022ed4a5396d3edbabb2b2f8 Mon Sep 17 00:00:00 2001 From: "tuna.yu" Date: Wed, 2 Oct 2024 03:19:10 +0800 Subject: [PATCH 2/2] feat(OpenAPISpecWriter): add description to schema --- src/Writing/OpenAPISpecWriter.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Writing/OpenAPISpecWriter.php b/src/Writing/OpenAPISpecWriter.php index 27bcde10..11a6a84d 100644 --- a/src/Writing/OpenAPISpecWriter.php +++ b/src/Writing/OpenAPISpecWriter.php @@ -586,10 +586,13 @@ public function generateSchemaForValue(mixed $value, OutputEndpointData $endpoin $properties[$subField] = $this->generateSchemaForValue($subValue, $endpoint, $subFieldPath); } - return [ + $schema = [ 'type' => 'object', 'properties' => $this->objectIfEmpty($properties), ]; + $this->setDescription($schema, $endpoint, $path); + + return $schema; } $schema = [ @@ -615,6 +618,9 @@ public function generateSchemaForValue(mixed $value, OutputEndpointData $endpoin return $schema; } + /** + * Set the description for the schema. If the field has a description, it is set in the schema. + */ private function setDescription(array &$schema, OutputEndpointData $endpoint, string $path): void { if (isset($endpoint->responseFields[$path]->description)) {