Skip to content

Commit dd741b9

Browse files
authored
[ISSUE-2321] Updates documentation on re-mounted configuration for params (#2339)
* [ISSUE-2321] Updates documentation on re-mounted configuration for params * Adds changelog * Extra space on changelog * More updates to changelog
1 parent 1147658 commit dd741b9

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#### Fixes
1414

15+
* [#2339](https://github.com/ruby-grape/grape/pull/2339): Documentation and specs for remountable configuration in params - [@myxoh](https://github.com/myxoh).
1516
* [#2328](https://github.com/ruby-grape/grape/pull/2328): Don't cache Class.instance_methods - [@byroot](https://github.com/byroot).
1617
* [#2337](https://github.com/ruby-grape/grape/pull/2337): Fix: allow custom validators that do not end with _validator - [@ericproulx](https://github.com/ericproulx).
1718
* Your contribution here.

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -526,15 +526,15 @@ end
526526
```ruby
527527
class BasicAPI < Grape::API
528528
desc 'Statuses index' do
529-
params: mounted { configuration[:entity] || API::Entities::Status }.documentation
529+
params: (configuration[:entity] || API::Entities::Status).documentation
530530
end
531531
params do
532-
requires :all, using: mounted { configuration[:entity] || API::Entities::Status }.documentation
532+
requires :all, using: (configuration[:entity] || API::Entities::Status).documentation
533533
end
534534
get '/statuses' do
535535
statuses = Status.all
536536
type = current_user.admin? ? :full : :default
537-
present statuses, with: mounted { configuration[:entity] || API::Entities::Status }, type: type
537+
present statuses, with: (configuration[:entity] || API::Entities::Status), type: type
538538
end
539539
end
540540

spec/grape/api_remount_spec.rb

+36
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,42 @@ def app
147147
end
148148
end
149149

150+
context 'when the params are configured via a configuration' do
151+
subject(:a_remounted_api) do
152+
Class.new(described_class) do
153+
params do
154+
requires configuration[:required_attr_name], type: String
155+
end
156+
157+
get(mounted { configuration[:endpoint] }) do
158+
status 200
159+
end
160+
end
161+
end
162+
163+
context 'when the configured param is my_attr' do
164+
it 'requires the configured params' do
165+
root_api.mount a_remounted_api, with: {
166+
required_attr_name: 'my_attr',
167+
endpoint: 'test'
168+
}
169+
get 'test?another_attr=1'
170+
expect(last_response.status).to eq 400
171+
get 'test?my_attr=1'
172+
expect(last_response.status).to eq 200
173+
174+
root_api.mount a_remounted_api, with: {
175+
required_attr_name: 'another_attr',
176+
endpoint: 'test_b'
177+
}
178+
get 'test_b?another_attr=1'
179+
expect(last_response.status).to eq 200
180+
get 'test_b?my_attr=1'
181+
expect(last_response.status).to eq 400
182+
end
183+
end
184+
end
185+
150186
context 'when executing a standard block within a `mounted` block with all dynamic params' do
151187
subject(:a_remounted_api) do
152188
Class.new(described_class) do

0 commit comments

Comments
 (0)