Skip to content

Commit 990c3a2

Browse files
author
Braktar
committed
Update md files
1 parent aa72fa7 commit 990c3a2

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
* [#2161](https://github.com/ruby-grape/grape/pull/2157): Handle EOFError from Rack when given an empty multipart body - [@bschmeck](https://github.com/bschmeck).
1212
* [#2162](https://github.com/ruby-grape/grape/pull/2162): Corrected a hash modification while iterating issue - [@Jack12816](https://github.com/Jack12816).
13+
* [#2164](https://github.com/ruby-grape/grape/pull/2164): Fix: `coerce_with` is now called for params with `nil` value - [@braktar](https://github.com/braktar).
1314

1415
### 1.5.2 (2021/02/06)
1516

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,7 @@ params do
12281228
end
12291229
end
12301230
```
1231+
Note that, a `nil` value will call the custom coercion method, while a missing parameter will not.
12311232

12321233
Example of use of `coerce_with` with a lambda (a class with a `parse` method could also have been used)
12331234
It will parse a string and return an Array of Integers, matching the `Array[Integer]` `type`.

UPGRADING.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
11
Upgrading Grape
22
===============
33

4+
5+
### Upgrading to >= 1.5.3
6+
7+
### Nil value and coercion
8+
9+
Prior to 1.2.5 version passing a `nil` value for a parameter with a custom coercer would invoke the coercer, and not passing a parameter would not invoke it.
10+
This behavior was not tested or documented. Version 1.3.0 quietly changed this behavior, in such that `nil` values skipped the coercion. Version 1.5.3 fixes and documents this as follows:
11+
12+
```ruby
13+
class Api < Grape::API
14+
params do
15+
optional :value, type: Integer, coerce_with: ->(val) { val || 0 }
16+
end
17+
18+
get 'example' do
19+
params[:my_param]
20+
end
21+
get '/example', params: { value: nil }
22+
# 1.5.2 = nil
23+
# 1.5.3 = 0
24+
get '/example', params: {}
25+
# 1.5.2 = nil
26+
# 1.5.3 = nil
27+
end
28+
```
29+
See [#2164](https://github.com/ruby-grape/grape/pull/2164) for more information.
30+
431
### Upgrading to >= 1.5.1
532

633
#### Dependent params

0 commit comments

Comments
 (0)