ruby-grape / ruby-grape/grape

Passing an array of string to params expecting an array of hash raises NoMethodError

Open
#1,471 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug?
Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

This is a bug report. I can probably fix it, but wanted to create an issue first.

In my API, I have declared something like the following:

# class
class Foo < Grape::API
  desc "Do foo"
  params do
    requires :foo, type: Array do
      optional :bar # Expects { "foo": [{ "bar": "baz" }] }
    end
  end
  post do
    puts declared(params)
  end
end

# specs
RSpec.describe Foo do
  it 'does do foo' do
    params = { foo: [""] }
    post "/", params, {} do
      # raises 500 error
    end
  end
end

It is raising an error because of https://github.com/ruby-grape/grape/blob/master/lib/grape/dsl/inside_route.rb#L39-L50. In line 41, params || {} is supposed to generate a hash, but for non-falsy value, it raises NoMethodError key? because a string does not respond to key?.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with lib/grape/dsl/inside_route.rb at lines 39-50, then reproduce the reported request using the Foo API and RSpec example in the issue. Trace how an array containing a string is handled by the parameter declaration; done means the request no longer raises NoMethodError and the resulting behavior is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.