Skip to content

Commit

Permalink
feat(OpenApiType): Support comments in object array types
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <kate@provokateurin.de>
  • Loading branch information
provokateurin committed Mar 5, 2025
1 parent 40acce7 commit bbfa4cb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion generate-spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
$astParser = (new ParserFactory())->createForNewestSupportedVersion();
$nodeFinder = new NodeFinder;

$config = new ParserConfig(usedAttributes: ['lines' => true, 'indexes' => true]);
$config = new ParserConfig(usedAttributes: ['lines' => true, 'indexes' => true, 'comments' => true]);
$lexer = new Lexer($config);
$constExprParser = new ConstExprParser($config);
$typeParser = new TypeParser($config, $constExprParser);
Expand Down
6 changes: 6 additions & 0 deletions src/OpenApiType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use PhpParser\Node\NullableType;
use PhpParser\Node\UnionType;
use PhpParser\NodeAbstract;
use PHPStan\PhpDocParser\Ast\Attribute;
use PHPStan\PhpDocParser\Ast\Comment;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
Expand Down Expand Up @@ -207,6 +209,10 @@ public static function resolve(string $context, array $definitions, ParamTagValu
foreach ($node->items as $item) {
$name = $item->keyName instanceof ConstExprStringNode ? $item->keyName->value : $item->keyName->name;
$type = self::resolve($context . ': ' . $name, $definitions, $item->valueType);
$comments = array_map(static fn (Comment $comment) => preg_replace('/^\/\/\s*/', '', $comment->text), $item->keyName->getAttribute(Attribute::COMMENTS) ?? []);
if ($comments !== []) {
$type->description = implode("\n", $comments);
}
$properties[$name] = $type;
if (!$item->optional) {
$required[] = $name;
Expand Down
5 changes: 5 additions & 0 deletions tests/lib/ResponseDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@
* }
*
* @psalm-type NotificationsRequestProperty = array{
* // A comment.
* publicKey: string,
* // A comment with a link: https://example.com.
* signature: string,
* // A comment.
* // Another comment.
* multipleComments: string,
* }
*/
class ResponseDefinitions {
Expand Down
13 changes: 10 additions & 3 deletions tests/openapi-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,21 @@
"type": "object",
"required": [
"publicKey",
"signature"
"signature",
"multipleComments"
],
"properties": {
"publicKey": {
"type": "string"
"type": "string",
"description": "A comment."
},
"signature": {
"type": "string"
"type": "string",
"description": "A comment with a link: https://example.com."
},
"multipleComments": {
"type": "string",
"description": "A comment. Another comment."
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions tests/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,21 @@
"type": "object",
"required": [
"publicKey",
"signature"
"signature",
"multipleComments"
],
"properties": {
"publicKey": {
"type": "string"
"type": "string",
"description": "A comment."
},
"signature": {
"type": "string"
"type": "string",
"description": "A comment with a link: https://example.com."
},
"multipleComments": {
"type": "string",
"description": "A comment. Another comment."
}
}
}
Expand Down

0 comments on commit bbfa4cb

Please sign in to comment.