Skip to content

Commit 65c3fb7

Browse files
committed
improvement: generate default oauth2RedirectUrl based on page location, including the path (an omission of swagger-api#5085)
1 parent 325909f commit 65c3fb7

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

src/core/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default function SwaggerUI(opts) {
3636
maxDisplayedTags: null,
3737
filter: null,
3838
validatorUrl: "https://validator.swagger.io/validator",
39-
oauth2RedirectUrl: `${window.location.protocol}//${window.location.host}/oauth2-redirect.html`,
39+
oauth2RedirectUrl: `${window.location.protocol}//${window.location.host}${window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/'))}/oauth2-redirect.html`,
4040
persistAuthorization: false,
4141
configs: {},
4242
custom: {},
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<!-- HTML for dev server -->
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<title>Swagger UI</title>
8+
<link rel="stylesheet" type="text/css" href="/swagger-ui.css" >
9+
<link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32" />
10+
<link rel="icon" type="image/png" href="/favicon-16x16.png" sizes="16x16" />
11+
<style>
12+
html
13+
{
14+
box-sizing: border-box;
15+
overflow: -moz-scrollbars-vertical;
16+
overflow-y: scroll;
17+
}
18+
*,
19+
*:before,
20+
*:after
21+
{
22+
box-sizing: inherit;
23+
}
24+
body {
25+
margin:0;
26+
background: #fafafa;
27+
}
28+
</style>
29+
</head>
30+
31+
<body>
32+
33+
<div id="swagger-ui"></div>
34+
35+
<script src="/swagger-ui-bundle.js" charset="UTF-8"> </script>
36+
<script src="/swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
37+
<script>
38+
window.onload = function() {
39+
window["SwaggerUIBundle"] = window["swagger-ui-bundle"]
40+
window["SwaggerUIStandalonePreset"] = window["swagger-ui-standalone-preset"]
41+
// Build a system
42+
const ui = SwaggerUIBundle({
43+
url: "https://petstore.swagger.io/v2/swagger.json",
44+
dom_id: '#swagger-ui',
45+
presets: [
46+
SwaggerUIBundle.presets.apis,
47+
SwaggerUIStandalonePreset
48+
],
49+
plugins: [
50+
SwaggerUIBundle.plugins.DownloadUrl
51+
],
52+
layout: SwaggerUIStandalonePreset ? "StandaloneLayout" : "BaseLayout",
53+
onComplete: () => {
54+
if(window.completeCount) {
55+
window.completeCount++
56+
} else {
57+
window.completeCount = 1
58+
}
59+
}
60+
})
61+
62+
window.ui = ui
63+
}
64+
</script>
65+
</body>
66+
67+
</html>

test/e2e-cypress/tests/features/dynamic-default-oauth.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,22 @@ describe("dynamic default oauth2RedirectUrl", () => {
55
.then(win => win.ui.getConfigs())
66
.should("include", { oauth2RedirectUrl: "http://localhost:3230/oauth2-redirect.html" })
77
})
8+
it("should compute an oauth2RedirectUrl based on the browser's location at runtime, including the path", () => {
9+
cy.visit("/pages/5085/")
10+
.window()
11+
.then(win => win.ui.getConfigs())
12+
.should("include", { oauth2RedirectUrl: "http://localhost:3230/pages/5085/oauth2-redirect.html" })
13+
})
14+
it("should compute an oauth2RedirectUrl based on the browser's location at runtime, including the path, without confusing the file name for a folder name", () => {
15+
cy.visit("/pages/5085/index.html")
16+
.window()
17+
.then(win => win.ui.getConfigs())
18+
.should("include", { oauth2RedirectUrl: "http://localhost:3230/pages/5085/oauth2-redirect.html" })
19+
})
20+
it("should compute an oauth2RedirectUrl based on the browser's location at runtime, including the path, even it does not end with a slash", () => {
21+
cy.visit("/pages/5085")
22+
.window()
23+
.then(win => win.ui.getConfigs())
24+
.should("include", { oauth2RedirectUrl: "http://localhost:3230/pages/5085/oauth2-redirect.html" })
25+
})
826
})

0 commit comments

Comments
 (0)