Skip to content

Commit 0e20d17

Browse files
authored
Skip type resolving on Schema with items set (#1916) the
1 parent cf33295 commit 0e20d17

8 files changed

Lines changed: 89 additions & 0 deletions

src/Type/LegacyTypeResolver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ protected function doAugment(Analysis $analysis, OA\Schema $schema, \Reflector $
5757

5858
$this->type2ref($schema, $analysis);
5959

60+
if ($schema->items instanceof OA\Items) {
61+
$schema->type = 'array';
62+
}
63+
6064
if (!Generator::isDefault($schema->const) && Generator::isDefault($schema->type)) {
6165
if (!$this->mapNativeType($schema, gettype($schema->const))) {
6266
$schema->type = Generator::UNDEFINED;

src/Type/TypeInfoTypeResolver.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ protected function doAugment(Analysis $analysis, OA\Schema $schema, \Reflector $
5959

6060
$this->type2ref($schema, $analysis, $sourceClass);
6161

62+
if ($schema->items instanceof OA\Items) {
63+
$schema->type = 'array';
64+
}
65+
6266
if (!Generator::isDefault($schema->const) && Generator::isDefault($schema->type)) {
6367
if (!$this->mapNativeType($schema, gettype($schema->const))) {
6468
$schema->type = Generator::UNDEFINED;
@@ -93,6 +97,11 @@ protected function setSchemaType(OA\Schema $schema, Type $type, Analysis $analys
9397
$builtinTypes = array_filter($types, static fn (Type $t): bool => $t instanceof BuiltinType);
9498
$otherTypes = array_filter($types, static fn (Type $t): bool => !$t instanceof BuiltinType);
9599

100+
if ($schema->items instanceof OA\Items) {
101+
// nothing more we can do here
102+
return $schema;
103+
}
104+
96105
$schema->type = Generator::UNDEFINED;
97106
$schema->oneOf = [];
98107

tests/Fixtures/PHP/DocblockAndTypehintTypes.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,19 @@ public function blah(
219219

220220
#[OAT\Property]
221221
public FirstInterface&SecondInterface $intersectionVar;
222+
223+
/**
224+
* @var array<DocblockAndTypehintTypes>|array<string>
225+
*/
226+
#[OAT\Property]
227+
public array $nestedOneOf;
228+
229+
/**
230+
* @var array<DocblockAndTypehintTypes>|array<string>
231+
*/
232+
#[OAT\Property(items: new OAT\Items(oneOf: [
233+
new OAT\Schema(type: DocblockAndTypehintTypes::class),
234+
new OAT\Schema(type: 'string'),
235+
]))]
236+
public array $nestedOneOfWithItems;
222237
}

tests/Fixtures/Scratch/NestedSchema.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ class NestedSchemaTwo
1818
{
1919
}
2020

21+
#[OAT\Schema]
22+
class MultipleOneOf
23+
{
24+
/**
25+
* @param array<MultipleOneOf>|array<NestedSchemaTwo> $values
26+
*/
27+
public function __construct(
28+
#[OAT\Property(items: new OAT\Items(oneOf: [
29+
new OAT\Schema(type: MultipleOneOf::class, description: 'Recursive nested item'),
30+
new OAT\Schema(type: NestedSchemaTwo::class, description: 'Another item'),
31+
]))]
32+
public array $values
33+
) {
34+
}
35+
}
36+
2137
#[OAT\Info(
2238
title: 'Parameter Content Scratch',
2339
version: '1.0'

tests/Fixtures/Scratch/NestedSchema3.0.0.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ components:
2626
schemas:
2727
NestedSchemaOne: { }
2828
NestedSchemaTwo: { }
29+
MultipleOneOf:
30+
properties:
31+
values:
32+
type: array
33+
items:
34+
oneOf:
35+
-
36+
$ref: '#/components/schemas/MultipleOneOf'
37+
-
38+
$ref: '#/components/schemas/NestedSchemaTwo'
39+
type: object
2940
NestedSchema:
3041
required:
3142
- errors

