Skip to content

Commit e906ac2

Browse files
committed
fix(OpenApiType): Support class-string as array key type
Signed-off-by: provokateurin <[email protected]>
1 parent edecd1d commit e906ac2

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

src/OpenApiType.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,10 @@ public static function resolve(string $context, array $definitions, ParamTagValu
220220
);
221221
}
222222

223-
if ($node instanceof GenericTypeNode && $node->type->name === 'array' && count($node->genericTypes) === 2 && $node->genericTypes[0] instanceof IdentifierTypeNode) {
224-
$allowedTypes = ['string', 'lowercase-string', 'non-empty-string', 'non-empty-lowercase-string'];
225-
if (in_array($node->genericTypes[0]->name, $allowedTypes, true)) {
223+
if ($node instanceof GenericTypeNode && $node->type->name === 'array' && count($node->genericTypes) === 2 && ($node->genericTypes[0] instanceof IdentifierTypeNode || $node->genericTypes[0] instanceof GenericTypeNode)) {
224+
$key = $node->genericTypes[0] instanceof IdentifierTypeNode ? $node->genericTypes[0]->name : $node->genericTypes[0]->type->name;
225+
$allowedTypes = ['string', 'lowercase-string', 'non-empty-string', 'non-empty-lowercase-string', 'class-string'];
226+
if (in_array($key, $allowedTypes, true)) {
226227
return new OpenApiType(
227228
context: $context,
228229
type: 'object',

tests/lib/ResponseDefinitions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* type: string,
1919
* primary: bool,
2020
* class: class-string<AdminSettingsController>,
21+
* arrayClass: array<class-string<AdminSettingsController>, int>,
2122
* }
2223
*
2324
* @psalm-type NotificationsCollection = list<NotificationsItem>

tests/openapi-full.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
"link",
5454
"type",
5555
"primary",
56-
"class"
56+
"class",
57+
"arrayClass"
5758
],
5859
"properties": {
5960
"label": {
@@ -71,6 +72,13 @@
7172
"class": {
7273
"type": "string",
7374
"minLength": 1
75+
},
76+
"arrayClass": {
77+
"type": "object",
78+
"additionalProperties": {
79+
"type": "integer",
80+
"format": "int64"
81+
}
7482
}
7583
}
7684
},

tests/openapi.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
"link",
5454
"type",
5555
"primary",
56-
"class"
56+
"class",
57+
"arrayClass"
5758
],
5859
"properties": {
5960
"label": {
@@ -71,6 +72,13 @@
7172
"class": {
7273
"type": "string",
7374
"minLength": 1
75+
},
76+
"arrayClass": {
77+
"type": "object",
78+
"additionalProperties": {
79+
"type": "integer",
80+
"format": "int64"
81+
}
7482
}
7583
}
7684
},

0 commit comments

Comments
 (0)