question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support custom basePath

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rjbmacommented, Oct 25, 2021

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!

1reaction
cdimasciocommented, Nov 12, 2019

Thanks @henhal. I’m going to leave this open for a bit.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Base Path - next.config.js
Learn more about setting a base path in Next.js. ... Custom `Document` ... application under a sub-path of a domain you can use...
Read more >
Feature request: Basepath support · Issue #4998 · vercel/next.js
I just don't want to maintain custom fork of next.js. It doesn't make sense in my opinion. Additional context. The assetPrefix solution allows ......
Read more >
Amazon API Gateway custom domain names now support ...
Amazon API Gateway custom domain names now support multi-level base path mappings · You can now configure each path segment of an API...
Read more >
API Host and Base Path - Swagger
The base URL is defined by schemes , host and basePath on the root level of the ... Swagger supports the http ,...
Read more >
How to set base url for rest in spring boot? - Stack Overflow
basePath =/api ... It is now possible to define a custom RequestMappingHandlerMapping that allows to define a ... spring: data: rest: basePath: /api....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found