Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Incorrect OpenAPI documentation for application/vnd.api+json: properties outside serialization groups are documented and the browser crashes #8951

Backlog

Description

@ramard

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. include only 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/json documentation 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 included Author schema only has name, which is exactly what GET /books/1?include=author returns.
  • Actual: the Author schema also has email, passwordHash and books, and books points 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

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions