Skip to content

Commit 91c0b15

Browse files
Will-thomsoyuka
authored andcommitted
Fix JSON:API collection included schema
1 parent 1c2f829 commit 91c0b15

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

src/JsonApi/JsonSchema/SchemaFactory.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,21 @@ public function buildSchema(string $className, string $format = 'jsonapi', strin
259259
$properties = $this->buildDefinitionPropertiesSchema($key, $className, $format, $type, $operation, $schema, []);
260260
$properties['data']['properties']['attributes']['$ref'] = $prefix.$key;
261261

262+
$collectionProperties = [
263+
'data' => [
264+
'type' => 'array',
265+
'items' => $properties['data'],
266+
],
267+
];
268+
269+
if (isset($properties['included'])) {
270+
$collectionProperties['included'] = $properties['included'];
271+
}
272+
262273
$schema['description'] = "$definitionName collection.";
263274
$schema['allOf'] = [
264275
['$ref' => $prefix.(false === $operation->getPaginationEnabled() ? self::COLLECTION_BASE_SCHEMA_NAME_NO_PAGINATION : self::COLLECTION_BASE_SCHEMA_NAME)],
265-
['type' => 'object', 'properties' => [
266-
'data' => [
267-
'type' => 'array',
268-
'items' => $properties['data'],
269-
],
270-
]],
276+
['type' => 'object', 'properties' => $collectionProperties],
271277
];
272278

273279
return $schema;

tests/Functional/OpenApiTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,24 @@ public function testHasSchemasForMultipleFormats(): void
261261
], 'description' => 'A resource used for OpenAPI tests.'], $res['components']['schemas']['Crud.jsonld']);
262262
}
263263

264+
public function testJsonApiCollectionSchemaDocumentsIncludedResources(): void
265+
{
266+
$response = self::createClient()->request('GET', '/docs', [
267+
'headers' => ['Accept' => 'application/vnd.openapi+json'],
268+
]);
269+
270+
$this->assertResponseIsSuccessful();
271+
$json = $response->toArray();
272+
$schema = $json['paths']['/dummies']['get']['responses']['200']['content']['application/vnd.api+json']['schema'];
273+
$properties = $schema['allOf'][1]['properties'];
274+
275+
$this->assertArrayHasKey('included', $properties);
276+
$this->assertSame('array', $properties['included']['type']);
277+
$this->assertTrue($properties['included']['readOnly']);
278+
$this->assertArrayHasKey('anyOf', $properties['included']['items']);
279+
$this->assertNotEmpty($properties['included']['items']['anyOf']);
280+
}
281+
264282
public function testRetrieveTheOpenApiDocumentation(): void
265283
{
266284
$response = self::createClient()->request('GET', '/docs', ['headers' => ['accept' => 'application/vnd.openapi+json']]);

0 commit comments

Comments
 (0)