Skip to content

Commit 619b9b5

Browse files
sapklunny
authored andcommitted
Move swagger-ui under /api/v1 (#2746)
* Move swagger interface under /api/v1 * Update swagger-ui * Add /api/swagger and prepare for multiple api version * Update test links * Fix footer link
1 parent bc8d726 commit 619b9b5

17 files changed

+166
-192
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,10 @@ generate-stylesheets:
304304
.PHONY: swagger-ui
305305
swagger-ui:
306306
rm -Rf public/vendor/assets/swagger-ui
307-
git clone --depth=10 -b v3.0.7 --single-branch https://github.com/swagger-api/swagger-ui.git $(TMPDIR)/swagger-ui
307+
git clone --depth=10 -b v3.3.2 --single-branch https://github.com/swagger-api/swagger-ui.git $(TMPDIR)/swagger-ui
308308
mv $(TMPDIR)/swagger-ui/dist public/vendor/assets/swagger-ui
309309
rm -Rf $(TMPDIR)/swagger-ui
310-
$(SED_INPLACE) "s;http://petstore.swagger.io/v2/swagger.json;../../swagger.v1.json;g" public/assets/swagger-ui/index.html
310+
$(SED_INPLACE) "s;http://petstore.swagger.io/v2/swagger.json;../../../swagger.v1.json;g" public/vendor/assets/swagger-ui/index.html
311311

312312
.PHONY: update-translations
313313
update-translations:

integrations/links_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ func TestLinksNoLogin(t *testing.T) {
2626
"/user/sign_up",
2727
"/user/login",
2828
"/user/forgot_password",
29-
"/swagger",
29+
"/api/swagger",
30+
"/api/v1/swagger",
3031
// TODO: follow this page and test every link
3132
"/vendor/librejs.html",
3233
}
@@ -47,7 +48,8 @@ func testLinksAsUser(userName string, t *testing.T) {
4748
"/explore/organizations?q=test&tab=",
4849
"/",
4950
"/user/forgot_password",
50-
"/swagger",
51+
"/api/swagger",
52+
"/api/v1/swagger",
5153
"/issues",
5254
"/issues?type=your_repositories&repo=0&sort=&state=open",
5355
"/issues?type=assigned&repo=0&sort=&state=open",

public/vendor/assets/swagger-ui/index.html

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
<style>
1212
html
1313
{
14-
box-sizing: border-box;
15-
overflow: -moz-scrollbars-vertical;
16-
overflow-y: scroll;
14+
box-sizing: border-box;
15+
overflow: -moz-scrollbars-vertical;
16+
overflow-y: scroll;
1717
}
1818
*,
1919
*:before,
2020
*:after
2121
{
22-
box-sizing: inherit;
22+
box-sizing: inherit;
2323
}
2424

2525
body {
@@ -71,10 +71,12 @@
7171
<script src="./swagger-ui-standalone-preset.js"> </script>
7272
<script>
7373
window.onload = function() {
74+
7475
// Build a system
7576
const ui = SwaggerUIBundle({
76-
url: "../../swagger.v1.json",
77+
url: "../../../swagger.v1.json",
7778
dom_id: '#swagger-ui',
79+
deepLinking: true,
7880
presets: [
7981
SwaggerUIBundle.presets.apis,
8082
SwaggerUIStandalonePreset

public/vendor/assets/swagger-ui/oauth2-redirect.html

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@
88
function run () {
99
var oauth2 = window.opener.swaggerUIRedirectOauth2;
1010
var sentState = oauth2.state;
11-
var isValid, qp;
11+
var redirectUrl = oauth2.redirectUrl;
12+
var isValid, qp, arr;
1213

13-
qp = (window.location.hash || location.search).substring(1);
14+
if (/code|token|error/.test(window.location.hash)) {
15+
qp = window.location.hash.substring(1);
16+
} else {
17+
qp = location.search.substring(1);
18+
}
1419

15-
qp = qp ? JSON.parse('{"' + qp.replace(/&/g, '","').replace(/=/g, '":"') + '"}',
20+
arr = qp.split("&")
21+
arr.forEach(function (v,i,_arr) { _arr[i] = '"' + v.replace('=', '":"') + '"';})
22+
qp = qp ? JSON.parse('{' + arr.join() + '}',
1623
function (key, value) {
1724
return key === "" ? value : decodeURIComponent(value)
1825
}
@@ -33,51 +40,18 @@
3340
if (qp.code) {
3441
delete oauth2.state;
3542
oauth2.auth.code = qp.code;
36-
createForm(oauth2.auth, qp).submit();
43+
oauth2.callback({auth: oauth2.auth, redirectUrl: redirectUrl});
3744
} else {
3845
oauth2.errCb({
3946
authId: oauth2.auth.name,
4047
source: "auth",
4148
level: "error",
42-
message: "Authorization failed: no accessCode came from the server"
49+
message: "Authorization failed: no accessCode received from the server"
4350
});
44-
window.close();
4551
}
4652
} else {
47-
oauth2.callback({auth: oauth2.auth, token: qp, isValid: isValid});
48-
window.close();
53+
oauth2.callback({auth: oauth2.auth, token: qp, isValid: isValid, redirectUrl: redirectUrl});
4954
}
55+
window.close();
5056
}
51-
52-
function createForm(auth, qp) {
53-
var form = document.createElement("form");
54-
var schema = auth.schema;
55-
var action = schema.get("tokenUrl");
56-
var name, input;
57-
58-
var fields = {
59-
code: qp.code,
60-
"redirect_uri": location.protocol + "//" + location.host + location.pathname,
61-
"grant_type": "authorization_code",
62-
"client_secret": auth.clientSecret,
63-
"client_id": auth.clientId
64-
}
65-
66-
for ( name in fields ) {
67-
input = document.createElement("input");
68-
input.name = name;
69-
input.value = fields[name];
70-
input.type = "hidden";
71-
form.appendChild(input);
72-
}
73-
74-
75-
form.method = "POST";
76-
form.action = action;
77-
78-
document.body.appendChild(form);
79-
80-
return form;
81-
}
82-
8357
</script>

public/vendor/assets/swagger-ui/swagger-ui-bundle.js

Lines changed: 86 additions & 104 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/vendor/assets/swagger-ui/swagger-ui-bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/vendor/assets/swagger-ui/swagger-ui-standalone-preset.js

Lines changed: 12 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/vendor/assets/swagger-ui/swagger-ui-standalone-preset.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/vendor/assets/swagger-ui/swagger-ui.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/vendor/assets/swagger-ui/swagger-ui.js

Lines changed: 7 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/vendor/assets/swagger-ui/swagger-ui.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

routers/api/v1/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,11 @@ func mustAllowPulls(ctx *context.Context) {
275275
func RegisterRoutes(m *macaron.Macaron) {
276276
bind := binding.Bind
277277

278+
m.Get("/swagger", misc.Swagger) //Render V1 by default
279+
278280
m.Group("/v1", func() {
279281
// Miscellaneous
282+
m.Get("/swagger", misc.Swagger)
280283
m.Get("/version", misc.Version)
281284
m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
282285
m.Post("/markdown/raw", misc.MarkdownRaw)

routers/api/v1/misc/swagger.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package misc
6+
7+
import (
8+
"code.gitea.io/gitea/modules/base"
9+
"code.gitea.io/gitea/modules/context"
10+
)
11+
12+
// tplSwagger swagger page template
13+
const tplSwagger base.TplName = "swagger"
14+
15+
// Swagger render swagger-ui page with v1 json
16+
func Swagger(ctx *context.Context) {
17+
ctx.Data["APIJSONVersion"] = "v1"
18+
ctx.HTML(200, tplSwagger)
19+
}

routers/home.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
const (
2121
// tplHome home page template
2222
tplHome base.TplName = "home"
23-
// tplSwagger swagger page template
24-
tplSwagger base.TplName = "swagger"
2523
// tplExploreRepos explore repositories page template
2624
tplExploreRepos base.TplName = "explore/repos"
2725
// tplExploreUsers explore users page template
@@ -53,11 +51,6 @@ func Home(ctx *context.Context) {
5351
ctx.HTML(200, tplHome)
5452
}
5553

56-
// Swagger render swagger-ui page
57-
func Swagger(ctx *context.Context) {
58-
ctx.HTML(200, tplSwagger)
59-
}
60-
6154
// RepoSearchOptions when calling search repositories
6255
type RepoSearchOptions struct {
6356
OwnerID int64

routers/routes/routes.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ func RegisterRoutes(m *macaron.Macaron) {
160160
return ""
161161
})
162162
m.Get("/", ignSignIn, routers.Home)
163-
m.Get("/swagger", ignSignIn, routers.Swagger)
164163
m.Group("/explore", func() {
165164
m.Get("", func(ctx *context.Context) {
166165
ctx.Redirect(setting.AppSubURL + "/explore/repos")

templates/base/footer.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</div>
2424
</div>
2525
<a href="{{AppSubUrl}}/vendor/librejs.html" data-jslicense="1">Javascript licenses</a>
26-
<a href="{{AppSubUrl}}/swagger">API</a>
26+
<a href="{{AppSubUrl}}/api/swagger">API</a>
2727
<a target="_blank" rel="noopener" href="https://gitea.io">{{.i18n.Tr "website"}}</a>
2828
{{if (or .ShowFooterVersion .PageIsAdmin)}}<span class="version">{{GoVer}}</span>{{end}}
2929
</div>

templates/swagger.tmpl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
<style>
1212
html
1313
{
14-
box-sizing: border-box;
15-
overflow: -moz-scrollbars-vertical;
16-
overflow-y: scroll;
14+
box-sizing: border-box;
15+
overflow: -moz-scrollbars-vertical;
16+
overflow-y: scroll;
1717
}
1818
*,
1919
*:before,
2020
*:after
2121
{
22-
box-sizing: inherit;
22+
box-sizing: inherit;
2323
}
2424

2525
body {
@@ -70,11 +70,13 @@
7070
<script src="{{AppSubUrl}}/vendor/assets/swagger-ui/swagger-ui-bundle.js"> </script>
7171
<script src="{{AppSubUrl}}/vendor/assets/swagger-ui/swagger-ui-standalone-preset.js"> </script>
7272
<script>
73+
7374
window.onload = function() {
7475
// Build a system
7576
const ui = SwaggerUIBundle({
76-
url: "{{AppUrl}}swagger.v1.json",
77+
url: "{{AppUrl}}swagger.{{.APIJSONVersion}}.json",
7778
dom_id: '#swagger-ui',
79+
deepLinking: true,
7880
presets: [
7981
SwaggerUIBundle.presets.apis,
8082
SwaggerUIStandalonePreset

0 commit comments

Comments
 (0)