Skip to content

Commit

Permalink
Merge branch 'master' into fix_to_ary_method_missing
Browse files Browse the repository at this point in the history
  • Loading branch information
ericproulx authored Nov 19, 2023
2 parents ee8627f + 34f90f8 commit feb3960
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 6 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
### 2.0.1 (Next)
### 2.1.0 (Next)

#### Features

* [#2371](https://github.com/ruby-grape/grape/pull/2371): Use a param value as the `default` value of other param - [@jcagarcia](https://github.com/jcagarcia).
* Your contribution here.

#### Fixes

* [#2370](https://github.com/ruby-grape/grape/pull/2370): Stop method_missing to_ary - [@ericproulx](https://github.com/ericproulx).
* [#2372](https://github.com/ruby-grape/grape/pull/2372): Fix `declared` method for hash params with overlapping names - [@jcagarcia](https://github.com/jcagarcia).
* Your contribution here.

### 2.0.0 (2023/11/11)
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ content negotiation, versioning and much more.

## Stable Release

You're reading the documentation for the next release of Grape, which should be **2.0.1**.
You're reading the documentation for the next release of Grape, which should be **2.1.0**.
Please read [UPGRADING](UPGRADING.md) when upgrading from a previous version.
The current stable release is [2.0.0](https://github.com/ruby-grape/grape/blob/v2.0.0/README.md).

Expand Down Expand Up @@ -1242,6 +1242,15 @@ params do
end
```

You can use the value of one parameter as the default value of some other parameter. In this case, if the `primary_color` parameter is not provided, it will have the same value as the `color` one. If both of them not provided, both of them will have `blue` value.

```ruby
params do
optional :color, type: String, default: 'blue'
optional :primary_color, type: String, default: -> (params) { params[:color] }
end
```

### Supported Parameter Types

The following are all valid types, supported out of the box by Grape:
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def handle_passed_param(params_nested_path, has_passed_children = false, &_block

route_options_params = options[:route_options][:params] || {}
type = route_options_params.dig(key, :type)
has_children = route_options_params.keys.any? { |k| k != key && k.start_with?(key) }
has_children = route_options_params.keys.any? { |k| k != key && k.start_with?("#{key}[") }

if type == 'Hash' && !has_children
{}
Expand Down
6 changes: 5 additions & 1 deletion lib/grape/validations/validators/default_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ def initialize(attrs, options, required, scope, **opts)

def validate_param!(attr_name, params)
params[attr_name] = if @default.is_a? Proc
@default.call
if @default.parameters.empty?
@default.call
else
@default.call(params)
end
elsif @default.frozen? || !@default.duplicable?
@default
else
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

module Grape
# The current version of Grape.
VERSION = '2.0.1'
VERSION = '2.1.0'
end
4 changes: 3 additions & 1 deletion spec/grape/endpoint/declared_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def app
optional :empty_arr, type: Array
optional :empty_typed_arr, type: Array[String]
optional :empty_hash, type: Hash
optional :empty_hash_two, type: Hash
optional :empty_set, type: Set
optional :empty_typed_set, type: Set[String]
end
Expand Down Expand Up @@ -122,7 +123,7 @@ def app
end
get '/declared?first=present'
expect(last_response.status).to eq(200)
expect(JSON.parse(last_response.body).keys.size).to eq(11)
expect(JSON.parse(last_response.body).keys.size).to eq(12)
end

it 'has a optional param with default value all the time' do
Expand Down Expand Up @@ -201,6 +202,7 @@ def app

body = JSON.parse(last_response.body)
expect(body['empty_hash']).to eq({})
expect(body['empty_hash_two']).to eq({})
expect(body['nested']).to be_a(Hash)
expect(body['nested']['empty_hash']).to eq({})
expect(body['nested']['nested_two']).to be_a(Hash)
Expand Down
26 changes: 26 additions & 0 deletions spec/grape/validations/validators/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@
get '/another_nested_optional_array' do
{ root: params[:root] }
end

params do
requires :foo
optional :bar, default: ->(params) { params[:foo] }
optional :qux, default: ->(params) { params[:bar] }
end
get '/default_values_from_other_params' do
{
foo: params[:foo],
bar: params[:bar],
qux: params[:qux]
}
end
end
end

Expand Down Expand Up @@ -460,4 +473,17 @@ def app
expect(JSON.parse(last_response.body)).to eq(expected)
end
end

it 'sets default value for optional params using other params values' do
expected_foo_value = 'foo-value'

get("/default_values_from_other_params?foo=#{expected_foo_value}")

expect(last_response.status).to eq(200)
expect(last_response.body).to eq({
foo: expected_foo_value,
bar: expected_foo_value,
qux: expected_foo_value
}.to_json)
end
end

0 comments on commit feb3960

Please sign in to comment.