grape icon indicating copy to clipboard operation
grape copied to clipboard

Parameter defaults ignored when nested inside hash

Open tlconnor opened this issue 5 years ago • 9 comments

I'm not sure if this is the intended behavior or not, but there appears to be a problem with the :default option on params that are nested inside another param of type Hash.

For example, given this code:

params do
  optional :filter, type: Hash do
    optional :status, default: 'new'
  end
end
get do
  params
end

If I call this API, I would expect to get back:

{ "filter": { "status": "new" } }

but instead it will respond with

{}

I have a commit with an example failing test case. https://github.com/tlconnor/grape/commit/26beeab

tlconnor avatar Jun 23 '20 04:06 tlconnor

Does setting default on :filter change anything?

dblock avatar Jun 23 '20 15:06 dblock

Does setting default on :filter change anything?

It does not help. Adding default: {} to the parent results in:

{ "filter": {} }

tlconnor avatar Jun 23 '20 21:06 tlconnor

Looks like a bug. PRs welcome.

dblock avatar Jun 24 '20 03:06 dblock

@dblock I would like to create a fix for this this, can you please point me to the right direction?

sekmo avatar Sep 21 '22 10:09 sekmo

@sekmo Start by writing a failing RSpec test?

dblock avatar Sep 21 '22 22:09 dblock

@dblock I mean can you point me to the part in the codebase where we deal with this logic?

sekmo avatar Sep 21 '22 23:09 sekmo

Most parameter logic lives around https://github.com/ruby-grape/grape/blob/master/lib/grape/validations/params_scope.rb, but I don't have anything more specific to point you to, sorry.

dblock avatar Sep 22 '22 15:09 dblock

Thank you, I'll check it out as soon as I have some time :)

sekmo avatar Sep 23 '22 08:09 sekmo