next version preparation: hero, mvc: fix payload binding

Former-commit-id: d95f750dd9e1532c9ac0d30a85b383d60acb3178
This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-03-04 10:29:34 +02:00
parent 409f83ca66
commit 5ee06f9a92
3 changed files with 33 additions and 13 deletions

View File

@ -227,7 +227,7 @@ func (m *MyController) MyCustomHandler(id int64) string { return "MyCustomHandle
如果是 `mvc.New(app.Party("/assets")).Handle(new(file.Controller))` 如果是 `mvc.New(app.Party("/assets")).Handle(new(file.Controller))`
- `func(*Controller) GetByWildard(path string)` - `GET:/assets/{param:path}` - `func(*Controller) GetByWildcard(path string)` - `GET:/assets/{param:path}`
支持的函数接收者类型是int, int64, bool and string。 支持的函数接收者类型是int, int64, bool and string。

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"sort" "sort"
"strings"
"github.com/kataras/iris/v12/context" "github.com/kataras/iris/v12/context"
) )
@ -324,24 +325,33 @@ func payloadBinding(index int, typ reflect.Type) *binding {
newValue = reflect.New(indirectType(input.Type)) newValue = reflect.New(indirectType(input.Type))
ptr := newValue.Interface() ptr := newValue.Interface()
switch ctx.GetContentTypeRequested() { contentType := ctx.GetContentTypeRequested()
if contentType != "" {
if idx := strings.IndexByte(contentType, ';'); idx > 0 {
// e.g. contentType=[multipart/form-data] trailing: ; boundary=4e2946168dbbac
contentType = contentType[:idx]
}
}
switch contentType {
case context.ContentXMLHeaderValue: case context.ContentXMLHeaderValue:
err = ctx.ReadXML(ptr) err = ctx.ReadXML(ptr)
case context.ContentYAMLHeaderValue: case context.ContentYAMLHeaderValue:
err = ctx.ReadYAML(ptr) err = ctx.ReadYAML(ptr)
case context.ContentFormHeaderValue: case context.ContentFormHeaderValue, context.ContentFormMultipartHeaderValue:
err = ctx.ReadQuery(ptr)
case context.ContentFormMultipartHeaderValue:
err = ctx.ReadForm(ptr) err = ctx.ReadForm(ptr)
default: case context.ContentJSONHeaderValue:
err = ctx.ReadJSON(ptr) err = ctx.ReadJSON(ptr)
// json default:
if ctx.Request().URL.RawQuery != "" {
// try read from query.
err = ctx.ReadQuery(ptr)
} else {
// otherwise default to JSON.
err = ctx.ReadJSON(ptr)
}
} }
// if err != nil {
// return emptyValue, err
// }
if !wasPtr { if !wasPtr {
newValue = newValue.Elem() newValue = newValue.Elem()
} }

View File

@ -11,8 +11,8 @@ import (
// dynamic func // dynamic func
type testUserStruct struct { type testUserStruct struct {
ID int64 `json:"id"` ID int64 `json:"id" form:"id" url:"id"`
Username string `json:"username"` Username string `json:"username" form:"username" url:"username"`
} }
func testBinderFunc(ctx iris.Context) testUserStruct { func testBinderFunc(ctx iris.Context) testUserStruct {
@ -142,8 +142,18 @@ func TestPayloadBinding(t *testing.T) {
app.Post("/2", postHandler2) app.Post("/2", postHandler2)
e := httptest.New(t, app) e := httptest.New(t, app)
// JSON
e.POST("/").WithJSON(iris.Map{"username": "makis"}).Expect().Status(httptest.StatusOK).Body().Equal("makis") e.POST("/").WithJSON(iris.Map{"username": "makis"}).Expect().Status(httptest.StatusOK).Body().Equal("makis")
e.POST("/2").WithJSON(iris.Map{"username": "kataras"}).Expect().Status(httptest.StatusOK).Body().Equal("kataras") e.POST("/2").WithJSON(iris.Map{"username": "kataras"}).Expect().Status(httptest.StatusOK).Body().Equal("kataras")
// FORM (url-encoded)
e.POST("/").WithFormField("username", "makis").Expect().Status(httptest.StatusOK).Body().Equal("makis")
// FORM (multipart)
e.POST("/").WithMultipart().WithFormField("username", "makis").Expect().Status(httptest.StatusOK).Body().Equal("makis")
// URL query.
e.POST("/").WithQuery("username", "makis").Expect().Status(httptest.StatusOK).Body().Equal("makis")
} }
/* Author's notes: /* Author's notes: