Skip to content

Commit

Permalink
Implemented simpler resource attributes descriptions (#463)
Browse files Browse the repository at this point in the history
* added comments description

* removed unused import
  • Loading branch information
romalytvynenko authored Jul 19, 2024
1 parent 5b3afc1 commit 60596e9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Support/Generator/TypeTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ public function transform(Type $type)
? $this->transform(PhpDocTypeHelper::toType($varNode->type))
: $openApiType;

if ($varNode && $varNode->description) {
$openApiType->setDescription($varNode->description);
$commentDescription = trim($docNode->getAttribute('summary').' '.$docNode->getAttribute('description'));
$varNodeDescription = $varNode && $varNode->description ? trim($varNode->description) : '';
if ($commentDescription || $varNodeDescription) {
$openApiType->setDescription(implode('. ', array_filter([$varNodeDescription, $commentDescription])));
}

if ($examples = ExamplesExtractor::make($docNode)->extract(preferString: $openApiType instanceof StringType)) {
Expand Down
34 changes: 34 additions & 0 deletions tests/TypeToSchemaTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,25 @@
]);
});

it('supports simple comments descriptions in api resource', function () {
$transformer = new TypeTransformer($infer = app(Infer::class), $components = new Components, [JsonResourceTypeToSchema::class]);

$type = new ObjectType(ApiResourceTest_ResourceWithSimpleDescription::class);

expect($transformer->transform($type)->toArray())->toBe([
'$ref' => '#/components/schemas/ApiResourceTest_ResourceWithSimpleDescription',
]);

expect($components->getSchema(ApiResourceTest_ResourceWithSimpleDescription::class)->toArray()['properties']['now'])->toBe([
'type' => 'string',
'description' => 'The date of the current moment.',
]);
expect($components->getSchema(ApiResourceTest_ResourceWithSimpleDescription::class)->toArray()['properties']['now2'])->toBe([
'type' => 'string',
'description' => 'Inline comments are also supported.',
]);
});

class ComplexTypeHandlersTest_SampleType extends JsonResource
{
public function toArray($request)
Expand Down Expand Up @@ -335,6 +354,21 @@ public function toArray($request)
}
}

class ApiResourceTest_ResourceWithSimpleDescription extends JsonResource
{
public function toArray($request)
{
return [
/**
* The date of the current moment.
*/
'now' => now(),
// Inline comments are also supported.
'now2' => now(),
];
}
}

enum StatusTwo: string
{
case DRAFT = 'draft';
Expand Down

0 comments on commit 60596e9

Please sign in to comment.