Support custom basePath
See original GitHub issueI 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.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:10 (4 by maintainers)
I also have a similar issue where this would come in handy.
In my case I use a spec produced by a third party, which includes some (dummy) absolute base path (e.g,
http://my.server.com/base/rsc/v1
). What I currently do is modify the spec to say/base/rsc/v1
instead.Of course this creates a problem when a new version of the spec comes along; I need to remember to change the path again, or else express-openapi-validator will stop validating requests without me being aware there’s a problem.
Of course this is manageable (like pre-processing the spec as suggested previously), but would be great to have a “not so hacky” solution.
Anyways, thanks for this great library!
Thanks @henhal. I’m going to leave this open for a bit.