-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
fix(rest-explorer): exclude basePath from /openapi URL #2856
Conversation
94102bd
to
c8db7eb
Compare
I'll validate this PR with my Nginx + LB4 setup. |
The PR does not fix the issue with the following setup:
The API Explorer UI now resolves Without nginx passes the original request url One thing I can imagine is to add request headers so that LB4 app knows the proxy url:
If the nginx is dedicated for one LB4 app, we can change the conf to be:
This configuration works out of box. |
@raymondfeng It is not my intention to fix nginx setup. My intention is to fix LoopBack to correctly support the following four use cases when sending requests directly to the application:
Everything else is out of scope of this pull request. I agree it would be great to improve support for running behind a reverse proxy like nginx, but let's open a new pull request for that and don't delay landing of this fix, which I useful on its own IMO. |
I tried making an application and added
The home page is correctly served at http://[::1]:3000/api, but the explorer doesn't work: It's still looking for |
Thank you @nabdelgadir for checking. I guess I should not rely on my automated tests only and run an example app too. I'll take a look next week. |
@nabdelgadir I am not able to reproduce :( Is it possible that your example app is perhaps using I am testing the changes using Todo example app an everything works fine. Here is the patch to apply: diff --git a/examples/todo/public/index.html b/examples/todo/public/index.html
index 861687005..54b32126e 100644
--- a/examples/todo/public/index.html
+++ b/examples/todo/public/index.html
@@ -59,7 +59,7 @@
<h1>@loopback/example-todo</h1>
<h3>OpenAPI spec: <a href="/openapi.json">/openapi.json</a></h3>
- <h3>API Explorer: <a href="/explorer">/explorer</a></h3>
+ <h3>API Explorer: <a href="/api/explorer">/explorer</a></h3>
</div>
<footer class="power">
diff --git a/examples/todo/src/application.ts b/examples/todo/src/application.ts
index 9ee0dc685..4022b893a 100644
--- a/examples/todo/src/application.ts
+++ b/examples/todo/src/application.ts
@@ -18,6 +18,8 @@ export class TodoListApplication extends BootMixin(
constructor(options: ApplicationConfig = {}) {
super(options);
+ this.basePath('/api');
+
// Set up the custom sequence
this.sequence(MySequence); |
c8db7eb
to
e3516db
Compare
@bajtos ah you're right. I was using
I tested the above two with the
I tested this one with
I also tested this one with |
Endpoints serving OpenAPI spec ignore basePath setting, i.e. even if basePath is set to `/api`, the spec is served at `/openapi.json`. Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
Good catch! I turns out my new tests were written incorrectly�. They were calling I reworked my new tests to use To make the review easier, I created a new commit for these changes. I'll squash it with the first commit before landing. @nabdelgadir LGTY now? |
e3516db
to
2fdc30f
Compare
Here is the patch I used to test express composition with a custom basePath: diff --git a/examples/express-composition/public/express.html b/examples/express-composition/public/express.html
index 0dd105688..673be559e 100644
--- a/examples/express-composition/public/express.html
+++ b/examples/express-composition/public/express.html
@@ -15,7 +15,7 @@
<body>
<h2>Express</h2>
<h4 style="font-weight:normal;">This is the main Express page.
- Click <a href='/api/'>here</a> for the LoopBack main page and
+ Click <a href='/api/v1'>here</a> for the LoopBack main page and
<a href='/hello/'>here</a> to say hi.</h4>
</body>
diff --git a/examples/express-composition/src/server.ts b/examples/express-composition/src/server.ts
index a60f1f577..80684405a 100644
--- a/examples/express-composition/src/server.ts
+++ b/examples/express-composition/src/server.ts
@@ -19,6 +19,7 @@ export class ExpressServer {
constructor(options: ApplicationConfig = {}) {
this.app = express();
this.lbApp = new NoteApplication(options);
+ this.lbApp.basePath('/v1');
// Expose the front-end assets via Express, not as LB4 route
this.app.use('/api', this.lbApp.requestHandler); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some mocha test title state supports basePath
but comments within a test state basepath not supported
. Please update test titles to make things clearer; where possible. Thanks :)
Endpoints serving OpenAPI spec ignore basePath setting, i.e. even if basePath is set to
/api
, the spec is served at/openapi.json
.This pull request is fixing the problem and supersedes #2554 and #2854.
The following four use cases are correctly supported now (when sending requests directly to the application):
/
.app.basePath()
. Explorer is served at/api/explorer
, OpenAPI spec is served at/openapi.json
./api
, no LB basePath is set. Explorer is served at/api/explorer
, OpenAPI spec is served at/api/openapi.json
./api
, basePath is set tov1
. Explorer is served at/api/v1/explorer
, OpenAPI spec is served at/api/openapi.json
.Support for reverse proxies like nginx is OUT OF SCOPE of this pull request.
/cc @gordancso @dkrantsberg @sanadHaj
Checklist
👉 Read and sign the CLA (Contributor License Agreement) 👈
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated👉 Check out how to submit a PR 👈