forked from zircote/swagger-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeResolverTest.php
More file actions
174 lines (160 loc) · 15.3 KB
/
TypeResolverTest.php
File metadata and controls
174 lines (160 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php declare(strict_types=1);
/**
* @license Apache 2.0
*/
namespace OpenApi\Tests\Type;
use OpenApi\Analysis;
use OpenApi\Annotations as OA;
use OpenApi\Context;
use OpenApi\Generator;
use OpenApi\Pipeline;
use OpenApi\Processors\AugmentSchemas;
use OpenApi\Processors\MergeIntoOpenApi;
use OpenApi\Tests\Fixtures\PHP\DocblockAndTypehintTypes;
use OpenApi\Tests\OpenApiTestCase;
use OpenApi\TypeResolverInterface;
use PHPUnit\Framework\Attributes\DataProvider;
final class TypeResolverTest extends OpenApiTestCase
{
public static function resolverAugmentCases(): iterable
{
$expectations = [
OA\OpenApi::VERSION_3_0_0 => [
'nothing' => '{ "property": "nothing" }',
'string' => '{ "type": "string", "property": "string" }',
'nullablestring' => '{ "type": "string", "nullable": true, "property": "nullableString" }',
'nullablestringexplicit' => '{ "type": "string", "nullable": false, "property": "nullableStringExplicit" }',
'nullablestringdocblock' => '{ "type": "string", "nullable": true, "property": "nullableStringDocblock" }',
'nullablestringnative' => '{ "type": "string", "nullable": true, "property": "nullableStringNative" }',
'stringarray' => '{ "type": "array", "items": { "type": "string" }, "property": "stringArray" }',
'stringlist' => '{ "type": "array", "items": { "type": "string" }, "property": "stringList" }',
'stringlistexplicit' => '{ "type": "array", "items": { "type": "string", "example": "foo" }, "property": "stringListExplicit" }',
'nullablestringlist' => '{ "type": "array", "items": { "type": "string" }, "nullable": true, "property": "nullableStringList" }',
'nullablestringlistunion' => '{ "type": "array", "items": { "type": "string" }, "nullable": true, "property": "nullableStringListUnion" }',
'class' => '{ "$ref": "#/components/schemas/DocblockAndTypehintTypes" }',
'nullableclass' => '{ "oneOf": [ { "$ref": "#/components/schemas/DocblockAndTypehintTypes" } ], "nullable": true, "property": "nullableClass" }',
'namespacedglobalclass' => '{ "type": "string", "format": "date-time", "property": "namespacedGlobalClass" }',
'nullablenamespacedglobalclass' => '{ "type": "string", "format": "date-time", "nullable": true, "property": "nullableNamespacedGlobalClass" }',
'alsonullablenamespacedglobalclass' => '{ "type": "string", "format": "date-time", "nullable": true, "property": "alsoNullableNamespacedGlobalClass" }',
'intrange' => '{ "type": "integer", "maximum": 10, "minimum": -9223372036854775808, "property": "intRange" }',
'positiveint' => '{ "type": "integer", "maximum": 9223372036854775807, "minimum": 1, "property": "positiveInt" }',
'nonzeroint' => '{ "type": "integer", "not": { "enum": [ 0 ] }, "property": "nonZeroInt" }',
'legacy:arrayshape' => '{ "type": "array", "items": { "type": "boolean" }, "property": "arrayShape" }',
'type-info:arrayshape' => '{ "type": "object", "additionalProperties": { "type": "boolean" }, "property": "arrayShape" }',
'promotedstring' => '{ "type": "string", "property": "promotedString" }',
'legacy:mixedunion' => '{ "example": "My value", "property": "mixedUnion" }',
'type-info:mixedunion' => '{ "example": "My value", "oneOf": [ { "type": "string" }, { "type": "array", "items": { "type": "mixed" } } ], "property": "mixedUnion" }',
'getstring' => '{ "type": "string", "property": "getString" }',
'paramdatetimelist' => '{ "type": "array", "items": { "type": "string", "format": "date-time" }, "property": "paramDateTimeList" }',
'paramstringlist' => '{ "type": "array", "items": { "type": "string" }, "property": "paramStringList" }',
'blah' => '{ "type": "string", "example": "My blah", "nullable": true, "property": "blah" }',
'blah_values' => '{ "type": "array", "items": { "type": "string", "example": "hello" }, "nullable": true, "property": "blah_values" }',
'oneofvar' => '{ "oneOf": [ { "type": "string" }, { "type": "bool" } ], "property": "oneOfVar" }',
'oneoflist' => '{ "type": "array", "items": { "oneOf": [ { "type": "string" }, { "type": "bool" } ] }, "property": "oneOfList" }',
'legacy:nullabletypedlistunion' => '{ "nullable": true, "property": "nullableTypedListUnion" }',
'type-info:nullabletypedlistunion' => '{ "nullable": true, "oneOf": [ { "$ref": "#/components/schemas/DocblockAndTypehintTypes" }, { "type": "array", "items": { "$ref": "#/components/schemas/DocblockAndTypehintTypes" } } ], "property": "nullableTypedListUnion" }',
'legacy:nullablenestedtypedlistunion' => '{ "nullable": true, "property": "nullableNestedTypedListUnion" }',
'type-info:nullablenestedtypedlistunion' => '{ "nullable": true, "oneOf": [ { "$ref": "#/components/schemas/DocblockAndTypehintTypes" }, { "type": "array", "items": { "$ref": "#/components/schemas/DocblockAndTypehintTypes" } }, { "type": "array", "items": { "type": "object", "additionalProperties": { "$ref": "#/components/schemas/DocblockAndTypehintTypes" } } } ], "property": "nullableNestedTypedListUnion" }',
'reflectionvalue' => '{ "example": true, "nullable": true, "property": "reflectionValue" }',
'legacy:intersectionvar' => '{ "property": "intersectionVar" }',
'type-info:intersectionvar' => '{ "allOf": [ { "$ref": "#/components/schemas/FirstInterface" }, { "$ref": "#/components/schemas/SecondInterface" } ], "property": "intersectionVar" }',
'legacy:nestedoneof' => '{ "property": "nestedOneOf" }',
'type-info:nestedoneof' => '{ "oneOf": [ { "type": "array", "items": { "$ref": "#/components/schemas/DocblockAndTypehintTypes" } }, { "type": "array", "items": { "type": "string" } } ], "property": "nestedOneOf" }',
'legacy:nestedoneofwithitems' => '{ "type": "array", "items": { "oneOf": [ { "$ref": "#/components/schemas/DocblockAndTypehintTypes" }, { "type": "string" } ] }, "property": "nestedOneOfWithItems" }',
'type-info:nestedoneofwithitems' => '{ "type": "array", "items": { "oneOf": [ { "$ref": "#/components/schemas/DocblockAndTypehintTypes" }, { "type": "string" } ] }, "property": "nestedOneOfWithItems" }',
'type-info:stringmap' => '{ "type": "object", "additionalProperties": { "type": "string" }, "property": "stringMap" }',
'type-info:intkeyedmap' => '{ "type": "object", "additionalProperties": { "type": "string" }, "property": "intKeyedMap" }',
],
OA\OpenApi::VERSION_3_1_0 => [
'nothing' => '{ "property": "nothing" }',
'string' => '{ "type": "string", "property": "string" }',
'nullablestring' => '{ "type": [ "string", "null" ], "property": "nullableString" }',
'nullablestringexplicit' => '{ "type": "string", "property": "nullableStringExplicit" }',
'nullablestringdocblock' => '{ "type": [ "string", "null" ], "property": "nullableStringDocblock" }',
'nullablestringnative' => '{ "type": [ "string", "null" ], "property": "nullableStringNative" }',
'stringarray' => '{ "type": "array", "items": { "type": "string" }, "property": "stringArray" }',
'stringlist' => '{ "type": "array", "items": { "type": "string" }, "property": "stringList" }',
'stringlistexplicit' => '{ "type": "array", "items": { "type": "string", "example": "foo" }, "property": "stringListExplicit" }',
'nullablestringlist' => '{ "type": [ "array", "null" ], "items": { "type": "string" }, "property": "nullableStringList" }',
'nullablestringlistunion' => '{ "type": [ "array", "null" ], "items": { "type": "string" }, "property": "nullableStringListUnion" }',
'class' => '{ "$ref": "#/components/schemas/DocblockAndTypehintTypes" }',
'nullableclass' => '{ "oneOf": [ { "$ref": "#/components/schemas/DocblockAndTypehintTypes" }, { "type": "null" } ], "property": "nullableClass" }',
'namespacedglobalclass' => '{ "type": "string", "format": "date-time", "property": "namespacedGlobalClass" }',
'nullablenamespacedglobalclass' => '{ "type": [ "string", "null" ], "format": "date-time", "property": "nullableNamespacedGlobalClass" }',
'alsonullablenamespacedglobalclass' => '{ "type": [ "string", "null" ], "format": "date-time", "property": "alsoNullableNamespacedGlobalClass" }',
'intrange' => '{ "type": "integer", "maximum": 10, "minimum": -9223372036854775808, "property": "intRange" }',
'positiveint' => '{ "type": "integer", "maximum": 9223372036854775807, "minimum": 1, "property": "positiveInt" }',
'nonzeroint' => '{ "type": "integer", "not": { "const": 0 }, "property": "nonZeroInt" } ',
'legacy:arrayshape' => '{ "type": "array", "items": { "type": "boolean" }, "property": "arrayShape" }',
'type-info:arrayshape' => '{ "type": "object", "additionalProperties": { "type": "boolean" }, "property": "arrayShape" }',
'type-info:uniontype' => '{ "type": [ "integer", "string" ], "property": "unionType" }',
'promotedstring' => '{ "type": "string", "property": "promotedString" }',
'legacy:mixedunion' => '{ "example": "My value", "property": "mixedUnion" }',
'type-info:mixedunion' => '{ "example": "My value", "oneOf": [ { "type": [ "string" ] }, { "type": "array", "items": { "type": "mixed" } } ], "property": "mixedUnion" }',
'getstring' => '{ "type": "string", "property": "getString" }',
'paramdatetimelist' => '{ "type": "array", "items": { "type": "string", "format": "date-time" }, "property": "paramDateTimeList" }',
'paramstringlist' => '{ "type": "array", "items": { "type": "string" }, "property": "paramStringList" }',
'blah' => '{ "type": [ "string", "null" ], "example": "My blah", "property": "blah" }',
'blah_values' => '{ "type": [ "array", "null" ], "items": { "type": "string", "example": "hello" }, "property": "blah_values" }',
'oneofvar' => '{ "oneOf": [ { "type": "string" }, { "type": "bool" } ], "property": "oneOfVar" }',
'oneoflist' => '{ "type": "array", "items": { "oneOf": [ { "type": "string" }, { "type": "bool" } ] }, "property": "oneOfList" }',
'legacy:nullabletypedlistunion' => '{ "property": "nullableTypedListUnion" }',
'type-info:nullabletypedlistunion' => '{ "oneOf": [ { "$ref": "#/components/schemas/DocblockAndTypehintTypes" }, { "type": "array", "items": { "$ref": "#/components/schemas/DocblockAndTypehintTypes" } }, { "type": "null" } ], "property": "nullableTypedListUnion" }',
'legacy:nullablenestedtypedlistunion' => '{ "property": "nullableNestedTypedListUnion" }',
'type-info:nullablenestedtypedlistunion' => '{ "oneOf": [ { "$ref": "#/components/schemas/DocblockAndTypehintTypes" }, { "type": "array", "items": { "$ref": "#/components/schemas/DocblockAndTypehintTypes" } }, { "type": "array", "items": { "type": "object", "additionalProperties": { "$ref": "#/components/schemas/DocblockAndTypehintTypes" } } }, { "type": "null" } ], "property": "nullableNestedTypedListUnion" }',
'legacy:reflectionvalue' => '{ "example": true, "property": "reflectionValue" }',
'type-info:reflectionvalue' => '{ "type": [ "boolean", "integer", "null" ], "example": true, "property": "reflectionValue" }',
'legacy:intersectionvar' => '{ "property": "intersectionVar" }',
'type-info:intersectionvar' => '{ "allOf": [ { "$ref": "#/components/schemas/FirstInterface" }, { "$ref": "#/components/schemas/SecondInterface" } ], "property": "intersectionVar" }',
'legacy:nestedoneof' => '{ "property": "nestedOneOf" }',
'type-info:nestedoneof' => '{ "oneOf": [ { "type": "array", "items": { "$ref": "#/components/schemas/DocblockAndTypehintTypes" } }, { "type": "array", "items": { "type": "string" } } ], "property": "nestedOneOf" }',
'legacy:nestedoneofwithitems' => '{ "type": "array", "items": { "oneOf": [ { "$ref": "#/components/schemas/DocblockAndTypehintTypes" }, { "type": "string" } ] }, "property": "nestedOneOfWithItems" }',
'type-info:nestedoneofwithitems' => '{ "type": "array", "items": { "oneOf": [ { "$ref": "#/components/schemas/DocblockAndTypehintTypes" }, { "type": "string" } ] }, "property": "nestedOneOfWithItems" }',
'type-info:stringmap' => '{ "type": "object", "additionalProperties": { "type": "string" }, "property": "stringMap" }',
'type-info:intkeyedmap' => '{ "type": "object", "additionalProperties": { "type": "string" }, "property": "intKeyedMap" }',
],
];
$rc = new \ReflectionClass(DocblockAndTypehintTypes::class);
$fixtureFolder = dirname($rc->getFileName());
$sources = [
$rc->getFileName(),
"{$fixtureFolder}/FirstInterface.php",
"{$fixtureFolder}/SecondInterface.php",
];
foreach (self::getTypeResolvers() as $key => $typeResolver) {
foreach ([OA\OpenApi::VERSION_3_0_0, OA\OpenApi::VERSION_3_1_0] as $version) {
$analysis = (new Generator())
->setVersion($version)
->setProcessorPipeline(new Pipeline([new MergeIntoOpenApi(), new AugmentSchemas()]))
->withContext(function (Generator $generator, Analysis $analysis, Context $context) use ($sources): Analysis {
$generator->generate($sources, $analysis, false);
return $analysis;
});
$schema = $analysis->getAnnotationForSource(DocblockAndTypehintTypes::class);
foreach ($schema->properties as $ii => $property) {
$property->property = $property->_context->property
// promoted properties might not have a name!
?? $property->_context->method;
$caseName = strtolower((string) $property->property);
$resolverCaseName = "{$key}:{$caseName}";
$fullCase = "{$key}:{$version}[{$ii}]-{$caseName}";
$json = $expectations[$version][$caseName] ?? $expectations[$version][$resolverCaseName] ?? null;
if ($json) {
yield $fullCase => [
$typeResolver,
$analysis,
$property,
json_decode($json, true),
];
}
}
}
}
}
#[DataProvider('resolverAugmentCases')]
public function testAugmentSchemaType(TypeResolverInterface $typeResolver, Analysis $analysis, OA\Schema $schema, array $expected): void
{
$typeResolver->augmentSchemaType($analysis, $schema);
$this->assertSpecEquals($schema->toJson(), $expected, $schema->toJson());
}
}