Skip to content

Support custom basePath #113

Open
Open
@henhal

Description

@henhal

I have an OpenAPI 3.0.2 definition which I use to create the public documentation for my API. In the spec, I list the URL:s of my public servers under the servers section.
Now I am trying to use the same definition document with express-openapi-validator to validate my requests, but my backend server is running in a private VPC behind a proxy and is not using the same base path as the public facing API served through AWS API gateway.

To solve this it would be great to be able to supply a custom basePath to the options passed to the OpenApiValidator, instead of having it parse the servers section.

Example:

document:

openapi: 3.0.2
info:
  title: foo
servers:
  - url: 'https://my-public-api.com/some/path/prefix'

API gateway will now route /some/path/prefix/* to my Express application, without the prefix, so a request for /some/path/prefix/foo will be routed to my backend server simply as /foo.
However, when I attach the OpenApi validator, it will look for route /foo in the open api route map as /some/path/prefix/foo, since it will have applied the servers path prefix to all routes, and this route does not exist so it ends up producing a NOT FOUND response.

I know that if I supply both the public and the internal mapping under the servers section, the routing will work - but OpenApiValidator will create a route map of double the size, half of it being incorrect - and my documentation rendered from the definition will now also be wrong.

It would be a shame to have to separate the definition I use as input to my documentation renderer from the one I use for validation in my backend server. It seems this could be solved by allowing a custom basePath: '/' to be supplied through the constructor options, which would then mean ignoring the servers section?

Note that this scenario also applies if you use multiple express sub-apps with separate API definitions for each one, e.g. you could have one definition for admin routes and one for user routes, and express set up with app.use('/admin', adminApp); app.use('/user', userApp);. Then the definitions would state the URL in the servers section including the path prefix, but each sub-app handles requests without the prefix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions