Skip to content

Commit 9cf6abe

Browse files
authored
Handle Mixed union type incl. array in LegacyTypeResolver (#1844)
1 parent 374262e commit 9cf6abe

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

src/Type/LegacyTypeResolver.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ protected function getDocblockTypeDetails(\Reflector $reflector, ?Context $conte
192192
}
193193

194194
$docComment = str_replace("\r\n", "\n", $docComment);
195+
$docComment = str_replace('list', 'array', $docComment);
195196
$docComment = preg_replace('/\*\/[ \t]*$/', '', $docComment); // strip '*/'
196197
preg_match($pattern, $docComment, $matches);
197198

@@ -201,6 +202,11 @@ protected function getDocblockTypeDetails(\Reflector $reflector, ?Context $conte
201202
$nullable = in_array('null', explode('|', strtolower($type))) || str_contains($type, '?');
202203
$isArray = str_contains($type, '[]') || str_contains($type, 'array');
203204
$type = str_replace(['|null', 'null|', '?', 'null', '[]'], '', $type);
205+
$isUnion = count(explode('|', $type)) > 1;
206+
if ($isUnion && $isArray) {
207+
$type = '';
208+
$isArray = false;
209+
}
204210

205211
// typed array
206212
$result = preg_match('/([^<]+)<([^>]+)>/', $type, $matches);

tests/Fixtures/Scratch/MultiTypeProperty.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ class MultiTypeProperty
1313
{
1414
#[OAT\Property(example: true)]
1515
public int|bool|null $value;
16+
17+
/**
18+
* @var string|list<string>
19+
*/
20+
#[OAT\Property(example: 'My value')]
21+
public string|array $mixedUnion;
22+
23+
/**
24+
* @param string|list<string> $otherValue
25+
*/
26+
public function __construct(
27+
#[OAT\Property(example: 'My value')]
28+
public string|array $otherValue,
29+
) {
30+
}
1631
}
1732

1833
#[OAT\Info(

tests/Fixtures/Scratch/MultiTypeProperty3.0.0.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ components:
1414
schemas:
1515
MultiTypeProperty:
1616
properties:
17+
otherValue:
18+
example: 'My value'
1719
value:
1820
example: true
1921
nullable: true
22+
mixedUnion:
23+
example: 'My value'
2024
type: object

tests/Fixtures/Scratch/MultiTypeProperty3.1.0.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ components:
1414
schemas:
1515
MultiTypeProperty:
1616
properties:
17+
otherValue:
18+
example: 'My value'
1719
value:
1820
example: true
21+
mixedUnion:
22+
example: 'My value'
1923
type: object

0 commit comments

Comments
 (0)