Skip to content

Commit b759d54

Browse files
committed
feat(OpenApiType): Support comments in object array types
Signed-off-by: provokateurin <[email protected]>
1 parent 40acce7 commit b759d54

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

generate-spec.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
$astParser = (new ParserFactory())->createForNewestSupportedVersion();
7171
$nodeFinder = new NodeFinder;
7272

73-
$config = new ParserConfig(usedAttributes: ['lines' => true, 'indexes' => true]);
73+
$config = new ParserConfig(usedAttributes: ['lines' => true, 'indexes' => true, 'comments' => true]);
7474
$lexer = new Lexer($config);
7575
$constExprParser = new ConstExprParser($config);
7676
$typeParser = new TypeParser($config, $constExprParser);

src/OpenApiType.php

+8
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@
1313
use PhpParser\Node\NullableType;
1414
use PhpParser\Node\UnionType;
1515
use PhpParser\NodeAbstract;
16+
use PHPStan\PhpDocParser\Ast\Attribute;
17+
use PHPStan\PhpDocParser\Ast\Comment;
1618
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
1719
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
1820
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
21+
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode;
1922
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
2023
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
2124
use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode;
2225
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
2326
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
2427
use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode;
2528
use PHPStan\PhpDocParser\Ast\Type\NullableTypeNode;
29+
use PHPStan\PhpDocParser\Ast\Type\ObjectShapeNode;
2630
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
2731
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
2832
use stdClass;
@@ -207,6 +211,10 @@ public static function resolve(string $context, array $definitions, ParamTagValu
207211
foreach ($node->items as $item) {
208212
$name = $item->keyName instanceof ConstExprStringNode ? $item->keyName->value : $item->keyName->name;
209213
$type = self::resolve($context . ': ' . $name, $definitions, $item->valueType);
214+
$comments = array_map(static fn (Comment $comment) => preg_replace('/^\/\/\s*/', '', $comment->text), $item->keyName->getAttribute(Attribute::COMMENTS) ?? []);
215+
if ($comments !== []) {
216+
$type->description = implode("\n", $comments);
217+
}
210218
$properties[$name] = $type;
211219
if (!$item->optional) {
212220
$required[] = $name;

tests/lib/ResponseDefinitions.php

+5
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@
5656
* }
5757
*
5858
* @psalm-type NotificationsRequestProperty = array{
59+
* // A comment.
5960
* publicKey: string,
61+
* // A comment with a link: https://example.com.
6062
* signature: string,
63+
* // A comment.
64+
* // Another comment.
65+
* multipleComments: string,
6166
* }
6267
*/
6368
class ResponseDefinitions {

tests/openapi-full.json

+10-3
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,21 @@
255255
"type": "object",
256256
"required": [
257257
"publicKey",
258-
"signature"
258+
"signature",
259+
"multipleComments"
259260
],
260261
"properties": {
261262
"publicKey": {
262-
"type": "string"
263+
"type": "string",
264+
"description": "A comment."
263265
},
264266
"signature": {
265-
"type": "string"
267+
"type": "string",
268+
"description": "A comment with a link: https://example.com."
269+
},
270+
"multipleComments": {
271+
"type": "string",
272+
"description": "A comment. Another comment."
266273
}
267274
}
268275
}

tests/openapi.json

+10-3
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,21 @@
222222
"type": "object",
223223
"required": [
224224
"publicKey",
225-
"signature"
225+
"signature",
226+
"multipleComments"
226227
],
227228
"properties": {
228229
"publicKey": {
229-
"type": "string"
230+
"type": "string",
231+
"description": "A comment."
230232
},
231233
"signature": {
232-
"type": "string"
234+
"type": "string",
235+
"description": "A comment with a link: https://example.com."
236+
},
237+
"multipleComments": {
238+
"type": "string",
239+
"description": "A comment. Another comment."
233240
}
234241
}
235242
}

0 commit comments

Comments
 (0)