-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Avoid returning [nil]
to Rack
#2118
Comments
I'm going to close this issue because this problem seems to have gone away in Rack v2.2.3, but v2.1.x has this issue. rack/rack#1535 might have helped. |
Closed too soon. I got this with v2.2.3 as well. |
Based on rack/rack#1713 (comment), it sounds like Grape should flag responses that don't contain String values? |
Do we want to raise errors in Grape for this? Feels like that would be a no, because a contract is a contract, but I'm open to it. |
I created this middleware: class ResponseCoercerMiddleware < ::Grape::Middleware::Base
def call(env)
response = super(env)
status = response[0]
body = response[2]
return response if Rack::Utils::STATUS_WITH_NO_ENTITY_BODY[status]
return response unless body.is_a?(Array)
body.map! do |part|
if part.is_a?(String)
part
else
part.to_s
end
end
response
end
end In test and development, I modified this to raise an exception when |
I wonder if we want to add some variant of this to the base middleware. |
Just got hit by this as well. Is there a blessed way to fix this issue? |
@ebenoist Care to dig up whether this is the case un latest Rack and what the Rack API is supposed to be? Seems like there's no conclusion above. |
Prior to Rack v2.1.0,
Rack::Response
would callto_s
on each of thebody
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]
:grape/lib/grape/endpoint.rb
Lines 267 to 277 in 0d5cf40
This causes fits with Rack:
Maybe Grape should return
nil
instead of[nil]
if there is no body? This might have to be done in the formatters sincenil
may meannull
in JSON.Rack issue: rack/rack#1712
The text was updated successfully, but these errors were encountered: