Skip to content

OpenAPI Documentation for anyOf Schema in Abstract Resource with Discriminator Map #2817

@TheoD02

Description

@TheoD02

Hello,

I am working on an API with Symfony using API Platform and have the following setup for my resources:

#[ApiResource()]
#[DiscriminatorMap(
    typeProperty: 'type',
    mapping: [
        'first_type' => FirstTypeOffer::class,
        'second_type' => SecondTypeOffer::class
    ]
)]
abstract class BaseOffer
{
    abstract public function getOfferType(): string;
}

The BaseOffer is an abstract class, and the response can return different types of offers, such as FirstTypeOffer or SecondTypeOffer. However, in the OpenAPI documentation, the schema currently only shows the BaseOffer schema, without accounting for the possible anyOf types (i.e., FirstTypeOffer or SecondTypeOffer).

Problem:

In the API documentation, the response schema is currently generated as follows:

"responses": {
  "200": {
    "description": "BaseOffer collection",
    "content": {
      "application/ld+json": {
        "schema": {
          "type": "object",
          "properties": {
            "member": {
              "type": "array",
              "items": {
                "$ref": "#/components/schemas/BaseOffer.jsonld"
              }
            },
            ...REST OF OPENAPI
        }
      }
    }
  }
}

The BaseOffer.jsonld schema looks like this:

"BaseOffer.jsonld": {
  "type": "object",
  "properties": {
    "@id": { "readOnly": true, "type": "string" },
    "@type": { "readOnly": true, "type": "string" },
    "offerType": { "readOnly": true, "type": "string" }
  }
}

As you can see, the response schema references BaseOffer.jsonld, which does not account for the possible FirstTypeOffer or SecondTypeOffer.

Expected Behavior:

The OpenAPI documentation should use anyOf to indicate that the member array can contain FirstTypeOffer or SecondTypeOffer schemas. For example:

responses:
  200:
    description: "BaseOffer collection"
    content:
      application/json:
        schema:
          type: object
          properties:
            member:
              type: array
              items:
                anyOf:
                  - $ref: '#/components/schemas/FirstTypeOffer.jsonld'
                  - $ref: '#/components/schemas/SecondTypeOffer.jsonld'
            totalItems:
              type: integer
              minimum: 0

Question:

How can I configure API Platform to generate the anyOf schema for the OpenAPI documentation when using a discriminator map with an abstract resource?

Environment:

  • API Platform version: 4.0
  • Symfony version: 7.2
  • PHP version: 8.4

Thank you in advance for your guidance!

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