Skip to content

[OpenAPI] @id, @type and @context fields are included in schemas inconsistently between Item and Collection operations #3667

Description

@jamesisaac

API Platform version(s) affected: 2.5.6 and master

Description

This is an easier problem to work around than the one I reported in #3666 , but thought I'd still make a note of it anyway.

If a certain serializer group only appears in a collection operation, then @id and @type fields are missing from the generated OpenAPI schema. E.g. this:

 * @ApiResource(
 *  normalizationContext={"groups"={"Book:Default"}},
 *  collectionOperations={
 *      "get"={
 *          "normalization_context"={
 *              "groups"={
 *                  "Book:Collection"
 *              },
 *          },
 *      }
 *  }
 * )

...will lead to a consumer of the OpenAPI schema thinking there are no @id and @type fields for each item in the collection, when in fact there are.

This problem can be solved if Book:Collection were to be added to an itemOperation, because the schema is then shared. However this does introduce another slight bug... it then suggests that the collection includes @context, which it doesn't.

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 endpoint according to OpenAPI schemas has no @id or @type attribute, but in reality it does.

Possible Solution

A partial solution would be including @id and @type when the schema comes from a collection.

Although technically, that still doesn't seem perfect, as then you'd have the inconsistency that @context is different between collections and items. I suppose to get this perfect you'd need a different schema for items and collections... not sure that's exactly desirable though as it will lead to even more generated schemas and longer names.

Additional Context

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