Skip to content

Fix JSON:API collection included schema#8190

Open
Will-thom wants to merge 1 commit into
api-platform:mainfrom
Will-thom:fix/jsonapi-openapi-included
Open

Fix JSON:API collection included schema#8190
Will-thom wants to merge 1 commit into
api-platform:mainfrom
Will-thom:fix/jsonapi-openapi-included

Conversation

@Will-thom
Copy link
Copy Markdown

Fixes #7956.

Preserves the JSON:API included schema when building OpenAPI schemas for collection responses and adds a functional regression test.

Tests:

  • php -l src/JsonApi/JsonSchema/SchemaFactory.php
  • php -l tests/Functional/OpenApiTest.php
  • vendor/bin/phpunit tests/Functional/OpenApiTest.php --filter testJsonApiCollectionSchemaDocumentsIncludedResources
  • vendor/bin/phpunit tests/Functional/OpenApiTest.php --filter testRetrieveTheOpenApiDocumentation
  • vendor/bin/phpunit tests/Functional/OpenApiTest.php --filter testHasSchemasForMultipleFormats
  • vendor/bin/phpunit tests/Functional/PaginationDisabledTest.php --filter testSchemaCollectionJsonApi

Comment on lines +262 to +276
$collectionProperties = [
'data' => [
'type' => 'array',
'items' => $properties['data'],
],
];

if (isset($properties['included'])) {
$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],
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.

],
];

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSON:API included compound documents no longer represented in OpenAPI output in 4.3.x

2 participants