Skip to content

Commit 279da2a

Browse files
author
Braktar
committed
Update md files
1 parent aa72fa7 commit 279da2a

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-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): coerce_with is 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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
Upgrading Grape
22
===============
33

4+
5+
### Upgrading to >= 1.5.3
6+
7+
### Nil value and coercion
8+
9+
A parameter with a value set to `nil` call custom coercion method, but the coercion is not triggered for missing parameters.
10+
11+
```ruby
12+
class Api < Grape::API
13+
params do
14+
optional :value, type: Integer, coerce_with: ->(val) { val || 0 }
15+
end
16+
17+
get 'example' do
18+
params[:my_param]
19+
end
20+
get '/example', params: { value: nil }
21+
# 1.5.2 = nil
22+
# 1.5.3 = 0
23+
get '/example', params: {}
24+
# 1.5.2 = nil
25+
# 1.5.3 = nil
26+
end
27+
```
28+
429
### Upgrading to >= 1.5.1
530

631
#### Dependent params

0 commit comments

Comments
 (0)