-
-
Notifications
You must be signed in to change notification settings - Fork 222
Support custom basePath #113
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
Comments
Assuming PR's are welcome, I provided one for you to look at. Thanks. |
@henhal PRs are always welcome. We love contributors! Thanks for your interest in this project :) With respect to the servers proposal...
That all being said and assuming you still do not want to include both base paths in the openapi spec, particularly when running in production, I would recommend the following... Add a modification to your deployment process that modifies the spec. For example, you might use a yaml parser or tool e.g. Hope this helps! |
Thanks for the input. I honestly don't think a base path option is very cluttering, and other open api adapters I've worked with provide this, but I see your point. I actually already use a preprocessing step on my spec (with yimp) so I guess I could solve it there though. However, I want to add that I think flexibility is key. For example I'd also have liked being able to skip certain types of validation, such as headers. Same reason really, a public facing api gateway might take care of the security schemes such as api keys and access tokens before relaying requests to a backend server. In that case I'd like to be able to customize so that headers are ignored in my express app. Just wanted to bring some context around the fact that open api specs can be used in several layers of a system. |
Thanks @henhal. I'm going to leave this open for a bit. |
@henhal you're point about security is a very good one. There certainly should be a way to opt-out of security, particularly since this is an area gateways often handle. I will be sure to add that feature asap. |
I don't known if it applies to this issue. We have a variable in our base path (see specs serverObject). The validator is only applied when I use the default value "000000". The validator does not work when I do a request on Edit: I think #115 would solve our issue
|
Thanks @dannydeut. This is a gap.we do plsn to add support server variables. Feel free to create a separate issue for this. |
I have the same issue, using AWS with API Gateway and trying to present a spec for client use but also using a spec for validation on the backend. I would appreciate some way to "not validate servers or base path" so that I could have one single spec instead of multiple spec views. My workaround is to have a spec for validation and a modified spec for client inspection. |
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, 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! |
Just to leave another vote for this feature: I have exactly the same problem as stated in the original post:
My current workaround while still in development is to use a relative path as the |
the new See PR #857 |
@cdimascio If i'm following this issue correctly, PR #857 only partially fixes the issues described here. Just to recap: servers:
- url: /api/my/pubic/gateway/
description: Public Gateway Server - Used by the public, performs rewrites to below internal URL
- url: /api/v1/private/
description: Internal URL - used by express-openapi-validator on internal server for validation only. However I would say the issues with this is that gateway spec exposes the internal server rewrite path, when it's only there for use by the internal sever validation code. Ontop of this, express-openapi-validator can't validate anything else but the express route its used on, so the gateway path Issue #857 servers:
- url: /api/my/pubic/gateway/
description: Public Gateway Server
- url: /
description: Allow lookupRoute()'s req.url to match this `/` prefix of the express route where the validator is used(). So when our validator is used() on the express route: One stop-gap solution I can think of is to NOT define the // Load schema as a json object and attach a dummy "/" server for relative route matching in the lookupRoute() fn
const schema = loadSchema("./path/to/schema.json"); // Or use yaml
schema.servers.push({ url: "/" });
// Attach the validator to some sub route, EG: /api/v1/private/
router.use(
OpenApiValidator.middleware({
apiSpec: schema,
useRequestUrl: true, // Use our CURRENT relative express route which will match the relative "/" server path
}),
); The spec: servers:
- url: /api/v1/
description: Gateway And what it looks like in a swagger-ui using this exact spec: |
Uh oh!
There was an error while loading. Please reload this page.
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:
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 theservers
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 theservers
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 theservers
section including the path prefix, but each sub-app handles requests without the prefix.The text was updated successfully, but these errors were encountered: