grape icon indicating copy to clipboard operation
grape copied to clipboard

Fail on (or get list of) unknown parameters

Open lvonk opened this issue 3 years ago • 3 comments

Hi,

Prior to https://github.com/ruby-grape/grape/pull/2189 the params object also contained the unknown parameters that weren't described in the route definition.

For instance:

 requires :name,
                type: String,
                as: :company_name,
optional :bar,
               type: String,

If then the following params where passed:

{
  "name": "Grape",
  "baz": "Bar",
}

The params object contained:

{
  "company_name": "Grape",
  "baz": "Bar",
}

This way our code could fail on the fact that "baz" was passed in but doesn't exist. This is helpful because most probably the consumer of the api wanted to pass in bar which is an optional field (this is a frequent occurring scenario btw in our production systems). I think it is good practice to fail fast in scenarios where parameters are passed in that are "unknown".

After this fix there is no easy way anymore to get these unknown parameters since declared filters them out, declared(params) yields to:

{
  "company_name": "Grape",
}

Merging them with the original params is also not an option because then we also get the name parameter back again.

So my question is: Is there a way in grape to get the "unknown parameters" passed in? Or let Grape fail when such a parameter is passed?

lvonk avatar Feb 14 '22 08:02 lvonk

This sounds like a valid scenario. Should we add an unknown or undeclared helper? Want to give this a try @lvonk?

dblock avatar Feb 14 '22 17:02 dblock

I can give it a try. I'd opt for failing when such a parameter is passed in, perhaps via a setting? Like fail_on_undeclared_params = true|false then when it is false you could still use the helper to get a list of these params.

lvonk avatar Feb 16 '22 16:02 lvonk

I think both features would be valuable, and failing would be built on top of extracting the names of such parameters.

dblock avatar Feb 19 '22 01:02 dblock