core icon indicating copy to clipboard operation
core copied to clipboard

OpenAPI (Swagger) documentation does not take into account the routes hostname

Open Crovitche-1623 opened this issue 2 years ago • 2 comments

API Platform version(s) affected: ^3.1

Description
I have endpoints that needs to be accessible from different hosts for a multi-tenant app. On the main domain (example.com/api), there are common data endpoints. For tenant subdomain (tenant_identifier.example.com/api), there are the tenant data endpoints.

I tried to restrict the common data endpoints with the host parameter and it works as expected but the endpoints does not disappear from the tenant API documentation (tenant_identifier.example.com/api/docs). Therefore, the host does not seems to be taken into account for the OpenAPI specification.

How to reproduce
I have the following service configuration to inject the hostname dynamically:

# config/services.yaml
parameters:
  # Used for route host matching as %env(...)% syntax is not supported
  # @see https://stackoverflow.com/questions/47165113/environment-variables-in-route-annotations
  hostname: '%env(resolve:APP_HOST)%'
# .env
APP_HOST="example.com"

When I specify the host used for an ApiResource, for instance:

#[ApiResource(
    // ...
    host: '%hostname%',
    // ...
)]
class FooBar

The endpoints are still available from the tenant API documentation (tenant_identifier.example.com).

Possible Solution
The server url should be taken into account as it's shown in the OpenAPI specification.

Additional Context
Ask if necessary

Crovitche-1623 avatar Dec 14 '23 16:12 Crovitche-1623

Is there a way to get the operation host in a decorator maybe ?

Crovitche-1623 avatar Jan 03 '24 21:01 Crovitche-1623

I've looked into the ApiPlatform\OpenApi\Factory\OpenApiFactory and saw that the $servers variable is not based on the host metadatas. Perhaps a change could be made here if I'm not mistaken ?

Crovitche-1623 avatar Jan 03 '24 22:01 Crovitche-1623