mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-03 14:54:28 +00:00
refactor: serve the api specs as plain json (#38715)
The api specs were Go templates whose committed form was not a valid swagger document, so `swagger-validate`, `generate-openapi.go` and `.spectral.yaml` each worked around it. They are now plain json, substituted at serve time. Renaming them off `.tmpl` also stops `make fmt` rewriting them, which used to bump their mtime and silently skip the next `make generate-swagger`. Also enables stricter spectral linting: extends `lint-swagger` to the OpenAPI 3 spec, turns on `openapi-tags`, `operation-singular-tag` and `operation-tag-defined`, adds a top-level `tags` array with descriptions to the swagger input, and drops the redundant `repository` tag from `POST /user/repos`. --------- Signed-off-by: silverwind <me@silverwind.io> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Giteabot <teabot@gitea.io>
This commit is contained in:
@@ -31,6 +31,42 @@ func TestLinks(t *testing.T) {
|
||||
t.Run("NoLoginNotExist", testLinksNoLoginNotExist)
|
||||
t.Run("AsUser", testLinksAsUser)
|
||||
t.Run("RepoCommon", testLinksRepoCommon)
|
||||
t.Run("ApiJson", testLinksApiJson)
|
||||
}
|
||||
|
||||
func testLinksApiJson(t *testing.T) {
|
||||
defer test.MockVariableValue(&setting.AppVer, "1.2.3")()
|
||||
defer test.MockVariableValue(&setting.AppSubURL)()
|
||||
t.Run("Swagger", func(t *testing.T) {
|
||||
for _, subURL := range []string{"", "/sub"} {
|
||||
setting.AppSubURL = subURL
|
||||
resp := MakeRequest(t, NewRequest(t, "GET", "/swagger.v1.json"), http.StatusOK)
|
||||
decoded := DecodeJSON(t, resp, &struct {
|
||||
BasePath string `json:"basePath"`
|
||||
Info struct {
|
||||
Version string `json:"version"`
|
||||
}
|
||||
}{})
|
||||
assert.Equal(t, subURL+"/api/v1", decoded.BasePath)
|
||||
assert.Equal(t, "1.2.3", decoded.Info.Version)
|
||||
}
|
||||
})
|
||||
t.Run("OpenAPI3", func(t *testing.T) {
|
||||
for _, subURL := range []string{"", "/sub"} {
|
||||
setting.AppSubURL = subURL
|
||||
resp := MakeRequest(t, NewRequest(t, "GET", "/openapi3.v1.json"), http.StatusOK)
|
||||
decoded := DecodeJSON(t, resp, &struct {
|
||||
Servers []struct {
|
||||
URL string `json:"url"`
|
||||
} `json:"servers"`
|
||||
Info struct {
|
||||
Version string `json:"version"`
|
||||
}
|
||||
}{})
|
||||
assert.Equal(t, subURL+"/api/v1", decoded.Servers[0].URL)
|
||||
assert.Equal(t, "1.2.3", decoded.Info.Version)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func testLinksNoLogin(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user