ruby-grape / ruby-grape/grape

Parameter defaults ignored when nested inside hash

Open
#2,073 9 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug?
Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the failing test case in commit 26beeab and trace how the nested Hash parameter is parsed and how default values are applied. Done means the example returns {"filter":{"status":"new"}} when filter is omitted, with the relevant tests passing.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.