Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attributes with nil values should not be considered "missing". #864

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Next Release
============

* Your contribution here.
* [#864](https://github.com/intridea/grape/pull/864): Attributes with nil values should not be considered "missing"Your contribution here. - [@ppadron](https://github.com/ppadron).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put back this line! :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also note where those attributes are considered missing or not. This is about calling declared_params.


0.10.0 (12/19/2014)
===================
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,29 @@ curl -X POST -H "Content-Type: application/json" localhost:9292/users/signup -d
}
````

Note that an attribute with a `nil` value is not considered *missing* and will also be returned
when `include_missing` is set to `false`:

**Request**

````bash
curl -X POST -H "Content-Type: application/json" localhost:9292/users/signup -d '{"user": {"first_name":"first name", "last_name": null, "address": { "city": "SF"}}}'
````

**Response with include_missing:false**

````json
{
"declared_params": {
"user": {
"first_name": "first name",
"last_name": null,
"address": { "city": "SF"}
}
}
}
````

## Parameter Validation and Coercion

You can define validations and coercion options for your parameters using a `params` block.
Expand Down
6 changes: 6 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Upgrading Grape
===============

### Upgrading to Next Release

#### Changes to `include_missing`

Attributes with `nil` values or with valus that evaluate to `false` are no longer considered *missing* and will be returned when `include_missing` is `false`.

### Upgrading to >= 0.10.0

#### Changes to content-types
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def declared(params, options = {}, declared_params = nil)
key.each_pair do |parent, children|
output_key = options[:stringify] ? parent.to_s : parent.to_sym

next unless options[:include_missing] || children || params[parent]
next unless options[:include_missing] || children || params.key?(parent)

if params.key?(parent) || options[:include_missing]
hash[output_key] = if children
Expand Down
36 changes: 34 additions & 2 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,38 @@ def app
expect(last_response.status).to eq(200)
end

it 'should not consider as a missing attribute a value that evaluates to false' do
subject.params do
requires :first
optional :boolean
end

subject.post '/declared' do
error!('expected false', 400) if declared(params, include_missing: false)[:boolean] != false
''
end

post '/declared', MultiJson.dump(first: 'one', boolean: false), 'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq(201)
end

it 'should not consider `nil` as a missing attribute' do
subject.params do
requires :first
optional :second
end

subject.post '/declared' do
unless declared(params, include_missing: false)[:second].nil?
error!('expected nil', 400)
end
''
end

post '/declared', MultiJson.dump(first: 'one', second: nil), 'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq(201)
end

it 'does not include missing attributes when there are nested hashes' do
subject.get '/dummy' do
end
Expand Down Expand Up @@ -356,9 +388,9 @@ def app

expect(last_response.status).to eq(200)
expect(inner_params[:first]).to eq 'present'
expect(inner_params[:nested].keys).to eq [:fourth, :nested_nested]
expect(inner_params[:nested].keys).to eq [:fourth, :fifth, :nested_nested]
expect(inner_params[:nested][:fourth]).to eq ''
expect(inner_params[:nested][:nested_nested].keys).to eq [:sixth]
expect(inner_params[:nested][:nested_nested].keys).to eq [:sixth, :seven]
expect(inner_params[:nested][:nested_nested][:sixth]).to eq 'sixth'
end
end
Expand Down