Skip to content

Fix: Operate on a cloned Hash when specifying param presence #942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#### Fixes

* [#936](https://github.com/intridea/grape/pull/936): Fixed default params processing for optional groups - [@dm1try](https://github.com/dm1try).
* [#](https://github.com/intridea/grape/pull/): Fixed forced presence for optional params when based on a reused entity that was also required in another context - [@croeck](https://github.com/croeck).

* Your contribution here.

Expand Down
4 changes: 2 additions & 2 deletions lib/grape/dsl/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def use(*names)
def requires(*attrs, &block)
orig_attrs = attrs.clone

opts = attrs.last.is_a?(Hash) ? attrs.pop : {}
opts = attrs.last.is_a?(Hash) ? attrs.pop.clone : {}
opts.merge!(presence: true)

if opts[:using]
Expand All @@ -37,7 +37,7 @@ def requires(*attrs, &block)
def optional(*attrs, &block)
orig_attrs = attrs.clone

opts = attrs.last.is_a?(Hash) ? attrs.pop : {}
opts = attrs.last.is_a?(Hash) ? attrs.pop.clone : {}
type = opts[:type]

# check type for optional parameter group
Expand Down
38 changes: 38 additions & 0 deletions spec/grape/validations/validators/presence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,42 @@ def app
expect(last_response.body).to eq('Nested triple'.to_json)
end
end

context 'with reused parameter documentation once required and once optional' do
before do
docs = { name: { type: String, desc: 'some name' } }

subject.params do
requires :all, using: docs
end
subject.get '/required' do
'Hello required'
end

subject.params do
optional :all, using: docs
end
subject.get '/optional' do
'Hello optional'
end
end
it 'works with required' do
get '/required'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"name is missing"}')

get '/required', name: 'Bob'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Hello required'.to_json)
end
it 'works with optional' do
get '/optional'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Hello optional'.to_json)

get '/optional', name: 'Bob'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Hello optional'.to_json)
end
end
end