From caac0480bae1379b6ca7c49ffab84297315644df Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Fri, 4 Jan 2019 11:40:55 +0200 Subject: [PATCH] context#ReadForm can skip unkown fields by iris/context.IsErrPath(err), fixes: https://github.com/kataras/iris/issues/1157 Former-commit-id: 5cc8e5a9d58071591154e988262b547653c34e36 --- _examples/http_request/read-form/main.go | 2 +- context/context.go | 11 +++++++---- go.mod | 2 +- go.sum | 4 ++-- iris.go | 6 ++++++ 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/_examples/http_request/read-form/main.go b/_examples/http_request/read-form/main.go index 7b619a7b..148bb195 100644 --- a/_examples/http_request/read-form/main.go +++ b/_examples/http_request/read-form/main.go @@ -27,7 +27,7 @@ func main() { app.Post("/form_action", func(ctx iris.Context) { visitor := Visitor{} err := ctx.ReadForm(&visitor) - if err != nil { + if err != nil && !iris.IsErrPath(err) /* see: https://github.com/kataras/iris/issues/1157 */ { ctx.StatusCode(iris.StatusInternalServerError) ctx.WriteString(err.Error()) } diff --git a/context/context.go b/context/context.go index 63b4d89d..fdb147b5 100644 --- a/context/context.go +++ b/context/context.go @@ -2286,9 +2286,12 @@ func (ctx *context) ReadXML(xmlObject interface{}) error { return ctx.UnmarshalBody(xmlObject, UnmarshalerFunc(xml.Unmarshal)) } -var ( - errReadBody = errors.New("while trying to read %s from the request body. Trace %s") -) +// IsErrPath can be used at `context#ReadForm`. +// It reports whether the incoming error is type of `formbinder.ErrPath`, +// which can be ignored when server allows unknown post values to be sent by the client. +// +// A shortcut for the `formbinder#IsErrPath`. +var IsErrPath = formbinder.IsErrPath // ReadForm binds the formObject with the form data // it supports any kind of type, including custom structs. @@ -2304,7 +2307,7 @@ func (ctx *context) ReadForm(formObject interface{}) error { // or dec := formbinder.NewDecoder(&formbinder.DecoderOptions{TagName: "form"}) // somewhere at the app level. I did change the tagName to "form" // inside its source code, so it's not needed for now. - return errReadBody.With(formbinder.Decode(values, formObject)) + return formbinder.Decode(values, formObject) } // +------------------------------------------------------------+ diff --git a/go.mod b/go.mod index f70ba2db..e5c854b2 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,7 @@ require ( github.com/hashicorp/go-version v1.0.0 github.com/imkira/go-interpol v1.1.0 // indirect github.com/iris-contrib/blackfriday v2.0.0+incompatible - github.com/iris-contrib/formBinder v0.0.0-20171010160137-ad9fb86c356f + github.com/iris-contrib/formBinder v0.0.0-20190104093907-fbd5963f41e1 github.com/iris-contrib/go.uuid v2.0.0+incompatible github.com/iris-contrib/httpexpect v0.0.0-20180314041918-ebe99fcebbce github.com/iris-contrib/i18n v0.0.0-20171121225848-987a633949d0 diff --git a/go.sum b/go.sum index 9270cd39..5c1b1605 100644 --- a/go.sum +++ b/go.sum @@ -29,8 +29,8 @@ github.com/hashicorp/go-version v1.0.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09 github.com/imkira/go-interpol v1.1.0/go.mod h1:z0h2/2T3XF8kyEPpRgJ3kmNv+C43p+I/CoI+jC3w2iA= github.com/iris-contrib/blackfriday v2.0.0+incompatible h1:o5sHQHHm0ToHUlAJSTjW9UWicjJSDDauOOQ2AHuIVp4= github.com/iris-contrib/blackfriday v2.0.0+incompatible/go.mod h1:UzZ2bDEoaSGPbkg6SAB4att1aAwTmVIx/5gCVqeyUdI= -github.com/iris-contrib/formBinder v0.0.0-20171010160137-ad9fb86c356f h1:WgD6cqCSncBgkftw34mSPlMKU5JgoruAomW/SJtRrGU= -github.com/iris-contrib/formBinder v0.0.0-20171010160137-ad9fb86c356f/go.mod h1:i8kTYUOEstd/S8TG0ChTXQdf4ermA/e8vJX0+QruD9w= +github.com/iris-contrib/formBinder v0.0.0-20190104093907-fbd5963f41e1 h1:7GsNnSLoVceNylMpwcfy5aFNz/S5/TV25crb34I5PEo= +github.com/iris-contrib/formBinder v0.0.0-20190104093907-fbd5963f41e1/go.mod h1:i8kTYUOEstd/S8TG0ChTXQdf4ermA/e8vJX0+QruD9w= github.com/iris-contrib/go.uuid v2.0.0+incompatible h1:XZubAYg61/JwnJNbZilGjf3b3pB80+OQg2qf6c8BfWE= github.com/iris-contrib/go.uuid v2.0.0+incompatible/go.mod h1:iz2lgM/1UnEf1kP0L/+fafWORmlnuysV2EMP8MW+qe0= github.com/iris-contrib/httpexpect v0.0.0-20180314041918-ebe99fcebbce/go.mod h1:VER17o2JZqquOx41avolD/wMGQSFEFBKWmhag9/RQRY= diff --git a/iris.go b/iris.go index 8423e5f7..6f589a84 100644 --- a/iris.go +++ b/iris.go @@ -463,6 +463,12 @@ var ( // // A shortcut for the `context#CookieDecode`. CookieDecode = context.CookieDecode + // IsErrPath can be used at `context#ReadForm`. + // It reports whether the incoming error is type of `formbinder.ErrPath`, + // which can be ignored when server allows unknown post values to be sent by the client. + // + // A shortcut for the `context#IsErrPath`. + IsErrPath = context.IsErrPath ) // SPA accepts an "assetHandler" which can be the result of an