Skip to content

Commit

Permalink
Swagger info corrections (#9441)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
6543 authored and lafriks committed Dec 20, 2019
1 parent 050a8af commit 2848c5e
Show file tree
Hide file tree
Showing 52 changed files with 1,262 additions and 648 deletions.
7 changes: 7 additions & 0 deletions modules/context/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ type APIValidationError struct {
URL string `json:"url"`
}

// APIInvalidTopicsError is error format response to invalid topics
// swagger:response invalidTopicsError
type APIInvalidTopicsError struct {
Topics []string `json:"invalidTopics"`
Message string `json:"message"`
}

//APIEmpty is an empty response
// swagger:response empty
type APIEmpty struct{}
Expand Down
14 changes: 9 additions & 5 deletions routers/api/v1/admin/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package admin

import (
"net/http"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
Expand Down Expand Up @@ -39,6 +41,7 @@ func CreateOrg(ctx *context.APIContext, form api.CreateOrgOption) {
// "$ref": "#/responses/forbidden"
// "422":
// "$ref": "#/responses/validationError"

u := user.GetUserByParams(ctx)
if ctx.Written() {
return
Expand All @@ -64,14 +67,14 @@ func CreateOrg(ctx *context.APIContext, form api.CreateOrgOption) {
if models.IsErrUserAlreadyExist(err) ||
models.IsErrNameReserved(err) ||
models.IsErrNamePatternNotAllowed(err) {
ctx.Error(422, "", err)
ctx.Error(http.StatusUnprocessableEntity, "", err)
} else {
ctx.Error(500, "CreateOrganization", err)
ctx.Error(http.StatusInternalServerError, "CreateOrganization", err)
}
return
}

ctx.JSON(201, convert.ToOrganization(org))
ctx.JSON(http.StatusCreated, convert.ToOrganization(org))
}

//GetAllOrgs API for getting information of all the organizations
Expand All @@ -95,6 +98,7 @@ func GetAllOrgs(ctx *context.APIContext) {
// "$ref": "#/responses/OrganizationList"
// "403":
// "$ref": "#/responses/forbidden"

users, _, err := models.SearchUsers(&models.SearchUserOptions{
Type: models.UserTypeOrganization,
OrderBy: models.SearchOrderByAlphabetically,
Expand All @@ -103,12 +107,12 @@ func GetAllOrgs(ctx *context.APIContext) {
Private: true,
})
if err != nil {
ctx.Error(500, "SearchOrganizations", err)
ctx.Error(http.StatusInternalServerError, "SearchOrganizations", err)
return
}
orgs := make([]*api.Organization, len(users))
for i := range users {
orgs[i] = convert.ToOrganization(users[i])
}
ctx.JSON(200, &orgs)
ctx.JSON(http.StatusOK, &orgs)
}
5 changes: 5 additions & 0 deletions routers/api/v1/admin/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ func CreateRepo(ctx *context.APIContext, form api.CreateRepoOption) {
// "$ref": "#/responses/Repository"
// "403":
// "$ref": "#/responses/forbidden"
// "404":
// "$ref": "#/responses/notFound"
// "409":
// "$ref": "#/responses/error"
// "422":
// "$ref": "#/responses/validationError"

owner := user.GetUserByParams(ctx)
if ctx.Written() {
return
Expand Down
47 changes: 28 additions & 19 deletions routers/api/v1/admin/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package admin

import (
"errors"
"net/http"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
Expand All @@ -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
}
Expand Down Expand Up @@ -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,
Expand All @@ -78,17 +82,17 @@ 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 {
if models.IsErrUserAlreadyExist(err) ||
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
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -215,6 +220,7 @@ func DeleteUser(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"
// "422":
// "$ref": "#/responses/validationError"

u := user.GetUserByParams(ctx)
if ctx.Written() {
return
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -293,6 +300,7 @@ func DeleteUserPublicKey(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"
// "404":
// "$ref": "#/responses/notFound"

u := user.GetUserByParams(ctx)
if ctx.Written() {
return
Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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)
}
Loading

0 comments on commit 2848c5e

Please sign in to comment.