Repository navigation
Description
API Platform version(s) affected: 4.3.21 (same code on 4.4 and main)
Description
Since we upgraded from 2.7 to 4.3, the OpenAPI documentation of our JSON:API resources can no longer be opened in the browser: Swagger UI freezes when expanding an operation, and ReDoc crashes the browser tab. This is the same problem as #7146, which was closed after the 4.2 release without confirmation from the reporter: it is still there on 4.3.21.
The cause is that, for application/vnd.api+json, the documentation describes every property of the related resources, including the ones that are not in any serialization group. Each related resource is then described with all its own relations, recursively. In our application, a related resource that is serialized with 4 attributes is documented with ~310 attributes, and the OpenAPI document grows from 252 KB (with groups honored, as in 2.7 or with application/json) to 1.9 MB of cyclic schemas.
Documenting these properties does not make sense:
- We chose not to expose them. Serialization groups are precisely how we decide which properties are part of the API. The documentation now lists internal properties (e.g.
email,password…) that we deliberately keep out of the API. - API consumers can never get them. API Platform's JSON:API serializer does apply the groups, including to included resources.
includeonly works on relationships that are in the groups, and sparse fieldsets (fields[type]=…) can only narrow the output within the groups, never widen it. Whatever the request, these properties are never returned. - The documentation contradicts the responses, and the
application/jsondocumentation of the same operation, which honors the groups.
How to reproduce
api_platform:
formats:
jsonapi: ['application/vnd.api+json']#[ApiResource(operations: [new Get(normalizationContext: ['groups' => ['book:read']])])]
class Book
{
#[ApiProperty(identifier: true)]
public int $id;
#[Groups(['book:read'])]
public string $title;
#[Groups(['book:read'])]
public Author $author;
}
#[ApiResource(operations: [new Get()])]
class Author
{
#[ApiProperty(identifier: true)]
public int $id;
#[Groups(['book:read'])]
public string $name;
public string $email; // in no group
public string $passwordHash; // in no group
/** @var Book[] */
public array $books = []; // in no group
}bin/console api:json-schema:generate 'App\ApiResource\Book' --format=jsonapi
- Expected: the
includedAuthor schema only hasname, which is exactly whatGET /books/1?include=authorreturns. - Actual: the Author schema also has
email,passwordHashandbooks, andbookspoints back to the Book schema (cycle). On a real application with a few relations, the generated document becomes too large for Swagger UI.
Possible Solution
In src/JsonApi/JsonSchema/SchemaFactory.php, related resources are built with an empty serializer context (buildDefinitionPropertiesSchema(..., []), lines 164 and 261 on 4.3), with this comment:
// We don't use the serializer context here as JSON:API doesn't leverage serializer groups for related resources.As explained above, API Platform's JSON:API serializer does use them. Passing the context instead of [], at least when disable_json_schema_serializer_groups is false so the default behavior is unchanged, fixes the issue:
- $definitions[$definitionName]['properties'] = $this->buildDefinitionPropertiesSchema($key, $className, $format, $type, $operation, $schema, []);
+ $definitions[$definitionName]['properties'] = $this->buildDefinitionPropertiesSchema($key, $className, $format, $type, $operation, $schema, $jsonApiSerializerContext);
...
- $properties = $this->buildDefinitionPropertiesSchema($key, $className, $format, $type, $operation, $schema, []);
+ $properties = $this->buildDefinitionPropertiesSchema($key, $className, $format, $type, $operation, $schema, $jsonApiSerializerContext);With this change, we get group-specific definitions (e.g. Author.jsonapi-book.read) that match the real responses, and Swagger UI opens instantly.
Additional Context
- Related: Incorrect OpenAPI (Swagger) documentation for application/vnd.api+json #7146 (same symptom, closed without fix) and [Laravel / JSON:API] Request body OpenAPI docs ignore serialization groups #7294.
- Workaround we use: replacing the JSON:API schema factory with the generic one (
api_platform.jsonapi.json_schema.schema_factory: '@api_platform.json_schema.schema_factory'). The documentation is usable again, but loses the JSON:API document structure.
Activity