mirror of
https://github.com/kataras/iris.git
synced 2025-03-14 08:26:26 +01:00
next version preparation: hero, mvc: fix payload binding
Former-commit-id: d95f750dd9e1532c9ac0d30a85b383d60acb3178
This commit is contained in:
parent
409f83ca66
commit
5ee06f9a92
|
@ -227,7 +227,7 @@ func (m *MyController) MyCustomHandler(id int64) string { return "MyCustomHandle
|
|||
|
||||
如果是 `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。
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris/v12/context"
|
||||
)
|
||||
|
@ -324,23 +325,32 @@ func payloadBinding(index int, typ reflect.Type) *binding {
|
|||
newValue = reflect.New(indirectType(input.Type))
|
||||
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:
|
||||
err = ctx.ReadXML(ptr)
|
||||
case context.ContentYAMLHeaderValue:
|
||||
err = ctx.ReadYAML(ptr)
|
||||
case context.ContentFormHeaderValue:
|
||||
err = ctx.ReadQuery(ptr)
|
||||
case context.ContentFormMultipartHeaderValue:
|
||||
case context.ContentFormHeaderValue, context.ContentFormMultipartHeaderValue:
|
||||
err = ctx.ReadForm(ptr)
|
||||
default:
|
||||
case context.ContentJSONHeaderValue:
|
||||
err = ctx.ReadJSON(ptr)
|
||||
default:
|
||||
if ctx.Request().URL.RawQuery != "" {
|
||||
// try read from query.
|
||||
err = ctx.ReadQuery(ptr)
|
||||
} else {
|
||||
// otherwise default to JSON.
|
||||
err = ctx.ReadJSON(ptr)
|
||||
// json
|
||||
}
|
||||
|
||||
// if err != nil {
|
||||
// return emptyValue, err
|
||||
// }
|
||||
}
|
||||
|
||||
if !wasPtr {
|
||||
newValue = newValue.Elem()
|
||||
|
|
|
@ -11,8 +11,8 @@ import (
|
|||
|
||||
// dynamic func
|
||||
type testUserStruct struct {
|
||||
ID int64 `json:"id"`
|
||||
Username string `json:"username"`
|
||||
ID int64 `json:"id" form:"id" url:"id"`
|
||||
Username string `json:"username" form:"username" url:"username"`
|
||||
}
|
||||
|
||||
func testBinderFunc(ctx iris.Context) testUserStruct {
|
||||
|
@ -142,8 +142,18 @@ func TestPayloadBinding(t *testing.T) {
|
|||
app.Post("/2", postHandler2)
|
||||
|
||||
e := httptest.New(t, app)
|
||||
|
||||
// JSON
|
||||
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")
|
||||
|
||||
// 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:
|
||||
|
|
Loading…
Reference in New Issue
Block a user