From 8274a80a338f41e78be01f37d4cef5a75f40d4ef Mon Sep 17 00:00:00 2001 From: Tuna Date: Thu, 3 Oct 2024 03:14:53 +0800 Subject: [PATCH] feat: Add description to type as object when generating swagger file (#896) * refactor(OpenAPISpecWriter): extract description setting to a separate method * feat(OpenAPISpecWriter): add description to schema --------- Co-authored-by: tuna.yu --- src/Writing/OpenAPISpecWriter.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Writing/OpenAPISpecWriter.php b/src/Writing/OpenAPISpecWriter.php index fc89ace4..11a6a84d 100644 --- a/src/Writing/OpenAPISpecWriter.php +++ b/src/Writing/OpenAPISpecWriter.php @@ -586,19 +586,20 @@ 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 = [ '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 +617,14 @@ 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)) { + $schema['description'] = $endpoint->responseFields[$path]->description; + } + } }