-
Notifications
You must be signed in to change notification settings - Fork 92
/
SchemaTest.php
358 lines (323 loc) · 11.9 KB
/
SchemaTest.php
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
<?php
use cebe\openapi\Reader;
use cebe\openapi\ReferenceContext;
use cebe\openapi\spec\Discriminator;
use cebe\openapi\spec\Reference;
use cebe\openapi\spec\Schema;
use cebe\openapi\spec\Type;
/**
* @covers \cebe\openapi\spec\Schema
* @covers \cebe\openapi\spec\Discriminator
*/
class SchemaTest extends \PHPUnit\Framework\TestCase
{
public function testRead()
{
/** @var $schema Schema */
$schema = Reader::readFromJson(<<<JSON
{
"type": "string",
"format": "email"
}
JSON
, Schema::class);
$result = $schema->validate();
$this->assertEquals([], $schema->getErrors());
$this->assertTrue($result);
$this->assertEquals(Type::STRING, $schema->type);
$this->assertEquals('email', $schema->format);
// additionalProperties defaults to true.
$this->assertTrue($schema->additionalProperties);
// nullable Default value is false.
$this->assertFalse($schema->nullable);
// readOnly Default value is false.
$this->assertFalse($schema->readOnly);
// writeOnly Default value is false.
$this->assertFalse($schema->writeOnly);
// deprecated Default value is false.
$this->assertFalse($schema->deprecated);
}
public function testReadObject()
{
/** @var $schema Schema */
$schema = Reader::readFromJson(<<<'JSON'
{
"type": "object",
"required": [
"name"
],
"properties": {
"name": {
"type": "string"
},
"address": {
"$ref": "#/components/schemas/Address"
},
"age": {
"type": "integer",
"format": "int32",
"minimum": 0
}
}
}
JSON
, Schema::class);
$result = $schema->validate();
$this->assertEquals([], $schema->getErrors());
$this->assertTrue($result);
$this->assertEquals(Type::OBJECT, $schema->type);
$this->assertEquals(['name'], $schema->required);
$this->assertEquals(Type::INTEGER, $schema->properties['age']->type);
$this->assertEquals(0, $schema->properties['age']->minimum);
// additionalProperties defaults to true.
$this->assertTrue($schema->additionalProperties);
// nullable Default value is false.
$this->assertFalse($schema->nullable);
// readOnly Default value is false.
$this->assertFalse($schema->readOnly);
// writeOnly Default value is false.
$this->assertFalse($schema->writeOnly);
// deprecated Default value is false.
$this->assertFalse($schema->deprecated);
}
public function testDiscriminator()
{
/** @var $schema Schema */
$schema = Reader::readFromYaml(<<<'YAML'
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
- $ref: '#/components/schemas/Lizard'
discriminator:
map:
cat: Cat
dog: Dog
YAML
, Schema::class);
$result = $schema->validate();
$this->assertEquals([
'Discriminator is missing required property: propertyName'
], $schema->getErrors());
$this->assertFalse($result);
/** @var $schema Schema */
$schema = Reader::readFromYaml(<<<'YAML'
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
- $ref: '#/components/schemas/Lizard'
discriminator:
propertyName: type
mapping:
cat: Cat
monster: https://gigantic-server.com/schemas/Monster/schema.json
YAML
, Schema::class);
$result = $schema->validate();
$this->assertEquals([], $schema->getErrors());
$this->assertTrue($result);
$this->assertInstanceOf(Discriminator::class, $schema->discriminator);
$this->assertEquals('type', $schema->discriminator->propertyName);
$this->assertEquals([
'cat' => 'Cat',
'monster' => 'https://gigantic-server.com/schemas/Monster/schema.json',
], $schema->discriminator->mapping);
}
public function testCreateionFromObjects()
{
$schema = new Schema([
'allOf' => [
new Schema(['type' => 'integer']),
new Schema(['type' => 'string']),
],
'additionalProperties' => new Schema([
'type' => 'object',
]),
'discriminator' => new Discriminator([
'mapping' => ['A' => 'B'],
]),
]);
$this->assertSame('integer', $schema->allOf[0]->type);
$this->assertSame('string', $schema->allOf[1]->type);
$this->assertInstanceOf(Schema::class, $schema->additionalProperties);
$this->assertSame('object', $schema->additionalProperties->type);
$this->assertSame(['A' => 'B'], $schema->discriminator->mapping);
}
public function badSchemaProvider()
{
yield [['properties' => ['a' => 'foo']], 'Unable to instantiate cebe\openapi\spec\Schema Object with data \'foo\''];
yield [['properties' => ['a' => 42]], 'Unable to instantiate cebe\openapi\spec\Schema Object with data \'42\''];
yield [['properties' => ['a' => false]], 'Unable to instantiate cebe\openapi\spec\Schema Object with data \'\''];
yield [['properties' => ['a' => new stdClass()]], "Unable to instantiate cebe\openapi\spec\Schema Object with data 'stdClass Object\n(\n)\n'"];
yield [['additionalProperties' => 'foo'], 'Schema::$additionalProperties MUST be either boolean or a Schema/Reference object, "string" given'];
yield [['additionalProperties' => 42], 'Schema::$additionalProperties MUST be either boolean or a Schema/Reference object, "integer" given'];
yield [['additionalProperties' => new stdClass()], 'Schema::$additionalProperties MUST be either boolean or a Schema/Reference object, "stdClass" given'];
// The last one can be supported in future, but now SpecBaseObjects::__construct() requires array explicitly
}
/**
* @dataProvider badSchemaProvider
*/
public function testPathsCanNotBeCreatedFromBullshit($config, $expectedException)
{
$this->expectException(\cebe\openapi\exceptions\TypeErrorException::class);
$this->expectExceptionMessage($expectedException);
new Schema($config);
}
public function testAllOf()
{
$json = <<<'JSON'
{
"components": {
"schemas": {
"identifier": {
"type": "object",
"properties": {
"id": {"type": "string"}
}
},
"person": {
"allOf": [
{"$ref": "#/components/schemas/identifier"},
{
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
]
}
}
}
}
JSON;
$openApi = Reader::readFromJson($json);
$this->assertInstanceOf(Schema::class, $identifier = $openApi->components->schemas['identifier']);
$this->assertInstanceOf(Schema::class, $person = $openApi->components->schemas['person']);
$this->assertEquals('object', $identifier->type);
$this->assertTrue(is_array($person->allOf));
$this->assertCount(2, $person->allOf);
$this->assertInstanceOf(Reference::class, $person->allOf[0]);
$this->assertInstanceOf(Schema::class, $refResolved = $person->allOf[0]->resolve(new ReferenceContext($openApi, 'tmp://openapi.yaml')));
$this->assertInstanceOf(Schema::class, $person->allOf[1]);
$this->assertEquals('object', $refResolved->type);
$this->assertEquals('object', $person->allOf[1]->type);
$this->assertArrayHasKey('id', $refResolved->properties);
$this->assertArrayHasKey('name', $person->allOf[1]->properties);
}
/**
* Ensure Schema properties are accessable and have default values.
*/
public function testSchemaProperties()
{
$schema = new Schema([]);
$validProperties = [
// https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#schema-object
// The following properties are taken directly from the JSON Schema definition and follow the same specifications:
'title' => null,
'multipleOf' => null,
'maximum' => null,
'exclusiveMaximum' => false,
'minimum' => null,
'exclusiveMinimum' => false,
'maxLength' => null,
'minLength' => null,
'pattern' => null,
'maxItems' => null,
'minItems' => null,
'uniqueItems' => false,
'maxProperties' => null,
'minProperties' => null,
'required' => null, // if set, it should not be an empty array, according to the spec
'enum' => null, // if it is an array, it means restriction of values
// The following properties are taken from the JSON Schema definition but their definitions were adjusted to the OpenAPI Specification.
'type' => null,
'allOf' => null,
'oneOf' => null,
'anyOf' => null,
'not' => null,
'items' => null,
'properties' => [],
'additionalProperties' => true,
'description' => null,
'format' => null,
'default' => null,
// Other than the JSON Schema subset fields, the following fields MAY be used for further schema documentation:
'nullable' => false,
'readOnly' => false,
'writeOnly' => false,
'xml' => null,
'externalDocs' => null,
'example' => null,
'deprecated' => false,
];
foreach($validProperties as $property => $defaultValue) {
$this->assertEquals($defaultValue, $schema->$property, "testing property '$property'");
}
}
public function testRefAdditionalProperties()
{
$json = <<<'JSON'
{
"components": {
"schemas": {
"booleanProperties": {
"type": "boolean"
},
"person": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"additionalProperties": {"$ref": "#/components/schemas/booleanProperties"}
}
}
}
}
JSON;
$openApi = Reader::readFromJson($json);
$this->assertInstanceOf(Schema::class, $booleanProperties = $openApi->components->schemas['booleanProperties']);
$this->assertInstanceOf(Schema::class, $person = $openApi->components->schemas['person']);
$this->assertEquals('boolean', $booleanProperties->type);
$this->assertInstanceOf(Reference::class, $person->additionalProperties);
$this->assertInstanceOf(Schema::class, $refResolved = $person->additionalProperties->resolve(new ReferenceContext($openApi, 'tmp://openapi.yaml')));
$this->assertEquals('boolean', $refResolved->type);
$schema = new Schema(['additionalProperties' => new Reference(['$ref' => '#/here'], Schema::class)]);
$this->assertInstanceOf(Reference::class, $schema->additionalProperties);
}
/**
* Ensure that a property named "$ref" is not interpreted as a reference.
* @link https://github.com/OAI/OpenAPI-Specification/issues/2173
*/
public function testPropertyNameRef()
{
$json = <<<'JSON'
{
"components": {
"schemas": {
"person": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"$ref": {
"type": "string"
}
}
}
}
}
}
JSON;
$openApi = Reader::readFromJson($json);
$this->assertInstanceOf(Schema::class, $person = $openApi->components->schemas['person']);
$this->assertEquals(['name', '$ref'], array_keys($person->properties));
$this->assertInstanceOf(Schema::class, $person->properties['name']);
$this->assertInstanceOf(Schema::class, $person->properties['$ref']);
$this->assertEquals('string', $person->properties['name']->type);
$this->assertEquals('string', $person->properties['$ref']->type);
}
}