From 567c06702fa4359dc4835593a55c55854069954a Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Sun, 14 Feb 2021 13:40:56 +0200 Subject: [PATCH] respect the iris.WithEmptyFormError option on Context.ReadQuery too as requested at: #1727 --- HISTORY.md | 2 ++ _examples/request-body/read-query/main.go | 17 +++++++++++++++-- aliases.go | 2 +- configuration.go | 2 +- context/context.go | 10 ++++++++-- macro/interpreter/parser/parser.go | 2 +- 6 files changed, 28 insertions(+), 7 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 08568fa10e..31beb8fe25 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -28,6 +28,8 @@ The codebase for Dependency Injection, Internationalization and localization and ## Fixes and Improvements +- The `iris.WithEmptyFormError` option is respected on `context.ReadQuery` method too, as requested at [#1727](https://github.com/kataras/iris/issues/1727). [Example comments](https://github.com/kataras/iris/blob/master/_examples/request-body/read-query/main.go) were updated. + - New `httptest.Strict` option setter to enable the `httpexpect.RequireReporter` instead of the default `httpexpect.AssetReporter. Use that to enable complete test failure on the first error. As requested at: [#1722](https://github.com/kataras/iris/issues/1722). - New `uuid` builtin path parameter type. Example: diff --git a/_examples/request-body/read-query/main.go b/_examples/request-body/read-query/main.go index 941b4dc171..e8b763a5cf 100644 --- a/_examples/request-body/read-query/main.go +++ b/_examples/request-body/read-query/main.go @@ -17,7 +17,18 @@ func main() { app.Get("/", func(ctx iris.Context) { var t MyType err := ctx.ReadQuery(&t) - if err != nil && !iris.IsErrPath(err) { + + // To ignore errors of "required" or when unexpected values are passed to the query, + // use the iris.IsErrPath. + // It can be ignored, e.g: + // if err!=nil && !iris.IsErrPath(err) { ... return } + // + // To receive an error on EMPTY query when ReadQuery is called + // you should enable the `FireEmptyFormError/WithEmptyFormError` ( see below). + // To check for the empty error you simple compare the error with the ErrEmptyForm, e.g.: + // err == iris.ErrEmptyForm, so, to ignore both path and empty errors, you do: + // if err!=nil && err != iris.ErrEmptyForm && !iris.IsErrPath(err) { ctx.StopWithError(...); return } + if err != nil { ctx.StopWithError(iris.StatusInternalServerError, err) return } @@ -32,5 +43,7 @@ func main() { // http://localhost:8080?name=iris&age=3 // http://localhost:8080/simple?name=john&name=doe&name=kataras - app.Listen(":8080") + // + // Note: this `WithEmptyFormError` will give an error if the query was empty. + app.Listen(":8080", iris.WithEmptyFormError) } diff --git a/aliases.go b/aliases.go index 37500a43b0..a70c7ef8e5 100644 --- a/aliases.go +++ b/aliases.go @@ -497,7 +497,7 @@ var ( CookieEncoding = context.CookieEncoding // IsErrPath can be used at `context#ReadForm` and `context#ReadQuery`. - // It reports whether the incoming error is type of `formbinder.ErrPath`, + // It reports whether the incoming error is type of `schema.ErrPath`, // which can be ignored when server allows unknown post values to be sent by the client. // // A shortcut for the `context#IsErrPath`. diff --git a/configuration.go b/configuration.go index 98d9613ea5..d677f2f178 100644 --- a/configuration.go +++ b/configuration.go @@ -731,7 +731,7 @@ type Configuration struct { // // See `Context.RecordRequestBody` method for the same feature, per-request. DisableBodyConsumptionOnUnmarshal bool `ini:"disable_body_consumption" json:"disableBodyConsumptionOnUnmarshal,omitempty" yaml:"DisableBodyConsumptionOnUnmarshal" toml:"DisableBodyConsumptionOnUnmarshal"` - // FireEmptyFormError returns if set to tue true then the `context.ReadBody/ReadForm` + // FireEmptyFormError returns if set to tue true then the `context.ReadForm/ReadQuery/ReadBody` // will return an `iris.ErrEmptyForm` on empty request form data. FireEmptyFormError bool `ini:"fire_empty_form_error" json:"fireEmptyFormError,omitempty" yaml:"FireEmptyFormError" toml:"FireEmptyFormError"` diff --git a/context/context.go b/context/context.go index 83a05454ce..1fed82ae2d 100644 --- a/context/context.go +++ b/context/context.go @@ -2386,8 +2386,11 @@ var ( // A shortcut for the `schema#IsErrPath`. IsErrPath = schema.IsErrPath - // ErrEmptyForm is returned by `context#ReadForm` and `context#ReadBody` - // when it should read data from a request form data but there is none. + // ErrEmptyForm is returned by + // - `context#ReadForm` + // - `context#ReadQuery` + // - `context#ReadBody` + // when the request data (form, query and body respectfully) is empty. ErrEmptyForm = errors.New("empty form") // ErrEmptyFormField reports whether a specific field exists but it's empty. @@ -2460,6 +2463,9 @@ func (ctx *Context) ReadForm(formObject interface{}) error { func (ctx *Context) ReadQuery(ptr interface{}) error { values := ctx.getQuery() if len(values) == 0 { + if ctx.app.ConfigurationReadOnly().GetFireEmptyFormError() { + return ErrEmptyForm + } return nil } diff --git a/macro/interpreter/parser/parser.go b/macro/interpreter/parser/parser.go index 65ac011979..a489298e5e 100644 --- a/macro/interpreter/parser/parser.go +++ b/macro/interpreter/parser/parser.go @@ -39,7 +39,7 @@ func Parse(fullpath string, paramTypes []ast.ParamType) ([]*ast.ParamStatement, } // if we have param type path but it's not the last path part if ast.IsTrailing(stmt.Type) && i < len(pathParts)-1 { - return nil, fmt.Errorf("%s: parameter type \"%s\" should be registered to the very last of a path", s, stmt.Type.Indent()) + return nil, fmt.Errorf("%s: parameter type \"%s\" should be registered to the very end of a path", s, stmt.Type.Indent()) } statements = append(statements, stmt)