Skip to content
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

[ISSUE-2321] Updates documentation on re-mounted configuration for params #2339

Merged
merged 4 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -12,6 +12,7 @@

#### Fixes

* [#2339](/~https://github.com/ruby-grape/grape/pull/2339): Documentation and specs for remountable configuration in params - [@myxoh](/~https://github.com/myxoh).
* [#2328](/~https://github.com/ruby-grape/grape/pull/2328): Don't cache Class.instance_methods - [@byroot](/~https://github.com/byroot).
* [#2337](/~https://github.com/ruby-grape/grape/pull/2337): Fix: allow custom validators that do not end with _validator - [@ericproulx](/~https://github.com/ericproulx).
* Your contribution here.
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -526,15 +526,15 @@ end
```ruby
class BasicAPI < Grape::API
desc 'Statuses index' do
params: mounted { configuration[:entity] || API::Entities::Status }.documentation
params: (configuration[:entity] || API::Entities::Status).documentation
end
params do
requires :all, using: mounted { configuration[:entity] || API::Entities::Status }.documentation
requires :all, using: (configuration[:entity] || API::Entities::Status).documentation
end
get '/statuses' do
statuses = Status.all
type = current_user.admin? ? :full : :default
present statuses, with: mounted { configuration[:entity] || API::Entities::Status }, type: type
present statuses, with: (configuration[:entity] || API::Entities::Status), type: type
end
end

Expand Down
36 changes: 36 additions & 0 deletions spec/grape/api_remount_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,42 @@ def app
end
end

context 'when the params are configured via a configuration' do
subject(:a_remounted_api) do
Class.new(described_class) do
params do
requires configuration[:required_attr_name], type: String
end

get(mounted { configuration[:endpoint] }) do
status 200
end
end
end

context 'when the configured param is my_attr' do
it 'requires the configured params' do
root_api.mount a_remounted_api, with: {
required_attr_name: 'my_attr',
endpoint: 'test'
}
get 'test?another_attr=1'
expect(last_response.status).to eq 400
get 'test?my_attr=1'
expect(last_response.status).to eq 200

root_api.mount a_remounted_api, with: {
required_attr_name: 'another_attr',
endpoint: 'test_b'
}
get 'test_b?another_attr=1'
expect(last_response.status).to eq 200
get 'test_b?my_attr=1'
expect(last_response.status).to eq 400
end
end
end

context 'when executing a standard block within a `mounted` block with all dynamic params' do
subject(:a_remounted_api) do
Class.new(described_class) do
Expand Down