Skip to content

Commit fcb7afa

Browse files
committed
Merge pull request #1052 from marshall-lee/fix_description_params
Reset description[:params] when resetting validations
2 parents aa41c15 + 471f4e5 commit fcb7afa

File tree

5 files changed

+70
-5
lines changed

5 files changed

+70
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Next Release
1313
* [#1042](https://github.com/intridea/grape/issues/1042): Fix coercion of complex arrays - [@dim](https://github.com/dim).
1414
* [#1045](https://github.com/intridea/grape/pull/1045): Do not convert `Rack::Response` to `Rack::Response` in middleware - [@dmitry](https://github.com/dmitry).
1515
* [#1048](https://github.com/intridea/grape/pull/1048): Only dup `InheritableValues`, remove support for `deep_dup` - [@toddmazierski](https://github.com/toddmazierski/)
16+
* [#1052](https://github.com/intridea/grape/pull/1052): Reset `description[:params]` when resetting validations - [@marshall-lee](https://github.com/marshall-lee).
1617

1718
0.12.0 (6/18/2015)
1819
==================

lib/grape/dsl/configuration.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ def desc(description, options = {}, &config_block)
7272
namespace_setting :description, options
7373
route_setting :description, options
7474
end
75+
76+
def description_field(field, value = nil)
77+
if value
78+
description = route_setting(:description)
79+
description ||= route_setting(:description, {})
80+
description[field] = value
81+
else
82+
description = route_setting(:description)
83+
description[field] if description
84+
end
85+
end
86+
87+
def unset_description_field(field)
88+
description = route_setting(:description)
89+
description.delete(field) if description
90+
end
7591
end
7692

7793
module_function

lib/grape/dsl/validations.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def reset_validations!
1313
unset_namespace_stackable :declared_params
1414
unset_namespace_stackable :validations
1515
unset_namespace_stackable :params
16+
unset_description_field :params
1617
end
1718

1819
# Opens a root-level ParamsScope, defining parameter coercions and
@@ -23,11 +24,8 @@ def params(&block)
2324
end
2425

2526
def document_attribute(names, opts)
26-
route_setting(:description, {}) unless route_setting(:description)
27-
28-
route_setting(:description)[:params] ||= {}
29-
30-
setting = route_setting(:description)[:params]
27+
setting = description_field(:params)
28+
setting ||= description_field(:params, {})
3129
Array(names).each do |name|
3230
setting[name[:full_name].to_s] ||= {}
3331
setting[name[:full_name].to_s].merge!(opts)

spec/grape/api_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,6 +2123,38 @@ def static
21232123
{ description: 'Reverses a string.', params: { 's' => { desc: 'string to reverse', type: 'string' } } }
21242124
]
21252125
end
2126+
it 'does not inherit param descriptions in consequent namespaces' do
2127+
subject.desc 'global description'
2128+
subject.params do
2129+
requires :param1
2130+
optional :param2
2131+
end
2132+
subject.namespace 'ns1' do
2133+
get do; end
2134+
end
2135+
subject.params do
2136+
optional :param2
2137+
end
2138+
subject.namespace 'ns2' do
2139+
get do; end
2140+
end
2141+
routes_doc = subject.routes.map { |route|
2142+
{ description: route.route_description, params: route.route_params }
2143+
}
2144+
expect(routes_doc).to eq [
2145+
{ description: 'global description',
2146+
params: {
2147+
'param1' => { required: true },
2148+
'param2' => { required: false }
2149+
}
2150+
},
2151+
{ description: 'global description',
2152+
params: {
2153+
'param2' => { required: false }
2154+
}
2155+
}
2156+
]
2157+
end
21262158
it 'merges the parameters of the namespace with the parameters of the method' do
21272159
subject.desc 'namespace'
21282160
subject.params do

spec/grape/dsl/validations_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,34 @@ class Dummy
1515
before do
1616
subject.namespace_stackable :declared_params, ['dummy']
1717
subject.namespace_stackable :validations, ['dummy']
18+
subject.namespace_stackable :params, ['dummy']
19+
subject.route_setting :description, description: 'lol', params: ['dummy']
1820
subject.reset_validations!
1921
end
2022

23+
after do
24+
subject.unset_route_setting :description
25+
end
26+
2127
it 'resets declared params' do
2228
expect(subject.namespace_stackable(:declared_params)).to eq []
2329
end
2430

2531
it 'resets validations' do
2632
expect(subject.namespace_stackable(:validations)).to eq []
2733
end
34+
35+
it 'resets params' do
36+
expect(subject.namespace_stackable(:params)).to eq []
37+
end
38+
39+
it 'resets documentation params' do
40+
expect(subject.route_setting(:description)[:params]).to be_nil
41+
end
42+
43+
it 'does not reset documentation description' do
44+
expect(subject.route_setting(:description)[:description]).to eq 'lol'
45+
end
2846
end
2947

3048
describe '.params' do

0 commit comments

Comments
 (0)