-
-
Notifications
You must be signed in to change notification settings - Fork 901
/
Copy pathJsonSchemaGenerateCommandTest.php
350 lines (293 loc) · 15.9 KB
/
JsonSchemaGenerateCommandTest.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
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Tests\JsonSchema\Command;
use ApiPlatform\Tests\Fixtures\TestBundle\Document\Dummy as DocumentDummy;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\ApplicationTester;
/**
* @author Jacques Lefebvre <[email protected]>
*/
class JsonSchemaGenerateCommandTest extends KernelTestCase
{
use ExpectDeprecationTrait;
private ApplicationTester $tester;
private string $entityClass;
protected function setUp(): void
{
$kernel = self::bootKernel();
$application = new Application(static::$kernel);
$application->setCatchExceptions(true);
$application->setAutoExit(false);
$this->entityClass = 'mongodb' === $kernel->getEnvironment() ? DocumentDummy::class : Dummy::class;
$this->tester = new ApplicationTester($application);
}
public function testExecuteWithoutOption(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass]);
$this->assertJson($this->tester->getDisplay());
}
public function testExecuteWithItemOperationGet(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--operation' => '_api_/dummies/{id}{._format}_get', '--type' => 'output']);
$this->assertJson($this->tester->getDisplay());
}
public function testExecuteWithCollectionOperationGet(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--operation' => '_api_/dummies{._format}_get_collection', '--type' => 'output']);
$this->assertJson($this->tester->getDisplay());
}
public function testExecuteWithJsonldFormatOption(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--operation' => '_api_/dummies{._format}_post', '--format' => 'jsonld', '--type' => 'output']);
$result = $this->tester->getDisplay();
$this->assertStringContainsString('@id', $result);
$this->assertStringContainsString('@context', $result);
$this->assertStringContainsString('@type', $result);
}
public function testExecuteWithJsonldTypeInput(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--operation' => '_api_/dummies{._format}_post', '--format' => 'jsonld', '--type' => 'input']);
$result = $this->tester->getDisplay();
$this->assertStringNotContainsString('@id', $result);
$this->assertStringNotContainsString('@context', $result);
$this->assertStringNotContainsString('@type', $result);
}
/**
* Test issue #5501, the locations relation inside BrokenDocs is a Resource (named Related) but its only operation is a NotExposed.
* Still, serializer groups are set, and therefore it is a "readableLink" so we actually want to compute the schema, even if it's not accessible
* directly, it is accessible through that relation.
*/
public function testExecuteWithNotExposedResourceAndReadableLink(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5501\BrokenDocs', '--type' => 'output']);
$result = $this->tester->getDisplay();
$this->assertStringContainsString('Related.jsonld-location.read_collection', $result);
}
/**
* When serializer groups are present the Schema should have an embed resource. #5470 breaks array references when serializer groups are present.
*/
public function testArraySchemaWithReference(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue5793\BagOfTests', '--type' => 'input']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$this->assertEquals($json['definitions']['BagOfTests.jsonld-write']['properties']['tests'], [
'type' => 'string',
'foo' => 'bar',
]);
$this->assertEquals($json['definitions']['BagOfTests.jsonld-write']['properties']['nonResourceTests'], [
'type' => 'array',
'items' => [
'$ref' => '#/definitions/NonResourceTestEntity.jsonld-write',
],
]);
$this->assertEquals($json['definitions']['BagOfTests.jsonld-write']['properties']['description'], [
'maxLength' => 255,
]);
$this->assertEquals($json['definitions']['BagOfTests.jsonld-write']['properties']['type'], [
'$ref' => '#/definitions/TestEntity.jsonld-write',
]);
}
public function testArraySchemaWithMultipleUnionTypesJsonLd(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6212\Nest', '--type' => 'output', '--format' => 'jsonld']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$this->assertEquals($json['definitions']['Nest.jsonld']['properties']['owner']['anyOf'], [
['$ref' => '#/definitions/Wren.jsonld'],
['$ref' => '#/definitions/Robin.jsonld'],
['type' => 'null'],
]);
$this->assertArrayHasKey('Wren.jsonld', $json['definitions']);
$this->assertArrayHasKey('Robin.jsonld', $json['definitions']);
}
public function testArraySchemaWithMultipleUnionTypesJsonApi(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6212\Nest', '--type' => 'output', '--format' => 'jsonapi']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$this->assertEquals($json['definitions']['Nest.jsonapi']['properties']['data']['properties']['attributes']['properties']['owner']['anyOf'], [
['$ref' => '#/definitions/Wren.jsonapi'],
['$ref' => '#/definitions/Robin.jsonapi'],
['type' => 'null'],
]);
$this->assertArrayHasKey('Wren.jsonapi', $json['definitions']);
$this->assertArrayHasKey('Robin.jsonapi', $json['definitions']);
}
public function testArraySchemaWithMultipleUnionTypesJsonHal(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6212\Nest', '--type' => 'output', '--format' => 'jsonhal']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$this->assertEquals($json['definitions']['Nest.jsonhal']['properties']['owner']['anyOf'], [
['$ref' => '#/definitions/Wren.jsonhal'],
['$ref' => '#/definitions/Robin.jsonhal'],
['type' => 'null'],
]);
$this->assertArrayHasKey('Wren.jsonhal', $json['definitions']);
$this->assertArrayHasKey('Robin.jsonhal', $json['definitions']);
}
/**
* TODO: add deprecation (TypeFactory will be deprecated in api platform 3.3).
*
* @group legacy
*/
public function testArraySchemaWithTypeFactory(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5896\Foo', '--type' => 'output']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$this->assertEquals($json['definitions']['Foo.jsonld']['properties']['expiration'], ['type' => 'string', 'format' => 'date']);
}
/**
* Test issue #5998.
*/
public function testWritableNonResourceRef(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue5998\SaveProduct', '--type' => 'input']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$this->assertEquals($json['definitions']['SaveProduct.jsonld']['properties']['codes']['items']['$ref'], '#/definitions/ProductCode.jsonld');
}
/**
* Test issue #6299.
*/
public function testOpenApiResourceRefIsNotOverwritten(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6299\Issue6299', '--type' => 'output']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$this->assertEquals('#/definitions/DummyFriend', $json['definitions']['Issue6299.Issue6299OutputDto.jsonld']['properties']['itemDto']['$ref']);
$this->assertEquals('#/definitions/DummyDate', $json['definitions']['Issue6299.Issue6299OutputDto.jsonld']['properties']['collectionDto']['items']['$ref']);
}
/**
* Test related Schema keeps json-ld context.
*/
public function testSubSchemaJsonLd(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelatedDummy', '--type' => 'output', '--format' => 'jsonld']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$this->assertArrayHasKey('@id', $json['definitions']['ThirdLevel.jsonld-friends']['properties']);
}
public function testJsonApiIncludesSchema(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\Entity\Question', '--type' => 'output', '--format' => 'jsonapi']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$properties = $json['definitions']['Question.jsonapi']['properties']['data']['properties'];
$included = $json['definitions']['Question.jsonapi']['properties']['included'];
$this->assertArrayHasKey('answer', $properties['relationships']['properties']);
$this->assertArrayHasKey('anyOf', $included['items']);
$this->assertCount(1, $included['items']['anyOf']);
$this->assertArrayHasKey('$ref', $included['items']['anyOf'][0]);
$this->assertSame('#/definitions/Answer.jsonapi', $included['items']['anyOf'][0]['$ref']);
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\AnimalObservation', '--type' => 'output', '--format' => 'jsonapi']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$properties = $json['definitions']['AnimalObservation.jsonapi']['properties']['data']['properties'];
$included = $json['definitions']['AnimalObservation.jsonapi']['properties']['included'];
$this->assertArrayHasKey('individuals', $properties['relationships']['properties']);
$this->assertArrayNotHasKey('individuals', $properties['attributes']['properties']);
$this->assertArrayHasKey('anyOf', $included['items']);
$this->assertCount(1, $included['items']['anyOf']);
$this->assertSame('#/definitions/Animal.jsonapi', $included['items']['anyOf'][0]['$ref']);
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Animal', '--type' => 'output', '--format' => 'jsonapi']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$properties = $json['definitions']['Animal.jsonapi']['properties']['data']['properties'];
$included = $json['definitions']['Animal.jsonapi']['properties']['included'];
$this->assertArrayHasKey('species', $properties['relationships']['properties']);
$this->assertArrayNotHasKey('species', $properties['attributes']['properties']);
$this->assertArrayHasKey('anyOf', $included['items']);
$this->assertCount(1, $included['items']['anyOf']);
$this->assertSame('#/definitions/Species.jsonapi', $included['items']['anyOf'][0]['$ref']);
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Species', '--type' => 'output', '--format' => 'jsonapi']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$properties = $json['definitions']['Species.jsonapi']['properties']['data']['properties'];
$this->assertArrayHasKey('kingdom', $properties['attributes']['properties']);
$this->assertArrayHasKey('phylum', $properties['attributes']['properties']);
}
/**
* Test issue #6317.
*/
public function testBackedEnumExamplesAreNotLost(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6317\Issue6317', '--type' => 'output', '--format' => 'jsonld']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$properties = $json['definitions']['Issue6317.jsonld']['properties'];
$this->assertArrayHasKey('example', $properties['id']);
$this->assertArrayHasKey('example', $properties['name']);
// jsonldContext
$this->assertArrayNotHasKey('example', $properties['ordinal']);
// openapiContext
$this->assertArrayNotHasKey('example', $properties['cardinal']);
}
public function testResourceWithEnumPropertiesSchema(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\ResourceWithEnumProperty', '--type' => 'output', '--format' => 'jsonld']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$properties = $json['definitions']['ResourceWithEnumProperty.jsonld']['properties'];
$this->assertSame(
[
'type' => ['string', 'null'],
'format' => 'iri-reference',
'example' => 'https://example.com/',
],
$properties['intEnum']
);
$this->assertSame(
[
'type' => 'array',
'items' => [
'type' => 'string',
'format' => 'iri-reference',
'example' => 'https://example.com/',
],
],
$properties['stringEnum']
);
$this->assertSame(
[
'type' => ['string', 'null'],
'enum' => ['male', 'female', null],
],
$properties['gender']
);
$this->assertSame(
[
'type' => 'array',
'items' => [
'type' => 'string',
'enum' => ['male', 'female'],
],
],
$properties['genders']
);
}
/**
* @group legacy
* TODO: find a way to keep required properties if needed
*/
public function testPatchSchemaRequiredProperties(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\PatchRequired\PatchMe', '--format' => 'json']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$this->assertEquals(['b'], $json['definitions']['PatchMe']['required']);
}
}