mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-09 11:43:10 +02:00
refactor: http request binding (#38971)
Better than before, still not good enough (more work can be done in the future) And add the missing error handling in the PrivateContext "bind" middleware. By the way, picked some "TrimSpace" changes from "fix: trim whitespace from SMTP address and port - #38934" (fix #38926)
This commit is contained in:
1 parent
6c425fae6e
commit
6904f6480c
21 files changed
+265
-419
No files matched your search
@@ -79,7 +79,9 @@ import (
|
||||
"gitea.dev/modules/setting"
|
||||
api "gitea.dev/modules/structs"
|
||||
"gitea.dev/modules/util"
|
||||
"gitea.dev/modules/validation"
|
||||
"gitea.dev/modules/web"
|
||||
"gitea.dev/modules/web/middleware"
|
||||
"gitea.dev/routers/api/v1/activitypub"
|
||||
"gitea.dev/routers/api/v1/admin"
|
||||
"gitea.dev/routers/api/v1/misc"
|
||||
@@ -99,7 +101,6 @@ import (
|
||||
|
||||
_ "gitea.dev/routers/api/v1/swagger" // for swagger generation
|
||||
|
||||
"gitea.com/go-chi/binding"
|
||||
chi_middleware "github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/go-chi/cors"
|
||||
)
|
||||
@@ -884,15 +885,14 @@ func mustEnableAttachments(ctx *context.APIContext) {
|
||||
}
|
||||
|
||||
// bind binding an obj to a func(ctx *context.APIContext)
|
||||
func bind[T any](_ T) any {
|
||||
func bind[T any](tmpl T) any {
|
||||
return func(ctx *context.APIContext) {
|
||||
theObj := new(T) // create a new form obj for every request but not use obj directly
|
||||
errs := binding.Bind(ctx.Req, theObj)
|
||||
form, errs := middleware.BindFormAny(ctx.Req, validation.Binder(), tmpl)
|
||||
if len(errs) > 0 {
|
||||
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("%s: %s", errs[0].FieldNames, errs[0].Error()))
|
||||
return
|
||||
}
|
||||
web.SetForm(ctx, theObj)
|
||||
web.SetForm(ctx, form)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user