Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support changing the OpenAPI document based on the request #2502

Closed
1 task done
botflux opened this issue Jun 29, 2023 · 3 comments
Closed
1 task done

Support changing the OpenAPI document based on the request #2502

botflux opened this issue Jun 29, 2023 · 3 comments
Labels

Comments

@botflux
Copy link
Contributor

botflux commented Jun 29, 2023

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

I use NestJs with the swagger module in a multi-tenant environment where each customer is a tenant.
All my API endpoints are prefixed with "/:customer/" using setGlobalPrefix.

I want all the requests made by the swagger UI prefixed with the current customer.
For example, if I go on /customer-1/docs, I want all the requests to start with /customer-1/.

I serve my swagger under /:customer/docs using SwaggerModule.setup('/:customer/docs', app, document,{useGlobalPrefix: false}).

In this setup, if I have a route /:customer/my-route, the swagger UI will target /my-route since the swagger module doesn't know there is a global prefix.

I want to be able to change the servers field of the OpenAPI document to add the /:customer part.

Describe the solution you'd like

To support this behaviour, we could pass a function called patchDocument that takes the request, response and document and return a patched document.

SwaggerModule.setup('/:customer/docs', app, document, {
    useGlobalPrefix: false,
    patchDocument: (req: Request, res: Response, document: OpenAPIObject): OpenAPIObject => {
      // change document.servers based on req.params.customer
      // and return the patched document
    }
  });

Teachability, documentation, adoption, migration strategy

No response

What is the motivation / use case for changing the behavior?

In our multi-tenant environment, we want our customers to be isolated. Automatically patching the OpenAPI document based on the request allow us to not add the :customer path param to each request.

@kamilmysliwiec
Copy link
Member

kamilmysliwiec commented Jun 30, 2023

Would you like to create a PR for this issue?

@botflux
Copy link
Contributor Author

botflux commented Jun 30, 2023

Yes !

botflux added a commit to botflux/swagger that referenced this issue Jun 30, 2023
Define `patchDocument` option to patch the OpenAPI document
based on the request, response and generated OpenAPI document.

nestjs#2502
@kamilmysliwiec
Copy link
Member

Let's track this here #2505

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants