context#ReadForm can skip unkown fields by iris/context.IsErrPath(err), fixes: https://github.com/kataras/iris/issues/1157

Former-commit-id: 5cc8e5a9d58071591154e988262b547653c34e36
This commit is contained in:
Gerasimos (Makis) Maropoulos 2019-01-04 11:40:55 +02:00
parent 8b74e3343d
commit caac0480ba
5 changed files with 17 additions and 8 deletions

View File

@ -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())
}

View File

@ -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)
}
// +------------------------------------------------------------+

2
go.mod
View File

@ -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

4
go.sum
View File

@ -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=

View File

@ -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