hanami/controller

Compatability issue with hanami-controller 2.0.0.alpha1

graudeejs opened this issue · 2 comments

Hello!
I'm trying to update hanami-router and hanami-controller in one of my dev projects to 2.0.0.alpha1

One thing that I came across is that hanami controller just doesn't work as it should by rack spec (perhaps that's expected)

Gemfile

source "https://rubygems.org"

gem 'hanami-router', '~> 2.0.0.alpha1'
gem 'hanami-controller', '~> 2.0.0.alpha1' 

config.ru

# frozen_string_literal: true

require 'hanami/router'
require 'hanami/controller'

class TestController < Hanami::Action
  def handle(_req, res)
    res.body = "ok"
    res.status = 200
  end
end

app = Hanami::Router.new(configuration: Hanami::Controller::Configuration.new) do
  get '/', to: "test_controller"
end

run app

This raises NoMethodException when requesting /.

Ruby | /home/graudeejs/.asdf/installs/ruby/2.7.0/lib/ruby/gems/2.7.0/gems/rack-2.1.2/lib/rack/lint.rb: in block in check_status, line 621

The problem is that hanami controller doesn't return rack compatible return value (it returns an instance of Hanami::Action::Response. This would be fine, if router somehow magically would #to_a it.

>> TestController.new(configuration: Hanami::Controller::Configuration.new).call({})

#<Hanami::Action::Response:0x0000558af2447078
 @action="TestController",
 @block=nil,
 @body=["ok"],
 @buffered=true,
 @charset="utf-8",
 @configuration=
  #<Hanami::Controller::Configuration:0x0000558af2447c80
   @cookies={},
   @default_charset=nil,
   @default_headers={},
   @default_request_format=nil,
   @default_response_format=nil,
   @formats={"application/octet-stream"=>:all, "*/*"=>:all, "text/html"=>:html},
   @handled_exceptions={},
   @mime_types=nil,
   @public_directory="/home/graudeejs/src/hanami2-routing/public",
   @root_directory=#<Pathname:/home/graudeejs/src/hanami2-routing>>,
 @env={"REQUEST_METHOD"=>"GET"},
 @exposures={:params=>#<Hanami::Action::BaseParams:0x0000558af2447370 @env={"REQUEST_METHOD"=>"GET"}, @params={}, @raw={}>, :format=>:all},
 @format=:all,
 @header={"Content-Type"=>"application/octet-stream; charset=utf-8", "Content-Length"=>"2"},
 @length=2,
 @sending_file=false,
 @status=200,
 @writer=
  #<Method: Hanami::Action::Response(Rack::Response::Helpers)#append(chunk) /home/graudeejs/.asdf/installs/ruby/2.7.0/lib/ruby/gems/2.7.0/gems/rack-2.1.2/lib/rack/response.rb:244>>
>> TestController.new(configuration: Hanami::Controller::Configuration.new).call({}).to_a
[200, {"Content-Type"=>"application/octet-stream; charset=utf-8", "Content-Length"=>"2"}, ["ok"]]

So the question is: Am I doing something very wrong? If so how to fix it correctly in this example.

P.S.
For now will use workaround:

class BaseAction < Hanami::Action
  def call(env)
    super.to_a
  end
end

class TestController < BaseAction
  def handle(_req, res)
    res.body = "ok"
    res.status = 200
  end
end
k0va1 commented

Yep, totally agree. I have the same issue

After updating router to 2.0.0.alpha3 the issue is gone (didn't check 2.0.0.alpha2)