luckyframework / lucky

A full-featured Crystal web framework that catches bugs for you, runs incredibly fast, and helps you write code that lasts.
https://luckyframework.org
MIT License
2.57k stars 156 forks source link

Building a route with array params doesn't work #1880

Open jwoertink opened 2 months ago

jwoertink commented 2 months ago

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\"]"]},
akadusei commented 1 month ago

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

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