Skip to content

Commit ec3bd8d

Browse files
author
Tim Connor
committed
Update README / CHANGELOG / UPGRADING
1 parent f0781fc commit ec3bd8d

File tree

3 files changed

+66
-9
lines changed

3 files changed

+66
-9
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#### Fixes
88

99
* Your contribution here.
10+
* [#2103](https://github.com/ruby-grape/grape/pull/2103): Ensure complete declared params structure is present - [@tlconnor](https://github.com/tlconnor).
1011
* [#2099](https://github.com/ruby-grape/grape/pull/2099): Added truffleruby to Travis-CI - [@gogainda](https://github.com/gogainda).
1112
* [#2089](https://github.com/ruby-grape/grape/pull/2089): Specify order of mounting Grape with Rack::Cascade in README - [@jonmchan](https://github.com/jonmchan).
1213
* [#2083](https://github.com/ruby-grape/grape/pull/2083): Set `Cache-Control` header only for streamed responses - [@stanhu](https://github.com/stanhu).

README.md

+48-5
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ use Rack::Session::Cookie
353353
run Rack::Cascade.new [Web, API]
354354
```
355355

356-
Note that order of loading apps using `Rack::Cascade` matters. The grape application must be last if you want to raise custom 404 errors from grape (such as `error!('Not Found',404)`). If the grape application is not last and returns 404 or 405 response, [cascade utilizes that as a signal to try the next app](https://www.rubydoc.info/gems/rack/Rack/Cascade). This may lead to undesirable behavior showing the [wrong 404 page from the wrong app](https://github.com/ruby-grape/grape/issues/1515).
356+
Note that order of loading apps using `Rack::Cascade` matters. The grape application must be last if you want to raise custom 404 errors from grape (such as `error!('Not Found',404)`). If the grape application is not last and returns 404 or 405 response, [cascade utilizes that as a signal to try the next app](https://www.rubydoc.info/gems/rack/Rack/Cascade). This may lead to undesirable behavior showing the [wrong 404 page from the wrong app](https://github.com/ruby-grape/grape/issues/1515).
357357

358358

359359
### Rails
@@ -787,7 +787,12 @@ Available parameter builders are `Grape::Extensions::Hash::ParamBuilder`, `Grape
787787

788788
### Declared
789789

790-
Grape allows you to access only the parameters that have been declared by your `params` block. It filters out the params that have been passed, but are not allowed. Consider the following API endpoint:
790+
Grape allows you to access only the parameters that have been declared by your `params` block. It will:
791+
792+
* Filter out the params that have been passed, but are not allowed.
793+
* Include any optional params that are declared but not passed.
794+
795+
Consider the following API endpoint:
791796

792797
````ruby
793798
format :json
@@ -820,9 +825,9 @@ Once we add parameters requirements, grape will start returning only the declare
820825
format :json
821826

822827
params do
823-
requires :user, type: Hash do
824-
requires :first_name, type: String
825-
requires :last_name, type: String
828+
optional :user, type: Hash do
829+
optional :first_name, type: String
830+
optional :last_name, type: String
826831
end
827832
end
828833

@@ -850,6 +855,44 @@ curl -X POST -H "Content-Type: application/json" localhost:9292/users/signup -d
850855
}
851856
````
852857

858+
Missing params that are declared as type `Hash` or `Array` will be included.
859+
860+
````ruby
861+
format :json
862+
863+
params do
864+
optional :user, type: Hash do
865+
optional :first_name, type: String
866+
optional :last_name, type: String
867+
end
868+
optional :widgets, type: Array
869+
end
870+
871+
post 'users/signup' do
872+
{ 'declared_params' => declared(params) }
873+
end
874+
````
875+
876+
**Request**
877+
878+
````bash
879+
curl -X POST -H "Content-Type: application/json" localhost:9292/users/signup -d '{}'
880+
````
881+
882+
**Response**
883+
884+
````json
885+
{
886+
"declared_params": {
887+
"user": {
888+
"first_name": null,
889+
"last_name": null
890+
},
891+
"widgets": []
892+
}
893+
}
894+
````
895+
853896
The returned hash is an `ActiveSupport::HashWithIndifferentAccess`.
854897

855898
The `#declared` method is not available to `before` filters, as those are evaluated prior to parameter coercion.

UPGRADING.md

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
Upgrading Grape
22
===============
33

4+
### Upgrading to >= 1.4.1
5+
6+
Previous to 1.3.3, the `declared` helper would always return the complete params structure if `include_missing=true` was set. In 1.3.3 the behavior was changed, so a missing Hash with or without nested parameters would always resolve as `{}`.
7+
8+
In 1.4.1 this behavior is reverted, so the whole params structure will always be available via `declared`, regardless of whether any params are passed.
9+
10+
The following rules now apply to the `declared` helper when params are missing and `include_missing=true`:
11+
12+
* Hash params with childern will resolve as a hash with keys for each declared child.
13+
* Hash params with no children will resolve as `{}`.
14+
* Array params will resolve as `[]`.
15+
* All other params will resolve as `null`.
16+
417
### Upgrading to >= 1.4.0
518

619
#### Reworking stream and file and un-deprecating stream like-objects
@@ -28,17 +41,17 @@ class API < Grape::API
2841
end
2942
```
3043

31-
Or use `stream` to stream other kinds of content. In the following example a streamer class
44+
Or use `stream` to stream other kinds of content. In the following example a streamer class
3245
streams paginated data from a database.
3346

3447
```ruby
35-
class MyObject
48+
class MyObject
3649
attr_accessor :result
3750

3851
def initialize(query)
3952
@result = query
4053
end
41-
54+
4255
def each
4356
yield '['
4457
# Do paginated DB fetches and return each page formatted
@@ -47,7 +60,7 @@ class MyObject
4760
yield process_records(records, first)
4861
first = false
4962
end
50-
yield ']'
63+
yield ']'
5164
end
5265

5366
def process_records(records, first)

0 commit comments

Comments
 (0)