Skip to content

Commit 96f8b80

Browse files
committed
fix(jsonapi): include collection schema data properties
1 parent 7383ea7 commit 96f8b80

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

src/JsonApi/JsonSchema/SchemaFactory.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,15 @@ 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+
$properties['data'] = [
263+
'type' => 'array',
264+
'items' => $properties['data'],
265+
];
266+
262267
$schema['description'] = "$definitionName collection.";
263268
$schema['allOf'] = [
264269
['$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-
]],
270+
['type' => 'object', 'properties' => $properties],
271271
];
272272

273273
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)