Skip to content

OpenAPI spec incomplete when input and output have the same classname ("shortname") #8169

@petski

Description

@petski

API Platform version(s) affected: 4.2.0

Description

OpenAPI spec incomplete when input and output have the same classname ("shortname").

Both input and output will produce the same $ref, causing them to override each other.

As a result, in the OpenAPI UI, the output data/Dto is presented as "Request body" for the input.

How to reproduce

Model structure:

./src/Domain/YourDomain/Model/
├── Input
│   └── ThingCreate.php
└── Output
    └── ThingCreate.php

API resource:

new Post(
    uriTemplate: '/things',
    processor: ThingCreateProcessor::class,
    input: \App\Domain\YourDomain\Model\Input\ThingCreate::class,
    output: \App\Domain\YourDomain\Model\Output\ThingCreate::class,
    openapi: new Operation(
        summary: 'Create a new shiny thing'
    ),
),

Possible Solution

  • Take TYPE_(INPUT|OUTPUT) into consideration when doing "class-to-name"
  • Let the Factory throw when this occurs AND add some callable/interface to the factory's constructor that is responsable for "class-to-name" (and reference to that in the exception thrown). This would be a BC break I guess....
    • Edit: For the latter, DefinitionNameFactory seems already in place. One could decorate that to archieve this. So my request would be: "Can we let the Factory notify the developer in case the "overwrite" take place?"
  • A feature proposed in 🎸Dynamic OpenAPI Schema Naming Based on DTO Namespace #8162 seems related.

Additional Context

$ bin/console api:json-schema:generate 'App\Domain\YourDomain\ApiResource\Thing' --type input 
{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$ref": "#/definitions/Thing.ThingCreate.jsonld",
    "definitions": {
        "Thing.ThingCreate.jsonld": {
            "type": "object",
            "required": [
                "name",
                "slug"
            ],
            "properties": {
                "name": {
                    "maxLength": 255
                },
                "slug": {
                    "maxLength": 255
                }
            }
        }
    }
}
$ bin/console api:json-schema:generate 'App\Domain\YourDomain\ApiResource\Thing' --type output
{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$ref": "#/definitions/Thing.ThingCreate.jsonld",
    "definitions": {
        "Thing.ThingCreate.jsonld": {
            "allOf": [
                {
                    "$ref": "#/definitions/HydraItemBaseSchema"
                },
                {
                    "type": "object",
                    "properties": {
                        "id": {
                            "type": "string",
                            "format": "uuid"
                        }
                    }
                }
            ]
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions