kogosoftwarellc / open-api

A Monorepo of various packages to power OpenAPI in node
MIT License
895 stars 237 forks source link

[express-openapi] OAS 3.0 servers and express app mount point #826

Open Jule- opened 2 years ago

Jule- commented 2 years ago

Regarding an API served at https://example.com/api/sub-api/, we should expect an API doc like this:

# api-doc.yml

openapi: '3.0.2'
info:
  title: Sub API
  version: '1.0.0'
  description: 'The Sub API does stuff...'
servers:
  - url: https://example.com/api/sub-api/

paths: {}

But if we try, for the sake of containment, to mount a sub express app at /api/sub-api with express-openapi we land in a "double nested routing" that leads to serving our API on /api/sub-api/api/sub-api/.

It seems that this lib is not subtracting the express mounting point to the actual calculated basePath.

To illustrate, here is a bootstrap of the situation:

const apiApp = express();

await initialize({ apiDoc: "./api-doc.yml", app: apiApp, paths: "./paths" });

const app = express().use("/api/sub-api", apiApp)

app.listen(port);

Here I would expect that /api/sub-api is removed from the generated routing at the mount point but it is not the case. This leads to 2 awkward workarounds:

  1. Replace .use("/api/sub-api", apiApp) by .use("/", apiApp) which could lead to unexpected routing hung by implementation of express-openapi and writing of the api-doc.yml.
  2. Replace url: https://example.com/api/sub-api/ by url: https://example.com/ which is untrue and could break other dependencies like Swagger UI.

Don't you think it could be valuable to remove the mount path from the base path? It could be seamless since I think people can't use the lib that way right now.

Jule- commented 2 years ago

You can use this sandbox to check that you need to double the /api to a path like /api/api/api-docs in order to reach the doc route. https://codesandbox.io/s/express-openapi-mount-path-we8kqn