ruby-grape / ruby-grape/grape

Avoid returning `[nil]` to Rack

Open
#2,118 8 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug?
Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

Prior to Rack v2.1.0, Rack::Response would call to_s on each of the body parts: https://github.com/rack/rack/blob/85684323f8f58409e717af91e446d257d496f8b8/lib/rack/response.rb#L40-L43. That means if Grape returned [nil], Rack would handle this gracefully.

This is no longer the case now with https://github.com/rack/rack/pull/1434/files. It's easy to have an API call that returns nil to Grape, and the body will be set to [nil]: https://github.com/ruby-grape/grape/blob/0d5cf4035c04c1c44e7868a8b77a969b28766245/lib/grape/endpoint.rb#L267-L277

This causes fits with Rack:

NoMethodError (undefined method `empty?' for nil:NilClass):
rack (2.1.4) lib/rack/etag.rb:70:in `block in digest_body'
rack (2.1.4) lib/rack/body_proxy.rb:34:in `block in each'
rack (2.1.4) lib/rack/body_proxy.rb:34:in `each'
rack (2.1.4) lib/rack/body_proxy.rb:34:in `each'
rack (2.1.4) lib/rack/etag.rb:68:in `digest_body'
rack (2.1.4) lib/rack/etag.rb:31:in `call'
rack (2.1.4) lib/rack/conditional_get.rb:40:in `call'
rack (2.1.4) lib/rack/head.rb:14:in `call'

Maybe Grape should return nil instead of [nil] if there is no body? This might have to be done in the formatters since nil may mean null in JSON.

Rack issue: https://github.com/rack/rack/issues/1712

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/grape/endpoint.rb at the body assignment around lines 267-277, then inspect the formatters mentioned in the issue and Rack's changed response handling. Reproduce an endpoint returning nil with Rack 2.1.4 and determine whether an empty body must be returned differently from a JSON null value. Add or update coverage for both cases if the relevant test location is identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.