Skip to content

Commit

Permalink
feat: Add description to type as object when generating swagger file (#…
Browse files Browse the repository at this point in the history
…896)

* refactor(OpenAPISpecWriter): extract description setting to a separate method

* feat(OpenAPISpecWriter): add description to schema

---------

Co-authored-by: tuna.yu <[email protected]>
  • Loading branch information
TunaKHH and tuna.yu authored Oct 2, 2024
1 parent 8a6c644 commit 8274a80
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/Writing/OpenAPISpecWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
}

0 comments on commit 8274a80

Please sign in to comment.