Skip to content

[OpenAPI] Subresource schema is incorrect - uses default normalizationContext, completely disregarding actual normalization context (includes minimal repro) #3666

Description

@jamesisaac

API Platform version(s) affected: 2.5.6 and master

Description

API Platform has great handling of different normalization contexts per operation, able to handle complex combinations of serialization groups across many nested objects, and still produce a perfect set of OpenAPI schemas (which can then be used to e.g. generate client-side scaffolding). However, this breaks down on Subresources (annotated with @ApiSubresource).

If I have a subresource with its own normalization context:

// Entity/Review.php

 * @ApiResource(
 *  normalizationContext={"groups"={"Review:Default"}},
 *  subresourceOperations={
 *      "api_books_reviews_get_subresource"={
 *          "normalization_context"={
 *              "groups"={
 *                  "Review:Subresource"
 *              },
 *          },
 *      },
 *  },
 * )

...then I would expect it (Review:Subresource) to be used for the OpenAPI docs. But unfortunately it's completely ignored, and the OpenAPI docs just use the default normalization context (in this case Review:Default, but would just be Symfony's default if that wasn't specified), even if that's completely different from what the endpoint actually returns.

How to reproduce

I've created a reproduction here:

https://github.com/jamesisaac/openapi-bugs

Once running, OpenAPI docs are visible at http://localhost:8000 or http://localhost:8000/docs.json

The /books/{id}/reviews endpoint returns the correct data when called (just the id and rating, as specified by Review:Subresource), but the OpenAPI docs are completely overpromising every field from Review:Default.

Additional Context

  • I've tried this on 2.5.6, and on master with OpenApi v3 refactor  #3407 merged, but the problem seems present on both.
  • In this example, no schema for Review:Subresource is generated at all, so it's not just a case of just the wrong schema being assigned to the operation - makes it very difficult to work around as potentially the fields from Review:Subresource are never exposed anywhere.

cc @soyuka

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions