Skip to content

Commit 2848c5e

Browse files
6543lafriks
authored andcommitted
Swagger info corrections (#9441)
* use numbers and not http.Status___ enum * fix test * add many missing swagger responses * code format * Deletion Sould return 204 ... * error handling improvements * if special error type ... then add it to swagger too * one smal nit * invalidTopicsError is []string * valid swagger specification 2.0 - if you add responses swagger can tell you if you do it right 👍 * use ctx.InternalServerError * Revert "use numbers and not http.Status___ enum" This reverts commit b1ff386. * use http.Status* enum everywhere
1 parent 050a8af commit 2848c5e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1262
-648
lines changed

modules/context/api.go

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ type APIValidationError struct {
3939
URL string `json:"url"`
4040
}
4141

42+
// APIInvalidTopicsError is error format response to invalid topics
43+
// swagger:response invalidTopicsError
44+
type APIInvalidTopicsError struct {
45+
Topics []string `json:"invalidTopics"`
46+
Message string `json:"message"`
47+
}
48+
4249
//APIEmpty is an empty response
4350
// swagger:response empty
4451
type APIEmpty struct{}

routers/api/v1/admin/org.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package admin
77

88
import (
9+
"net/http"
10+
911
"code.gitea.io/gitea/models"
1012
"code.gitea.io/gitea/modules/context"
1113
"code.gitea.io/gitea/modules/convert"
@@ -39,6 +41,7 @@ func CreateOrg(ctx *context.APIContext, form api.CreateOrgOption) {
3941
// "$ref": "#/responses/forbidden"
4042
// "422":
4143
// "$ref": "#/responses/validationError"
44+
4245
u := user.GetUserByParams(ctx)
4346
if ctx.Written() {
4447
return
@@ -64,14 +67,14 @@ func CreateOrg(ctx *context.APIContext, form api.CreateOrgOption) {
6467
if models.IsErrUserAlreadyExist(err) ||
6568
models.IsErrNameReserved(err) ||
6669
models.IsErrNamePatternNotAllowed(err) {
67-
ctx.Error(422, "", err)
70+
ctx.Error(http.StatusUnprocessableEntity, "", err)
6871
} else {
69-
ctx.Error(500, "CreateOrganization", err)
72+
ctx.Error(http.StatusInternalServerError, "CreateOrganization", err)
7073
}
7174
return
7275
}
7376

74-
ctx.JSON(201, convert.ToOrganization(org))
77+
ctx.JSON(http.StatusCreated, convert.ToOrganization(org))
7578
}
7679

7780
//GetAllOrgs API for getting information of all the organizations
@@ -95,6 +98,7 @@ func GetAllOrgs(ctx *context.APIContext) {
9598
// "$ref": "#/responses/OrganizationList"
9699
// "403":
97100
// "$ref": "#/responses/forbidden"
101+
98102
users, _, err := models.SearchUsers(&models.SearchUserOptions{
99103
Type: models.UserTypeOrganization,
100104
OrderBy: models.SearchOrderByAlphabetically,
@@ -103,12 +107,12 @@ func GetAllOrgs(ctx *context.APIContext) {
103107
Private: true,
104108
})
105109
if err != nil {
106-
ctx.Error(500, "SearchOrganizations", err)
110+
ctx.Error(http.StatusInternalServerError, "SearchOrganizations", err)
107111
return
108112
}
109113
orgs := make([]*api.Organization, len(users))
110114
for i := range users {
111115
orgs[i] = convert.ToOrganization(users[i])
112116
}
113-
ctx.JSON(200, &orgs)
117+
ctx.JSON(http.StatusOK, &orgs)
114118
}

routers/api/v1/admin/repo.go

+5
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ func CreateRepo(ctx *context.APIContext, form api.CreateRepoOption) {
3535
// "$ref": "#/responses/Repository"
3636
// "403":
3737
// "$ref": "#/responses/forbidden"
38+
// "404":
39+
// "$ref": "#/responses/notFound"
40+
// "409":
41+
// "$ref": "#/responses/error"
3842
// "422":
3943
// "$ref": "#/responses/validationError"
44+
4045
owner := user.GetUserByParams(ctx)
4146
if ctx.Written() {
4247
return

routers/api/v1/admin/user.go

+28-19
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package admin
77

88
import (
99
"errors"
10+
"net/http"
1011

1112
"code.gitea.io/gitea/models"
1213
"code.gitea.io/gitea/modules/context"
@@ -26,9 +27,9 @@ func parseLoginSource(ctx *context.APIContext, u *models.User, sourceID int64, l
2627
source, err := models.GetLoginSourceByID(sourceID)
2728
if err != nil {
2829
if models.IsErrLoginSourceNotExist(err) {
29-
ctx.Error(422, "", err)
30+
ctx.Error(http.StatusUnprocessableEntity, "", err)
3031
} else {
31-
ctx.Error(500, "GetLoginSourceByID", err)
32+
ctx.Error(http.StatusInternalServerError, "GetLoginSourceByID", err)
3233
}
3334
return
3435
}
@@ -57,8 +58,11 @@ func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
5758
// "$ref": "#/responses/User"
5859
// "403":
5960
// "$ref": "#/responses/forbidden"
61+
// "400":
62+
// "$ref": "#/responses/error"
6063
// "422":
6164
// "$ref": "#/responses/validationError"
65+
6266
u := &models.User{
6367
Name: form.Username,
6468
FullName: form.FullName,
@@ -78,17 +82,17 @@ func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
7882
}
7983
if !password.IsComplexEnough(form.Password) {
8084
err := errors.New("PasswordComplexity")
81-
ctx.Error(400, "PasswordComplexity", err)
85+
ctx.Error(http.StatusBadRequest, "PasswordComplexity", err)
8286
return
8387
}
8488
if err := models.CreateUser(u); err != nil {
8589
if models.IsErrUserAlreadyExist(err) ||
8690
models.IsErrEmailAlreadyUsed(err) ||
8791
models.IsErrNameReserved(err) ||
8892
models.IsErrNamePatternNotAllowed(err) {
89-
ctx.Error(422, "", err)
93+
ctx.Error(http.StatusUnprocessableEntity, "", err)
9094
} else {
91-
ctx.Error(500, "CreateUser", err)
95+
ctx.Error(http.StatusInternalServerError, "CreateUser", err)
9296
}
9397
return
9498
}
@@ -98,7 +102,7 @@ func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
98102
if form.SendNotify {
99103
mailer.SendRegisterNotifyMail(ctx.Locale, u)
100104
}
101-
ctx.JSON(201, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin))
105+
ctx.JSON(http.StatusCreated, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin))
102106
}
103107

104108
// EditUser api for modifying a user's information
@@ -127,6 +131,7 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) {
127131
// "$ref": "#/responses/forbidden"
128132
// "422":
129133
// "$ref": "#/responses/validationError"
134+
130135
u := user.GetUserByParams(ctx)
131136
if ctx.Written() {
132137
return
@@ -140,12 +145,12 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) {
140145
if len(form.Password) > 0 {
141146
if !password.IsComplexEnough(form.Password) {
142147
err := errors.New("PasswordComplexity")
143-
ctx.Error(400, "PasswordComplexity", err)
148+
ctx.Error(http.StatusBadRequest, "PasswordComplexity", err)
144149
return
145150
}
146151
var err error
147152
if u.Salt, err = models.GetUserSalt(); err != nil {
148-
ctx.Error(500, "UpdateUser", err)
153+
ctx.Error(http.StatusInternalServerError, "UpdateUser", err)
149154
return
150155
}
151156
u.HashPassword(form.Password)
@@ -184,15 +189,15 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) {
184189

185190
if err := models.UpdateUser(u); err != nil {
186191
if models.IsErrEmailAlreadyUsed(err) {
187-
ctx.Error(422, "", err)
192+
ctx.Error(http.StatusUnprocessableEntity, "", err)
188193
} else {
189-
ctx.Error(500, "UpdateUser", err)
194+
ctx.Error(http.StatusInternalServerError, "UpdateUser", err)
190195
}
191196
return
192197
}
193198
log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name)
194199

195-
ctx.JSON(200, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin))
200+
ctx.JSON(http.StatusOK, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin))
196201
}
197202

198203
// DeleteUser api for deleting a user
@@ -215,6 +220,7 @@ func DeleteUser(ctx *context.APIContext) {
215220
// "$ref": "#/responses/forbidden"
216221
// "422":
217222
// "$ref": "#/responses/validationError"
223+
218224
u := user.GetUserByParams(ctx)
219225
if ctx.Written() {
220226
return
@@ -223,15 +229,15 @@ func DeleteUser(ctx *context.APIContext) {
223229
if err := models.DeleteUser(u); err != nil {
224230
if models.IsErrUserOwnRepos(err) ||
225231
models.IsErrUserHasOrgs(err) {
226-
ctx.Error(422, "", err)
232+
ctx.Error(http.StatusUnprocessableEntity, "", err)
227233
} else {
228-
ctx.Error(500, "DeleteUser", err)
234+
ctx.Error(http.StatusInternalServerError, "DeleteUser", err)
229235
}
230236
return
231237
}
232238
log.Trace("Account deleted by admin(%s): %s", ctx.User.Name, u.Name)
233239

234-
ctx.Status(204)
240+
ctx.Status(http.StatusNoContent)
235241
}
236242

237243
// CreatePublicKey api for creating a public key to a user
@@ -260,6 +266,7 @@ func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) {
260266
// "$ref": "#/responses/forbidden"
261267
// "422":
262268
// "$ref": "#/responses/validationError"
269+
263270
u := user.GetUserByParams(ctx)
264271
if ctx.Written() {
265272
return
@@ -293,6 +300,7 @@ func DeleteUserPublicKey(ctx *context.APIContext) {
293300
// "$ref": "#/responses/forbidden"
294301
// "404":
295302
// "$ref": "#/responses/notFound"
303+
296304
u := user.GetUserByParams(ctx)
297305
if ctx.Written() {
298306
return
@@ -302,15 +310,15 @@ func DeleteUserPublicKey(ctx *context.APIContext) {
302310
if models.IsErrKeyNotExist(err) {
303311
ctx.NotFound()
304312
} else if models.IsErrKeyAccessDenied(err) {
305-
ctx.Error(403, "", "You do not have access to this key")
313+
ctx.Error(http.StatusForbidden, "", "You do not have access to this key")
306314
} else {
307-
ctx.Error(500, "DeleteUserPublicKey", err)
315+
ctx.Error(http.StatusInternalServerError, "DeleteUserPublicKey", err)
308316
}
309317
return
310318
}
311319
log.Trace("Key deleted by admin(%s): %s", ctx.User.Name, u.Name)
312320

313-
ctx.Status(204)
321+
ctx.Status(http.StatusNoContent)
314322
}
315323

316324
//GetAllUsers API for getting information of all the users
@@ -325,13 +333,14 @@ func GetAllUsers(ctx *context.APIContext) {
325333
// "$ref": "#/responses/UserList"
326334
// "403":
327335
// "$ref": "#/responses/forbidden"
336+
328337
users, _, err := models.SearchUsers(&models.SearchUserOptions{
329338
Type: models.UserTypeIndividual,
330339
OrderBy: models.SearchOrderByAlphabetically,
331340
PageSize: -1,
332341
})
333342
if err != nil {
334-
ctx.Error(500, "GetAllUsers", err)
343+
ctx.Error(http.StatusInternalServerError, "GetAllUsers", err)
335344
return
336345
}
337346

@@ -340,5 +349,5 @@ func GetAllUsers(ctx *context.APIContext) {
340349
results[i] = convert.ToUser(users[i], ctx.IsSigned, ctx.User.IsAdmin)
341350
}
342351

343-
ctx.JSON(200, &results)
352+
ctx.JSON(http.StatusOK, &results)
344353
}

0 commit comments

Comments
 (0)