given and declared do not work together
Open
Nobody has claimed this yet.
bug?
- Dominant language
- Ruby
- Stars
- 10k
- Forks
- 1.2k
- Avg merge
- 14h 38m
- Merged PRs (30d)
- 92
Description
I have the following params definition:
module FooParams
extend Grape::API::Helpers
params :bar_params do
requires :a, type: String
end
params :baz_params do
requires :b, type: String
end
params :foo_params do
requires :type, type: String
given type: -> (val) { val == 'Bar' } do
requires :foo, type: Hash do
use :bar_params
end
end
given type: -> (val) { val == 'Baz' } do
requires :foo, type: Hash do
use :baz_params
end
end
end
end
The foo_params are used in this post:
resource :foos do
helpers FooParams
params do
use :foo_params
end
post do
if params[:type] == 'Bar'
puts "original a: '#{params[:foo][:a]}'"
puts "declared a: '#{declared(params)[:foo][:a]}'"
end
{}
end
end
Now when I call POST /foos with {type: 'Bar', {foo: {a: 'letter A'}}}, I get the following output:
original a: 'letter A'
declared a: ''
If I remove the latter given definition (val == 'Baz'), it prints:
original a: 'letter A'
declared a: 'letter A'
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue with the shown FooParams definitions and POST /foos request. Start by tracing how the given and declared parameter handling uses multiple conditional branches. Done means declared(params) retains foo.a when type is Bar even when the Baz branch is also defined, with a regression test covering both branches.
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
- 42/100