tests/Fixtures/Scratch/NestedSchema3.1.0.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ components:
2626
schemas:
2727
NestedSchemaOne: { }
2828
NestedSchemaTwo: { }
29+
MultipleOneOf:
30+
properties:
31+
values:
32+
type: array
33+
items:
34+
oneOf:
35+
-
36+
$ref: '#/components/schemas/MultipleOneOf'
37+
description: 'Recursive nested item'
38+
-
39+
$ref: '#/components/schemas/NestedSchemaTwo'
40+
description: 'Another item'
41+
type: object
2942
NestedSchema:
3043
required:
3144
- errors

tests/Fixtures/Scratch/NestedSchema3.2.0.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ components:
2626
schemas:
2727
NestedSchemaOne: { }
2828
NestedSchemaTwo: { }
29+
MultipleOneOf:
30+
properties:
31+
values:
32+
type: array
33+
items:
34+
oneOf:
35+
-
36+
$ref: '#/components/schemas/MultipleOneOf'
37+
description: 'Recursive nested item'
38+
-
39+
$ref: '#/components/schemas/NestedSchemaTwo'
40+
description: 'Another item'
41+
type: object
2942
NestedSchema:
3043
required:
3144
- errors

tests/Type/TypeResolverTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public static function resolverAugmentCases(): iterable
6262
'reflectionvalue' => '{ "example": true, "nullable": true, "property": "reflectionValue" }',
6363
'legacy:intersectionvar' => '{ "property": "intersectionVar" }',
6464
'type-info:intersectionvar' => '{ "allOf": [ { "$ref": "#/components/schemas/FirstInterface" }, { "$ref": "#/components/schemas/SecondInterface" } ], "property": "intersectionVar" }',
65+
'legacy:nestedoneof' => '{ "property": "nestedOneOf" }',
66+
'type-info:nestedoneof' => '{ "oneOf": [ { "type": "array", "items": { "$ref": "#/components/schemas/DocblockAndTypehintTypes" } }, { "type": "array", "items": { "type": "string" } } ], "property": "nestedOneOf" }',
67+
'legacy:nestedoneofwithitems' => '{ "type": "array", "items": { "oneOf": [ { "$ref": "#/components/schemas/DocblockAndTypehintTypes" }, { "type": "string" } ] }, "property": "nestedOneOfWithItems" }',
68+
'type-info:nestedoneofwithitems' => '{ "type": "array", "items": { "oneOf": [ { "$ref": "#/components/schemas/DocblockAndTypehintTypes" }, { "type": "string" } ] }, "property": "nestedOneOfWithItems" }',
6569
],
6670
OA\OpenApi::VERSION_3_1_0 => [
6771
'nothing' => '{ "property": "nothing" }',
@@ -104,6 +108,10 @@ public static function resolverAugmentCases(): iterable
104108
'type-info:reflectionvalue' => '{ "type": [ "boolean", "integer", "null" ], "example": true, "property": "reflectionValue" }',
105109
'legacy:intersectionvar' => '{ "property": "intersectionVar" }',
106110
'type-info:intersectionvar' => '{ "allOf": [ { "$ref": "#/components/schemas/FirstInterface" }, { "$ref": "#/components/schemas/SecondInterface" } ], "property": "intersectionVar" }',
111+
'legacy:nestedoneof' => '{ "property": "nestedOneOf" }',
112+
'type-info:nestedoneof' => '{ "oneOf": [ { "type": "array", "items": { "$ref": "#/components/schemas/DocblockAndTypehintTypes" } }, { "type": "array", "items": { "type": "string" } } ], "property": "nestedOneOf" }',
113+
'legacy:nestedoneofwithitems' => '{ "type": "array", "items": { "oneOf": [ { "$ref": "#/components/schemas/DocblockAndTypehintTypes" }, { "type": "string" } ] }, "property": "nestedOneOfWithItems" }',
114+
'type-info:nestedoneofwithitems' => '{ "type": "array", "items": { "oneOf": [ { "$ref": "#/components/schemas/DocblockAndTypehintTypes" }, { "type": "string" } ] }, "property": "nestedOneOfWithItems" }',
107115
],
108116
];
109117

0 commit comments

Comments
 (0)