From b2833edd507f86b964ae625144719198a740997d Mon Sep 17 00:00:00 2001 From: Juan Carlos Garcia Date: Fri, 17 Nov 2023 14:01:53 +0100 Subject: [PATCH 1/4] feature(#2290): Use a param value as the `default` value of other param --- CHANGELOG.md | 1 + README.md | 11 ++++++++ .../validators/default_validator.rb | 6 ++++- .../validations/validators/default_spec.rb | 26 +++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e85f6072d1..a765139c2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ #### 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 diff --git a/README.md b/README.md index 81d6ddc810..831a81822a 100644 --- a/README.md +++ b/README.md @@ -1242,6 +1242,17 @@ 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: diff --git a/lib/grape/validations/validators/default_validator.rb b/lib/grape/validations/validators/default_validator.rb index 4058d7b1dd..9a59e5da15 100644 --- a/lib/grape/validations/validators/default_validator.rb +++ b/lib/grape/validations/validators/default_validator.rb @@ -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 diff --git a/spec/grape/validations/validators/default_spec.rb b/spec/grape/validations/validators/default_spec.rb index 980fa23044..2bb59792d8 100644 --- a/spec/grape/validations/validators/default_spec.rb +++ b/spec/grape/validations/validators/default_spec.rb @@ -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 @@ -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 From 325c8370c0741d6405ed1746b213a172a7f6c08e Mon Sep 17 00:00:00 2001 From: Juan Carlos Garcia Date: Fri, 17 Nov 2023 23:37:25 +0100 Subject: [PATCH 2/4] feature(#2290): Updating version to 2.1.0 --- CHANGELOG.md | 2 +- lib/grape/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a765139c2e..55682fecdb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -### 2.0.1 (Next) +### 2.1.0 (Next) #### Features diff --git a/lib/grape/version.rb b/lib/grape/version.rb index df942655ae..1013166e1a 100644 --- a/lib/grape/version.rb +++ b/lib/grape/version.rb @@ -2,5 +2,5 @@ module Grape # The current version of Grape. - VERSION = '2.0.1' + VERSION = '2.1.0' end From e34ad629c9d332d3df463507ee74de0c5586ddf9 Mon Sep 17 00:00:00 2001 From: Juan Carlos Garcia Date: Sat, 18 Nov 2023 11:00:51 +0100 Subject: [PATCH 3/4] feature(#2290): Updating stable release section with 2.1.0 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 831a81822a..d572f064cd 100644 --- a/README.md +++ b/README.md @@ -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). From 6c7003ad244bd396be03eed65cb853b2affc85e6 Mon Sep 17 00:00:00 2001 From: Juan Carlos Garcia Date: Sun, 19 Nov 2023 13:42:18 +0100 Subject: [PATCH 4/4] feature(#2290): Updating README for following one-line format --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index d572f064cd..58d391f7bd 100644 --- a/README.md +++ b/README.md @@ -1242,9 +1242,7 @@ 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. +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