Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/JsonApi/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,21 @@ public function buildSchema(string $className, string $format = 'jsonapi', strin
$properties = $this->buildDefinitionPropertiesSchema($key, $className, $format, $type, $operation, $schema, []);
$properties['data']['properties']['attributes']['$ref'] = $prefix.$key;

$collectionProperties = [
'data' => [
'type' => 'array',
'items' => $properties['data'],
],
];

if (isset($properties['included'])) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: buildDefinitionPropertiesSchema returns 'included' => [...] only when relationships exist (see L357-376), so it can never be null here — isset() is safe. If the suggestion above is applied this branch goes away anyway.

$collectionProperties['included'] = $properties['included'];
}

$schema['description'] = "$definitionName collection.";
$schema['allOf'] = [
['$ref' => $prefix.(false === $operation->getPaginationEnabled() ? self::COLLECTION_BASE_SCHEMA_NAME_NO_PAGINATION : self::COLLECTION_BASE_SCHEMA_NAME)],
['type' => 'object', 'properties' => [
'data' => [
'type' => 'array',
'items' => $properties['data'],
],
]],
['type' => 'object', 'properties' => $collectionProperties],
Comment on lines +262 to +276
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be simpler by mutating $properties['data'] in place instead of introducing a $collectionProperties intermediate. That way included (and any future top-level key returned by buildDefinitionPropertiesSchema) flows through automatically:

$properties['data'] = [
    'type' => 'array',
    'items' => $properties['data'],
];

$schema['description'] = "$definitionName collection.";
$schema['allOf'] = [
    ['$ref' => $prefix.(false === $operation->getPaginationEnabled() ? self::COLLECTION_BASE_SCHEMA_NAME_NO_PAGINATION : self::COLLECTION_BASE_SCHEMA_NAME)],
    ['type' => 'object', 'properties' => $properties],
];

Removes the explicit isset($properties['included']) branch and keeps the wrapper shape consistent with the item schema.

];

return $schema;
Expand Down
18 changes: 18 additions & 0 deletions tests/Functional/OpenApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,24 @@ public function testHasSchemasForMultipleFormats(): void
], 'description' => 'A resource used for OpenAPI tests.'], $res['components']['schemas']['Crud.jsonld']);
}

public function testJsonApiCollectionSchemaDocumentsIncludedResources(): void
{
$response = self::createClient()->request('GET', '/docs', [
'headers' => ['Accept' => 'application/vnd.openapi+json'],
]);

$this->assertResponseIsSuccessful();
$json = $response->toArray();
$schema = $json['paths']['/dummies']['get']['responses']['200']['content']['application/vnd.api+json']['schema'];
$properties = $schema['allOf'][1]['properties'];

$this->assertArrayHasKey('included', $properties);
$this->assertSame('array', $properties['included']['type']);
$this->assertTrue($properties['included']['readOnly']);
$this->assertArrayHasKey('anyOf', $properties['included']['items']);
$this->assertNotEmpty($properties['included']['items']['anyOf']);
}

public function testRetrieveTheOpenApiDocumentation(): void
{
$response = self::createClient()->request('GET', '/docs', ['headers' => ['accept' => 'application/vnd.openapi+json']]);
Expand Down
Loading