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 b1ff386e2418ed6a7f183e756b13277d701278ef.

* use http.Status* enum everywhere
This commit is contained in:
6543 2019-12-20 18:07:12 +01:00 committed by Lauris BH
parent 050a8af424
commit 2848c5eb8f
52 changed files with 1262 additions and 648 deletions

View file

@ -7,6 +7,7 @@ package admin
import (
"errors"
"net/http"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
@ -26,9 +27,9 @@ func parseLoginSource(ctx *context.APIContext, u *models.User, sourceID int64, l
source, err := models.GetLoginSourceByID(sourceID)
if err != nil {
if models.IsErrLoginSourceNotExist(err) {
ctx.Error(422, "", err)
ctx.Error(http.StatusUnprocessableEntity, "", err)
} else {
ctx.Error(500, "GetLoginSourceByID", err)
ctx.Error(http.StatusInternalServerError, "GetLoginSourceByID", err)
}
return
}
@ -57,8 +58,11 @@ func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
// "$ref": "#/responses/User"
// "403":
// "$ref": "#/responses/forbidden"
// "400":
// "$ref": "#/responses/error"
// "422":
// "$ref": "#/responses/validationError"
u := &models.User{
Name: form.Username,
FullName: form.FullName,
@ -78,7 +82,7 @@ func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
}
if !password.IsComplexEnough(form.Password) {
err := errors.New("PasswordComplexity")
ctx.Error(400, "PasswordComplexity", err)
ctx.Error(http.StatusBadRequest, "PasswordComplexity", err)
return
}
if err := models.CreateUser(u); err != nil {
@ -86,9 +90,9 @@ func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
models.IsErrEmailAlreadyUsed(err) ||
models.IsErrNameReserved(err) ||
models.IsErrNamePatternNotAllowed(err) {
ctx.Error(422, "", err)
ctx.Error(http.StatusUnprocessableEntity, "", err)
} else {
ctx.Error(500, "CreateUser", err)
ctx.Error(http.StatusInternalServerError, "CreateUser", err)
}
return
}
@ -98,7 +102,7 @@ func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
if form.SendNotify {
mailer.SendRegisterNotifyMail(ctx.Locale, u)
}
ctx.JSON(201, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin))
ctx.JSON(http.StatusCreated, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin))
}
// EditUser api for modifying a user's information
@ -127,6 +131,7 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) {
// "$ref": "#/responses/forbidden"
// "422":
// "$ref": "#/responses/validationError"
u := user.GetUserByParams(ctx)
if ctx.Written() {
return
@ -140,12 +145,12 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) {
if len(form.Password) > 0 {
if !password.IsComplexEnough(form.Password) {
err := errors.New("PasswordComplexity")
ctx.Error(400, "PasswordComplexity", err)
ctx.Error(http.StatusBadRequest, "PasswordComplexity", err)
return
}
var err error
if u.Salt, err = models.GetUserSalt(); err != nil {
ctx.Error(500, "UpdateUser", err)
ctx.Error(http.StatusInternalServerError, "UpdateUser", err)
return
}
u.HashPassword(form.Password)
@ -184,15 +189,15 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) {
if err := models.UpdateUser(u); err != nil {
if models.IsErrEmailAlreadyUsed(err) {
ctx.Error(422, "", err)
ctx.Error(http.StatusUnprocessableEntity, "", err)
} else {
ctx.Error(500, "UpdateUser", err)
ctx.Error(http.StatusInternalServerError, "UpdateUser", err)
}
return
}
log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name)
ctx.JSON(200, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin))
ctx.JSON(http.StatusOK, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin))
}
// DeleteUser api for deleting a user
@ -215,6 +220,7 @@ func DeleteUser(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"
// "422":
// "$ref": "#/responses/validationError"
u := user.GetUserByParams(ctx)
if ctx.Written() {
return
@ -223,15 +229,15 @@ func DeleteUser(ctx *context.APIContext) {
if err := models.DeleteUser(u); err != nil {
if models.IsErrUserOwnRepos(err) ||
models.IsErrUserHasOrgs(err) {
ctx.Error(422, "", err)
ctx.Error(http.StatusUnprocessableEntity, "", err)
} else {
ctx.Error(500, "DeleteUser", err)
ctx.Error(http.StatusInternalServerError, "DeleteUser", err)
}
return
}
log.Trace("Account deleted by admin(%s): %s", ctx.User.Name, u.Name)
ctx.Status(204)
ctx.Status(http.StatusNoContent)
}
// CreatePublicKey api for creating a public key to a user
@ -260,6 +266,7 @@ func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) {
// "$ref": "#/responses/forbidden"
// "422":
// "$ref": "#/responses/validationError"
u := user.GetUserByParams(ctx)
if ctx.Written() {
return
@ -293,6 +300,7 @@ func DeleteUserPublicKey(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"
// "404":
// "$ref": "#/responses/notFound"
u := user.GetUserByParams(ctx)
if ctx.Written() {
return
@ -302,15 +310,15 @@ func DeleteUserPublicKey(ctx *context.APIContext) {
if models.IsErrKeyNotExist(err) {
ctx.NotFound()
} else if models.IsErrKeyAccessDenied(err) {
ctx.Error(403, "", "You do not have access to this key")
ctx.Error(http.StatusForbidden, "", "You do not have access to this key")
} else {
ctx.Error(500, "DeleteUserPublicKey", err)
ctx.Error(http.StatusInternalServerError, "DeleteUserPublicKey", err)
}
return
}
log.Trace("Key deleted by admin(%s): %s", ctx.User.Name, u.Name)
ctx.Status(204)
ctx.Status(http.StatusNoContent)
}
//GetAllUsers API for getting information of all the users
@ -325,13 +333,14 @@ func GetAllUsers(ctx *context.APIContext) {
// "$ref": "#/responses/UserList"
// "403":
// "$ref": "#/responses/forbidden"
users, _, err := models.SearchUsers(&models.SearchUserOptions{
Type: models.UserTypeIndividual,
OrderBy: models.SearchOrderByAlphabetically,
PageSize: -1,
})
if err != nil {
ctx.Error(500, "GetAllUsers", err)
ctx.Error(http.StatusInternalServerError, "GetAllUsers", err)
return
}
@ -340,5 +349,5 @@ func GetAllUsers(ctx *context.APIContext) {
results[i] = convert.ToUser(users[i], ctx.IsSigned, ctx.User.IsAdmin)
}
ctx.JSON(200, &results)
ctx.JSON(http.StatusOK, &results)
}