mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-14 05:52:43 +00:00
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 b1ff386e24
.
* use http.Status* enum everywhere
This commit is contained in:
parent
050a8af424
commit
2848c5eb8f
52 changed files with 1262 additions and 648 deletions
|
@ -42,9 +42,7 @@ func ListTopics(ctx *context.APIContext) {
|
|||
})
|
||||
if err != nil {
|
||||
log.Error("ListTopics failed: %v", err)
|
||||
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
|
||||
"message": "ListTopics failed.",
|
||||
})
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -82,6 +80,8 @@ func UpdateTopics(ctx *context.APIContext, form api.RepoTopicOptions) {
|
|||
// responses:
|
||||
// "204":
|
||||
// "$ref": "#/responses/empty"
|
||||
// "422":
|
||||
// "$ref": "#/responses/invalidTopicsError"
|
||||
|
||||
topicNames := form.Topics
|
||||
validTopics, invalidTopics := models.SanitizeAndValidateTopics(topicNames)
|
||||
|
@ -105,9 +105,7 @@ func UpdateTopics(ctx *context.APIContext, form api.RepoTopicOptions) {
|
|||
err := models.SaveTopics(ctx.Repo.Repository.ID, validTopics...)
|
||||
if err != nil {
|
||||
log.Error("SaveTopics failed: %v", err)
|
||||
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
|
||||
"message": "Save topics failed.",
|
||||
})
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -140,11 +138,16 @@ func AddTopic(ctx *context.APIContext) {
|
|||
// responses:
|
||||
// "204":
|
||||
// "$ref": "#/responses/empty"
|
||||
// "422":
|
||||
// "$ref": "#/responses/invalidTopicsError"
|
||||
|
||||
topicName := strings.TrimSpace(strings.ToLower(ctx.Params(":topic")))
|
||||
|
||||
if !models.ValidateTopic(topicName) {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", "Topic name is invalid")
|
||||
ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
|
||||
"invalidTopics": topicName,
|
||||
"message": "Topic name is invalid",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -154,9 +157,7 @@ func AddTopic(ctx *context.APIContext) {
|
|||
})
|
||||
if err != nil {
|
||||
log.Error("AddTopic failed: %v", err)
|
||||
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
|
||||
"message": "ListTopics failed.",
|
||||
})
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
if len(topics) >= 25 {
|
||||
|
@ -169,9 +170,7 @@ func AddTopic(ctx *context.APIContext) {
|
|||
_, err = models.AddTopic(ctx.Repo.Repository.ID, topicName)
|
||||
if err != nil {
|
||||
log.Error("AddTopic failed: %v", err)
|
||||
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
|
||||
"message": "AddTopic failed.",
|
||||
})
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -204,19 +203,23 @@ func DeleteTopic(ctx *context.APIContext) {
|
|||
// responses:
|
||||
// "204":
|
||||
// "$ref": "#/responses/empty"
|
||||
// "422":
|
||||
// "$ref": "#/responses/invalidTopicsError"
|
||||
|
||||
topicName := strings.TrimSpace(strings.ToLower(ctx.Params(":topic")))
|
||||
|
||||
if !models.ValidateTopic(topicName) {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", "Topic name is invalid")
|
||||
ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
|
||||
"invalidTopics": topicName,
|
||||
"message": "Topic name is invalid",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
topic, err := models.DeleteTopic(ctx.Repo.Repository.ID, topicName)
|
||||
if err != nil {
|
||||
log.Error("DeleteTopic failed: %v", err)
|
||||
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
|
||||
"message": "DeleteTopic failed.",
|
||||
})
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -243,10 +246,11 @@ func TopicSearch(ctx *context.Context) {
|
|||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/TopicListResponse"
|
||||
// "403":
|
||||
// "$ref": "#/responses/forbidden"
|
||||
|
||||
if ctx.User == nil {
|
||||
ctx.JSON(http.StatusForbidden, map[string]interface{}{
|
||||
"message": "Only owners could change the topics.",
|
||||
})
|
||||
ctx.Error(http.StatusForbidden, "UserIsNil", "Only owners could change the topics.")
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -258,9 +262,7 @@ func TopicSearch(ctx *context.Context) {
|
|||
})
|
||||
if err != nil {
|
||||
log.Error("SearchTopics failed: %v", err)
|
||||
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
|
||||
"message": "Search topics failed.",
|
||||
})
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue