Skip to content

[API Platform] Missing server object in OpenAPI specification #1405

@lusimeon

Description

@lusimeon

Description

Considering API Platform recipes set a prefix /api in front of all API urls, recipes must add a server object to OpenAPI specification.

This way, it will be in line with OpenAPI specification and so, more compliant with OpenAPI specification related projects.

How to reproduce

For example, APIP api-doc-parser don't work out of the box (see this issue and I face the same kind of issue) with the /api prefix, and APIP admin neither (because based on APIP api-doc-parser).

Possible solution

This could be done adding a decorator responsible to add server object to specification and remove prefix from all routes, see example below.

OpenApiFactoryDecorator.php (source)

#[AsDecorator('api_platform.openapi.factory', priority: -99)]
readonly class OpenApiFactoryDecorator implements OpenApiFactoryInterface
{
    public function __construct(
        private OpenApiFactoryInterface $decorated,
    ) {
    }

    public function __invoke(array $context = []): OpenApi
    {
        $openApi = $this->decorated->__invoke($context);

        $paths = $openApi->getPaths()->getPaths();
        $newPaths = new Paths();
        foreach ($paths as $keyPath => $path) {
                /** @var PathItem $path */
                $newPaths->addPath(\str_replace('/api', '', $keyPath), $path);
        }

        $openApi = $openApi->withPaths($newPaths);
        $openApi = $openApi->withServers([new Server('/api')]);

        return $openApi;
    }
}

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