Skip to content

Commit 5ee06f9

Browse files
committed
next version preparation: hero, mvc: fix payload binding
Former-commit-id: d95f750dd9e1532c9ac0d30a85b383d60acb3178
1 parent 409f83c commit 5ee06f9

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

_examples/README_ZH.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func (m *MyController) MyCustomHandler(id int64) string { return "MyCustomHandle
227227

228228
如果是 `mvc.New(app.Party("/assets")).Handle(new(file.Controller))`
229229

230-
- `func(*Controller) GetByWildard(path string)` - `GET:/assets/{param:path}`
230+
- `func(*Controller) GetByWildcard(path string)` - `GET:/assets/{param:path}`
231231

232232
支持的函数接收者类型是:int, int64, bool and string。
233233

hero/binding.go

+20-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"reflect"
66
"sort"
7+
"strings"
78

89
"github.com/kataras/iris/v12/context"
910
)
@@ -324,24 +325,33 @@ func payloadBinding(index int, typ reflect.Type) *binding {
324325
newValue = reflect.New(indirectType(input.Type))
325326
ptr := newValue.Interface()
326327

327-
switch ctx.GetContentTypeRequested() {
328+
contentType := ctx.GetContentTypeRequested()
329+
if contentType != "" {
330+
if idx := strings.IndexByte(contentType, ';'); idx > 0 {
331+
// e.g. contentType=[multipart/form-data] trailing: ; boundary=4e2946168dbbac
332+
contentType = contentType[:idx]
333+
}
334+
}
335+
336+
switch contentType {
328337
case context.ContentXMLHeaderValue:
329338
err = ctx.ReadXML(ptr)
330339
case context.ContentYAMLHeaderValue:
331340
err = ctx.ReadYAML(ptr)
332-
case context.ContentFormHeaderValue:
333-
err = ctx.ReadQuery(ptr)
334-
case context.ContentFormMultipartHeaderValue:
341+
case context.ContentFormHeaderValue, context.ContentFormMultipartHeaderValue:
335342
err = ctx.ReadForm(ptr)
336-
default:
343+
case context.ContentJSONHeaderValue:
337344
err = ctx.ReadJSON(ptr)
338-
// json
345+
default:
346+
if ctx.Request().URL.RawQuery != "" {
347+
// try read from query.
348+
err = ctx.ReadQuery(ptr)
349+
} else {
350+
// otherwise default to JSON.
351+
err = ctx.ReadJSON(ptr)
352+
}
339353
}
340354

341-
// if err != nil {
342-
// return emptyValue, err
343-
// }
344-
345355
if !wasPtr {
346356
newValue = newValue.Elem()
347357
}

hero/handler_test.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111

1212
// dynamic func
1313
type testUserStruct struct {
14-
ID int64 `json:"id"`
15-
Username string `json:"username"`
14+
ID int64 `json:"id" form:"id" url:"id"`
15+
Username string `json:"username" form:"username" url:"username"`
1616
}
1717

1818
func testBinderFunc(ctx iris.Context) testUserStruct {
@@ -142,8 +142,18 @@ func TestPayloadBinding(t *testing.T) {
142142
app.Post("/2", postHandler2)
143143

144144
e := httptest.New(t, app)
145+
146+
// JSON
145147
e.POST("/").WithJSON(iris.Map{"username": "makis"}).Expect().Status(httptest.StatusOK).Body().Equal("makis")
146148
e.POST("/2").WithJSON(iris.Map{"username": "kataras"}).Expect().Status(httptest.StatusOK).Body().Equal("kataras")
149+
150+
// FORM (url-encoded)
151+
e.POST("/").WithFormField("username", "makis").Expect().Status(httptest.StatusOK).Body().Equal("makis")
152+
// FORM (multipart)
153+
e.POST("/").WithMultipart().WithFormField("username", "makis").Expect().Status(httptest.StatusOK).Body().Equal("makis")
154+
155+
// URL query.
156+
e.POST("/").WithQuery("username", "makis").Expect().Status(httptest.StatusOK).Body().Equal("makis")
147157
}
148158

149159
/* Author's notes:

0 commit comments

Comments
 (0)