Skip to content

Commit fb0eae7

Browse files
Alex Vondrakdm1try
authored andcommitted
Make sure to set documentation attribute even for default: false
1 parent c698c44 commit fb0eae7

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* [#950](https://github.com/intridea/grape/pull/950): Status method can now accept one of Rack::Utils status code symbols (:ok, :found, :bad_request, etc.) - [@dabrorius](https://github.com/dabrorius).
1313
* [#952](https://github.com/intridea/grape/pull/952): Status method now raises error when called with invalid status code - [@dabrorius](https://github.com/dabrorius).
1414
* [#957](https://github.com/intridea/grape/pull/957): Regexp validator now supports `allow_blank`, `nil` value behavior changed - [@calfzhou](https://giihub.com/calfzhou).
15+
* [#962](https://github.com/intridea/grape/pull/962): The `default` attribute with `false` value is documented now - [@ajvondrak](https://github.com/ajvondrak).
1516
* Your contribution here.
1617

1718
0.11.0 (2/23/2015)

lib/grape/validations/params_scope.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def validates(attrs, validations)
117117
doc_attrs[:desc] = desc if desc
118118

119119
default = validations[:default]
120-
doc_attrs[:default] = default if default
120+
doc_attrs[:default] = default if validations.key?(:default)
121121

122122
values = validations[:values]
123123
doc_attrs[:values] = values if values

spec/grape/validations/params_scope_spec.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,70 @@ def app
99
subject
1010
end
1111

12+
context 'setting a default' do
13+
let(:documentation) { subject.routes.first.route_params }
14+
15+
context 'when the default value is truthy' do
16+
before do
17+
subject.params do
18+
optional :int, type: Integer, default: 42
19+
end
20+
subject.get
21+
end
22+
23+
it 'adds documentation about the default value' do
24+
expect(documentation).to have_key('int')
25+
expect(documentation['int']).to have_key(:default)
26+
expect(documentation['int'][:default]).to eq(42)
27+
end
28+
end
29+
30+
context 'when the default value is false' do
31+
before do
32+
subject.params do
33+
optional :bool, type: Virtus::Attribute::Boolean, default: false
34+
end
35+
subject.get
36+
end
37+
38+
it 'adds documentation about the default value' do
39+
expect(documentation).to have_key('bool')
40+
expect(documentation['bool']).to have_key(:default)
41+
expect(documentation['bool'][:default]).to eq(false)
42+
end
43+
end
44+
45+
context 'when the default value is nil' do
46+
before do
47+
subject.params do
48+
optional :object, type: Object, default: nil
49+
end
50+
subject.get
51+
end
52+
53+
it 'adds documentation about the default value' do
54+
expect(documentation).to have_key('object')
55+
expect(documentation['object']).to have_key(:default)
56+
expect(documentation['object'][:default]).to eq(nil)
57+
end
58+
end
59+
end
60+
61+
context 'without a default' do
62+
before do
63+
subject.params do
64+
optional :object, type: Object
65+
end
66+
subject.get
67+
end
68+
69+
it 'does not add documentation for the default value' do
70+
documentation = subject.routes.first.route_params
71+
expect(documentation).to have_key('object')
72+
expect(documentation['object']).not_to have_key(:default)
73+
end
74+
end
75+
1276
context 'setting description' do
1377
[:desc, :description].each do |description_type|
1478
it "allows setting #{description_type}" do

0 commit comments

Comments
 (0)