Skip to content

Commit

Permalink
docfix #1566
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Jul 24, 2020
1 parent 18a8d13 commit 75d7fe7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
12 changes: 10 additions & 2 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -1909,8 +1909,6 @@ func (ctx *Context) ReadForm(formObject interface{}) error {
}

// ReadQuery binds url query to "ptr". The struct field tag is "url".
// If a client sent an unknown field, this method will return an error,
// in order to ignore that error use the `err != nil && !iris.IsErrPath(err)`.
//
// Example: https://github.com/kataras/iris/blob/master/_examples/request-body/read-query/main.go
func (ctx *Context) ReadQuery(ptr interface{}) error {
Expand Down Expand Up @@ -1980,6 +1978,16 @@ func (ctx *Context) ReadMsgPack(ptr interface{}) error {
// JSON, Protobuf, MsgPack, XML, YAML, MultipartForm and binds the result to the "ptr".
func (ctx *Context) ReadBody(ptr interface{}) error {
if ctx.Method() == http.MethodGet {
if ctx.Request().URL.RawQuery != "" {
// try read from query.
return ctx.ReadQuery(ptr)
}

// otherwise use the ReadForm,
// it's actually the same except
// ReadQuery will not fire errors on:
// 1. unknown or empty url query parameters
// 2. empty query or form (if FireEmptyFormError is enabled).
return ctx.ReadForm(ptr)
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/iris-contrib/httpexpect/v2 v2.0.5
github.com/iris-contrib/jade v1.1.4
github.com/iris-contrib/pongo2 v0.0.1
github.com/iris-contrib/schema v0.0.1
github.com/iris-contrib/schema v0.0.2
github.com/json-iterator/go v1.1.10
github.com/kataras/golog v0.0.18
github.com/kataras/neffos v0.0.16
Expand Down
10 changes: 7 additions & 3 deletions hero/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,10 @@ func getBindingsForStruct(v reflect.Value, dependencies []*Dependency, paramsCou
}

exportedBindings := getBindingsFor(inputs, dependencies, paramsCount)
// fmt.Printf("Controller [%s] | Inputs length: %d vs Bindings length: %d | Stateless : %d\n", typ, n, len(exportedBindings), stateless)
// fmt.Printf("Controller [%s] | Inputs length: %d vs Bindings length: %d | NonZero: %d | Stateless : %d\n",
// typ, n, len(exportedBindings), len(nonZero), stateless)
// for i, b := range exportedBindings {
// fmt.Printf("[%d] [Static=%v] %s\n", i, b.Dependency.Static, b.Dependency.DestType)
// fmt.Printf("[%d] [Static=%v] %#+v\n", i, b.Dependency.Static, b.Dependency.OriginalValue)
// }

if stateless == 0 && len(nonZero) >= len(exportedBindings) {
Expand Down Expand Up @@ -332,8 +333,11 @@ func paramDependencyHandler(paramIndex int) DependencyHandler {
}

// registered if input parameters are more than matched dependencies.
// It binds an input to a request body based on the request content-type header (JSON, XML, YAML, Query, Form).
// It binds an input to a request body based on the request content-type header
// (JSON, Protobuf, Msgpack, XML, YAML, Query, Form).
func payloadBinding(index int, typ reflect.Type) *binding {
// fmt.Printf("Register payload binding for index: %d and type: %s\n", index, typ.String())

return &binding{
Dependency: &Dependency{
Handle: func(ctx *context.Context, input *Input) (newValue reflect.Value, err error) {
Expand Down
6 changes: 5 additions & 1 deletion mvc/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,11 @@ func (c *ControllerActivator) handlerOf(relPath, methodName string) context.Hand
// c.injector.Container.GetErrorHandler(ctx).HandleError(ctx, err)
// }
c.injector.Container.GetErrorHandler(ctx).HandleError(ctx, err)
return
// allow skipping struct field bindings
// errors by a custom error handler.
if ctx.IsStopped() {
return
}
}

b := ctrl.Interface().(BaseController)
Expand Down

0 comments on commit 75d7fe7

Please sign in to comment.