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

The router crashes when the body is a JSON array #85

Closed
weppos opened this issue Jan 22, 2016 · 0 comments
Closed

The router crashes when the body is a JSON array #85

weppos opened this issue Jan 22, 2016 · 0 comments
Assignees
Labels
Milestone

Comments

@weppos
Copy link
Member

weppos commented Jan 22, 2016

I was able to reproduce the error here.

  1) Error:
Body parsing#test_0002_is successful (JSON) with an array:
TypeError: no implicit conversion of Array into Hash
    /Users/weppos/Mirrors/hanami--router/lib/hanami/routing/parsers.rb:49:in `merge!'
    /Users/weppos/Mirrors/hanami--router/lib/hanami/routing/parsers.rb:49:in `block in _redefine_call'
    /Users/weppos/Mirrors/hanami--router/lib/hanami/routing/http_router.rb:135:in `raw_call'
    /Users/weppos/.rvm/gems/ruby-2.3.0/gems/http_router-0.11.2/lib/http_router.rb:142:in `call'
    /Users/weppos/Mirrors/hanami--router/lib/hanami/router.rb:931:in `call'
    /Users/weppos/.rvm/gems/ruby-2.3.0/gems/rack-1.6.4/lib/rack/lint.rb:49:in `_call'
    /Users/weppos/.rvm/gems/ruby-2.3.0/gems/rack-1.6.4/lib/rack/lint.rb:37:in `call'
    /Users/weppos/.rvm/gems/ruby-2.3.0/gems/rack-1.6.4/lib/rack/mock.rb:74:in `request'
    /Users/weppos/.rvm/gems/ruby-2.3.0/gems/rack-1.6.4/lib/rack/mock.rb:59:in `patch'
    /Users/weppos/Mirrors/hanami--router/test/integration/body_parsing_test.rb:27:in `block (2 levels) in <top (required)>'
  it 'is successful (JSON) with an array' do
    body     = StringIO.new( %(["alpha", "beta"]).encode(Encoding::ASCII_8BIT) )
    response = @app.patch('/books/23', 'CONTENT_TYPE' => 'application/json', 'rack.input' => body, lint: true)

    response.status.must_equal 200
    response.body.must_equal %(["alpha", "beta"])
  end

This is an example of a request

$ curl http://example.com/path -X POST -d '["alpha", "beta"]' -H "Content-Type: application/json"

Of course, the tricky part is how this case should be handled in the params as it implies the params can also be an Array, instead of always a Hash.

It's possible to use a constant key, for example _, which is also the same approach Rails is following.

{"_json"=>["ns1.example.com", "ns2.example.com"], "controller"=>"something", "action"=>"create", "something"=>{"_json"=>["ns1.example.com", "ns2.example.com"]}}

In this case, Rails uses the _json key.

FYI, Lotus crashes at this point, because merge! can't handle an input which is not a Hash.

          env[ROUTER_PARAMS].merge!(
            @parsers[
              media_type(env)
            ].parse(body)
          )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants