Skip to content

Commit 4704cc5

Browse files
committed
Merge pull request #912 from croeck/optional-param-documentation-with-using
Added support for `optional` param documentation with `using`
2 parents 89591f2 + b18f498 commit 4704cc5

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Next Release
88
* [#881](https://github.com/intridea/grape/issues/881): Fixed `Grape::Validations::ValuesValidator` support for `Range` type - [@ajvondrak](https://github.com/ajvondrak).
99
* [#901](https://github.com/intridea/grape/pull/901): Fix: callbacks defined in a version block are only called for the routes defined in that block - [@kushkella](https://github.com/kushkella).
1010
* [#886](https://github.com/intridea/grape/pull/886): Group of parameters made to require an explicit type of Hash or Array - [@jrichter1](https://github.com/jrichter1).
11+
* [#912](https://github.com/intridea/grape/pull/912): Extended the `:using` feature for param documentation to `optional` fields - [@croeck](https://github.com/croeck).
1112
* Your contribution here.
1213

1314
0.10.1 (12/28/2014)

lib/grape/dsl/parameters.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ def optional(*attrs, &block)
4646
fail Grape::Exceptions::UnsupportedGroupTypeError.new unless [Array, Hash].include?(type)
4747
end
4848

49-
validate_attributes(attrs, opts, &block)
49+
if opts[:using]
50+
require_optional_fields(attrs.first, opts)
51+
else
52+
validate_attributes(attrs, opts, &block)
5053

51-
block_given? ? new_scope(orig_attrs, true, &block) :
52-
push_declared_params(attrs)
54+
block_given? ? new_scope(orig_attrs, true, &block) :
55+
push_declared_params(attrs)
56+
end
5357
end
5458

5559
def mutually_exclusive(*attrs)

lib/grape/validations/params_scope.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ def require_required_and_optional_fields(context, opts)
6464
end
6565
end
6666

67+
def require_optional_fields(context, opts)
68+
optional_fields = opts[:using].keys
69+
optional_fields -= Array(opts[:except]) unless context == :all
70+
optional_fields.each do |field|
71+
field_opts = opts[:using][field]
72+
optional(field, field_opts) if field_opts
73+
end
74+
end
75+
6776
def validate_attributes(attrs, opts, &block)
6877
validations = opts.clone
6978
validations[:type] ||= Array if block

spec/grape/validations_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,44 @@ def app
4747
end
4848
end
4949

50+
context 'optional using Grape::Entity documentation' do
51+
def define_optional_using
52+
documentation = { field_a: { type: String }, field_b: { type: String } }
53+
subject.params do
54+
optional :all, using: documentation
55+
end
56+
end
57+
before do
58+
define_optional_using
59+
subject.get '/optional' do
60+
'optional with using works'
61+
end
62+
end
63+
64+
it 'adds entity documentation to declared params' do
65+
define_optional_using
66+
expect(subject.route_setting(:declared_params)).to eq([:field_a, :field_b])
67+
end
68+
69+
it 'works when field_a and field_b are not present' do
70+
get '/optional'
71+
expect(last_response.status).to eq(200)
72+
expect(last_response.body).to eq('optional with using works')
73+
end
74+
75+
it 'works when field_a is present' do
76+
get '/optional', field_a: 'woof'
77+
expect(last_response.status).to eq(200)
78+
expect(last_response.body).to eq('optional with using works')
79+
end
80+
81+
it 'works when field_b is present' do
82+
get '/optional', field_b: 'woof'
83+
expect(last_response.status).to eq(200)
84+
expect(last_response.body).to eq('optional with using works')
85+
end
86+
end
87+
5088
context 'required' do
5189
before do
5290
subject.params do

0 commit comments

Comments
 (0)