lucky icon indicating copy to clipboard operation
lucky copied to clipboard

Building a route with array params doesn't work

Open jwoertink opened this issue 1 year ago • 1 comments

If you have an action that specifies an array param, you're not able to build a route from that action.

class Reports::Query < ApiAction
  param codes : Array(String)

  get "/reports/query" do
    results = ReportQuery.new.codes(codes)

    json(ReportSerializer.for_collection(results))
  end
end

# then in your spec for this

it "doesn't work" do
  client = ApiClient.new
  response = client.exec(Reports::Query.with(codes: ["a", "b", "c"]))
  response.status_code.should eq(200) # but it's actually 400 here
end

The route that's built looks more like

/reports/query?codes=%5B%22a%22%2C+%22b%22%2C+%22c%22%5D

Which basically looks like

@query_params=URI::Params{"codes" => ["[\"a\", \"b\", \"c\"]"]},

jwoertink avatar Apr 29 '24 20:04 jwoertink

I ran into a Lucky::MissingParamError with this a while back. My workaround looked like this:

params = URI::Params.encode({"codes[]": ["a", "b"]})
response = client.get("#{Reports::Query.url_without_query_params}?#{params}")
response.status_code.should eq(200)

akadusei avatar May 15 '24 23:05 akadusei