Skip to content

Incompatibility with Rack 2.1.0 and higher #1980

Closed
@ignatbagaev

Description

@ignatbagaev

Hi!
I use Grape in my application and after updating Rack to 2.1.1, I get an error
NoMethodError - undefined method '[]' for nil:NilClass without any backtrace. During debugging I found that it was raising in Grape::Router#cascade?

def cascade?(response)
  response && response[1][Grape::Http::Headers::X_CASCADE] == 'pass'
end

and the response looks like

[#<Rack::Response:0x00007fc561a338c8 @status=200, @header={"Content-Type"=>"application/json"}, @writer=#<Method: Rack::Response(Rack::Response::Helpers)#append>, @block=nil, @length=0, @body=["{\"foo\":\"bar\"}"], @buffered=false>, nil, nil]

I made some investigation and found that Rack team removed #to_ary from Rack::Response in version 2.1.0 and this change made the result of Grape::MIddleware::Formatter#build_formatted_response not implicitly convertable to Array. This is why I had response like a mentioned above instead of response like

[response_status, response_headers, response_body]

To fix this behavior just #finish an instance of Rack::Response in Formatter middleware:
change

Rack::Response.new(bodymap, status, headers)

at https://github.com/ruby-grape/grape/blob/master/lib/grape/middleware/formatter.rb#L49 to

Rack::Response.new(bodymap, status, headers).finish

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions