-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenApiSchemaConverterTest.php
More file actions
367 lines (310 loc) · 11.4 KB
/
OpenApiSchemaConverterTest.php
File metadata and controls
367 lines (310 loc) · 11.4 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
declare(strict_types=1);
namespace Studio\OpenApiContractTesting\Tests\Unit;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Studio\OpenApiContractTesting\OpenApiSchemaConverter;
use Studio\OpenApiContractTesting\OpenApiVersion;
class OpenApiSchemaConverterTest extends TestCase
{
// ========================================
// OAS 3.0 tests
// ========================================
#[Test]
public function v30_nullable_type_converted_to_type_array(): void
{
$schema = [
'type' => 'string',
'nullable' => true,
];
$result = OpenApiSchemaConverter::convert($schema, OpenApiVersion::V3_0);
$this->assertSame(['string', 'null'], $result['type']);
$this->assertArrayNotHasKey('nullable', $result);
}
#[Test]
public function v30_nullable_one_of_adds_null_type(): void
{
$schema = [
'nullable' => true,
'oneOf' => [
['type' => 'string'],
['type' => 'integer'],
],
];
$result = OpenApiSchemaConverter::convert($schema, OpenApiVersion::V3_0);
$this->assertCount(3, $result['oneOf']);
$this->assertSame(['type' => 'null'], $result['oneOf'][2]);
$this->assertArrayNotHasKey('nullable', $result);
}
#[Test]
public function v30_nullable_all_of_wrapped_in_one_of(): void
{
$schema = [
'nullable' => true,
'allOf' => [
['type' => 'object', 'properties' => ['id' => ['type' => 'string']]],
],
];
$result = OpenApiSchemaConverter::convert($schema, OpenApiVersion::V3_0);
$this->assertArrayNotHasKey('allOf', $result);
$this->assertArrayHasKey('oneOf', $result);
$this->assertCount(2, $result['oneOf']);
$this->assertArrayHasKey('allOf', $result['oneOf'][0]);
$this->assertSame(['type' => 'null'], $result['oneOf'][1]);
$this->assertArrayNotHasKey('nullable', $result);
}
#[Test]
public function v30_nested_properties_converted_recursively(): void
{
$schema = [
'type' => 'object',
'properties' => [
'name' => [
'type' => 'string',
'nullable' => true,
],
'address' => [
'type' => 'object',
'nullable' => true,
'properties' => [
'city' => [
'type' => 'string',
'nullable' => true,
],
],
],
],
];
$result = OpenApiSchemaConverter::convert($schema, OpenApiVersion::V3_0);
$this->assertSame(['string', 'null'], $result['properties']['name']['type']);
$this->assertSame(['object', 'null'], $result['properties']['address']['type']);
$this->assertSame(['string', 'null'], $result['properties']['address']['properties']['city']['type']);
}
#[Test]
public function v30_items_nullable_converted(): void
{
$schema = [
'type' => 'array',
'items' => [
'type' => 'string',
'nullable' => true,
],
];
$result = OpenApiSchemaConverter::convert($schema, OpenApiVersion::V3_0);
$this->assertSame(['string', 'null'], $result['items']['type']);
}
#[Test]
public function v30_openapi_only_keys_removed(): void
{
$schema = [
'type' => 'string',
'description' => 'a name',
'example' => 'John',
'deprecated' => true,
'readOnly' => true,
'writeOnly' => false,
'xml' => ['name' => 'test'],
'externalDocs' => ['url' => 'https://example.com'],
'discriminator' => ['propertyName' => 'type'],
];
$result = OpenApiSchemaConverter::convert($schema, OpenApiVersion::V3_0);
$this->assertSame('string', $result['type']);
$this->assertSame('a name', $result['description']);
$this->assertArrayNotHasKey('example', $result);
$this->assertArrayNotHasKey('deprecated', $result);
$this->assertArrayNotHasKey('readOnly', $result);
$this->assertArrayNotHasKey('writeOnly', $result);
$this->assertArrayNotHasKey('xml', $result);
$this->assertArrayNotHasKey('externalDocs', $result);
$this->assertArrayNotHasKey('discriminator', $result);
}
#[Test]
public function v30_nullable_false_not_converted(): void
{
$schema = [
'type' => 'string',
'nullable' => false,
];
$result = OpenApiSchemaConverter::convert($schema, OpenApiVersion::V3_0);
$this->assertSame('string', $result['type']);
$this->assertArrayNotHasKey('nullable', $result);
}
#[Test]
public function v30_default_version_when_omitted(): void
{
$schema = [
'type' => 'string',
'nullable' => true,
];
$result = OpenApiSchemaConverter::convert($schema);
$this->assertSame(['string', 'null'], $result['type']);
}
// ========================================
// OAS 3.1 tests
// ========================================
#[Test]
public function v31_type_array_preserved(): void
{
$schema = [
'type' => ['string', 'null'],
];
$result = OpenApiSchemaConverter::convert($schema, OpenApiVersion::V3_1);
$this->assertSame(['string', 'null'], $result['type']);
}
#[Test]
public function v31_prefix_items_converted_to_items(): void
{
$schema = [
'type' => 'array',
'prefixItems' => [
['type' => 'string'],
['type' => 'integer'],
],
];
$result = OpenApiSchemaConverter::convert($schema, OpenApiVersion::V3_1);
$this->assertArrayNotHasKey('prefixItems', $result);
$this->assertArrayHasKey('items', $result);
$this->assertCount(2, $result['items']);
$this->assertSame(['type' => 'string'], $result['items'][0]);
$this->assertSame(['type' => 'integer'], $result['items'][1]);
}
#[Test]
public function v31_draft_2020_12_keys_removed(): void
{
$schema = [
'type' => 'object',
'$dynamicRef' => '#meta',
'$dynamicAnchor' => 'meta',
'contentSchema' => ['type' => 'string'],
'examples' => [['key' => 'value']],
'properties' => [
'name' => ['type' => 'string'],
],
];
$result = OpenApiSchemaConverter::convert($schema, OpenApiVersion::V3_1);
$this->assertArrayNotHasKey('$dynamicRef', $result);
$this->assertArrayNotHasKey('$dynamicAnchor', $result);
$this->assertArrayNotHasKey('contentSchema', $result);
$this->assertArrayNotHasKey('examples', $result);
$this->assertSame('object', $result['type']);
$this->assertArrayHasKey('properties', $result);
}
#[Test]
public function v31_common_openapi_keys_removed(): void
{
$schema = [
'type' => 'string',
'description' => 'a name',
'example' => 'John',
'deprecated' => true,
'xml' => ['name' => 'test'],
'externalDocs' => ['url' => 'https://example.com'],
'discriminator' => ['propertyName' => 'type'],
];
$result = OpenApiSchemaConverter::convert($schema, OpenApiVersion::V3_1);
$this->assertSame('string', $result['type']);
$this->assertSame('a name', $result['description']);
$this->assertArrayNotHasKey('example', $result);
$this->assertArrayNotHasKey('deprecated', $result);
$this->assertArrayNotHasKey('xml', $result);
$this->assertArrayNotHasKey('externalDocs', $result);
$this->assertArrayNotHasKey('discriminator', $result);
}
#[Test]
public function v31_nullable_keyword_not_processed(): void
{
// OAS 3.1 doesn't have "nullable" — type arrays are used instead.
// If somehow present, it should NOT be converted to type array.
$schema = [
'type' => 'string',
'nullable' => true,
];
$result = OpenApiSchemaConverter::convert($schema, OpenApiVersion::V3_1);
// nullable is not in the 3.1 removal list, so it stays
// but handleNullable is NOT called for 3.1
$this->assertSame('string', $result['type']);
}
#[Test]
public function v31_nested_prefix_items_converted_recursively(): void
{
$schema = [
'type' => 'object',
'properties' => [
'coordinates' => [
'type' => 'array',
'prefixItems' => [
['type' => 'number'],
['type' => 'number'],
],
],
],
];
$result = OpenApiSchemaConverter::convert($schema, OpenApiVersion::V3_1);
$this->assertArrayNotHasKey('prefixItems', $result['properties']['coordinates']);
$this->assertArrayHasKey('items', $result['properties']['coordinates']);
$this->assertCount(2, $result['properties']['coordinates']['items']);
}
#[Test]
public function v31_read_only_write_only_preserved(): void
{
// In 3.1, readOnly/writeOnly are valid JSON Schema keywords (Draft 07 supports them)
$schema = [
'type' => 'string',
'readOnly' => true,
'writeOnly' => false,
];
$result = OpenApiSchemaConverter::convert($schema, OpenApiVersion::V3_1);
// These are NOT removed in 3.1 mode (only removed in 3.0 mode)
$this->assertTrue($result['readOnly']);
$this->assertFalse($result['writeOnly']);
}
// ========================================
// Input immutability tests
// ========================================
#[Test]
public function convert_does_not_mutate_input_schema(): void
{
$schema = [
'type' => 'object',
'nullable' => true,
'example' => 'test',
'properties' => [
'name' => [
'type' => 'string',
'nullable' => true,
'example' => 'John',
],
'tags' => [
'type' => 'array',
'items' => [
'type' => 'string',
'deprecated' => true,
],
],
],
];
$original = $schema;
OpenApiSchemaConverter::convert($schema, OpenApiVersion::V3_0);
$this->assertSame($original, $schema);
}
#[Test]
public function convert_does_not_mutate_input_schema_v31(): void
{
$schema = [
'type' => 'object',
'prefixItems' => [
['type' => 'string'],
],
'examples' => [['key' => 'value']],
'properties' => [
'data' => [
'type' => 'object',
'$dynamicRef' => '#meta',
],
],
];
$original = $schema;
OpenApiSchemaConverter::convert($schema, OpenApiVersion::V3_1);
$this->assertSame($original, $schema);
}
}