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

Compatability issue with hanami-controller 2.0.0.alpha1 #307

Closed
graudeejs opened this issue Feb 2, 2020 · 2 comments
Closed

Compatability issue with hanami-controller 2.0.0.alpha1 #307

graudeejs opened this issue Feb 2, 2020 · 2 comments

Comments

@graudeejs
Copy link

graudeejs commented Feb 2, 2020

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
Copy link

k0va1 commented Mar 9, 2020

Yep, totally agree. I have the same issue

@graudeejs
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants