Skip to content

Commit a35eca6

Browse files
committed
Merge pull request #851 from dblock/fix-801
Fix #801: Evaluate values proc lazily.
2 parents 4a94caf + 152fcc5 commit a35eca6

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* [#813](https://github.com/intridea/grape/pull/813): Routing methods dsl refactored to get rid of explicit `paths` parameter - [@AlexYankee](https://github.com/AlexYankee).
1919
* [#826](https://github.com/intridea/grape/pull/826): Find `coerce_type` for `Array` when not specified - [@manovotn](https://github.com/manovotn).
2020
* [#645](https://github.com/intridea/grape/issues/645): Invoking `body false` will return `204 No Content` - [@dblock](https://github.com/dblock).
21+
* [#801](https://github.com/intridea/grape/issues/801): Evaluate permitted parameter `values` lazily on each request when declared as a proc - [@dblock](https://github.com/dblock).
2122
* Your contribution here.
2223

2324
0.9.0 (8/27/2014)

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ params do
747747
end
748748
```
749749

750-
The `:values` option can also be supplied with a `Proc` to be evalutated at runtime. For example, given a status
750+
The `:values` option can also be supplied with a `Proc` to be evalutated at runtime for each request. For example, given a status
751751
model you may want to restrict by hashtags that you have previously defined in the `HashTag` model.
752752

753753
```ruby

UPGRADING.md

+20
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,26 @@ end
189189

190190
See the [the updated API Formats documentation](https://github.com/intridea/grape#api-formats) and [#809](https://github.com/intridea/grape/pull/809) for more info.
191191

192+
#### Changes to Evaluation of Permitted Parameter Values
193+
194+
Permitted parameter values are now evaluated lazily for each request when declared as a proc. The following code would produce the same allowed value in 0.9.0 for each request and a new value since 0.10.0.
195+
196+
```ruby
197+
params do
198+
optional :v, values: -> { [SecureRandom.uuid] }
199+
end
200+
```
201+
202+
Remove the proc to get the previous behavior.
203+
204+
```ruby
205+
params do
206+
optional :v, values: [SecureRandom.uuid]
207+
end
208+
```
209+
210+
See [#801](https://github.com/intridea/grape/issues/801) for more information.
211+
192212
### Upgrading to >= 0.9.0
193213

194214
#### Changes in Authentication

lib/grape/validations/params_scope.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,17 @@ def validates(attrs, validations)
109109
values = validations[:values]
110110
doc_attrs[:values] = values if values
111111

112-
values = values.call if values.is_a?(Proc)
112+
evaluated_values = values.is_a?(Proc) ? values.call : values
113113

114-
coerce_type = values.first.class if values && coerce_type == Array && !values.empty?
114+
coerce_type = evaluated_values.first.class if evaluated_values && coerce_type == Array && !evaluated_values.empty?
115115

116116
# default value should be present in values array, if both exist
117-
if default && values && !values.include?(default)
118-
fail Grape::Exceptions::IncompatibleOptionValues.new(:default, default, :values, values)
117+
if default && evaluated_values && !evaluated_values.include?(default)
118+
fail Grape::Exceptions::IncompatibleOptionValues.new(:default, default, :values, evaluated_values)
119119
end
120120

121121
# type should be compatible with values array, if both exist
122-
validate_value_coercion(coerce_type, values) if coerce_type && values
122+
validate_value_coercion(coerce_type, evaluated_values) if coerce_type && evaluated_values
123123

124124
doc_attrs[:documentation] = validations.delete(:documentation) if validations.key?(:documentation)
125125

spec/grape/validations/validators/values_spec.rb

+11
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ class API < Grape::API
7676
end
7777
end
7878
get '/optional_with_required_values'
79+
80+
params do
81+
optional :type, type: String, values: -> { [SecureRandom.uuid] }
82+
end
83+
get '/random_values'
7984
end
8085
end
8186
end
@@ -199,4 +204,10 @@ def app
199204
subject.params { requires :type, values: [10.5, 11], type: Integer }
200205
end.to raise_error Grape::Exceptions::IncompatibleOptionValues
201206
end
207+
208+
it 'evaluates values dynamically with each request' do
209+
allow(SecureRandom).to receive(:uuid).and_return('foo')
210+
get '/random_values', type: 'foo'
211+
expect(last_response.status).to eq 200
212+
end
202213
end

0 commit comments

Comments
 (0)