Amber: [Router] Allow multiple values for a single parameter key

Created on 29 Jan 2018  路  10Comments  路  Source: amberframework/amber

Description

Amber::Router::ParamsParser can't handle multiple values for a parameter.

      HTTP::Params.parse(values) do |key, value|
        params[key] = value

This always overwrites previous values where params is a Amber::Router::Params < Hash(String, String) .

Steps to Reproduce

require "amber"

request  = HTTP::Request.new("GET", "/?foo=bar&foo=baz")
response = HTTP::Server::Response.new(IO::Memory.new)
context  = HTTP::Server::Context.new(request, response)
context.params["foo"] # => "baz"

Expected behavior:

We should provide another method to fetch all values like Rails, Crystal, Kemal and any other libraries.

Crystal

require "http/params"

params = HTTP::Params.parse("foo=bar&foo=baz")
params.fetch("foo")     # => "bar"
params.fetch_all("foo") # => ["bar", "baz"]

Kemal

require "kemal"

request  = HTTP::Request.new("GET", "/?foo=bar&foo=baz")
params = Kemal::ParamParser.new(request)
params.query["foo"]           # => "bar"
params.query.fetch_all("foo") # => ["bar", "baz"]

Versions

  • Amber CLI (amberframework.org) - v0.6.4
  • Crystal 0.24.1 (2017-12-22)

Amber is a quite cool design, and I love it. 馃槃
I thinks it is time to polish it for production use! If no one has started this problem, I will try it.

Best regards,

enhancement

Most helpful comment

@marksiemers and I played with this last night and decided to create a fetch_all method which re-parses request and returns the entire array of params. This allows params to continue to function how they are without additional slow downs for most params which aren't expected to return arrays.

All 10 comments

In amber we've tried to unify all types of params into params rather than having to call params.query, params.body etc. This makes this a bit more difficult. I'm going to work on this for a bit.

I believe this can be as simple as just appending to the param key @elorest

@maiha thank you for the thorough submission.

I don't recall anything like this from Rails, and I don't see anything in their codebase that shows anything like params.fetch_all. My experience with it says later parameters simply overwrite so previous parameters aren't available. Unless you're thinking of hash and array parameters?

Apparently there is no standard (9th slide) for what to do when passing multiple parameters with the same name.

What's the benefit to passing multiple parameters with the same name?

Hi @robacarp

What's the benefit to passing multiple parameters with the same name?

It is necessary to process the select multiple form.
For example, <select name=lang multiple> would post parameters like this.

lang=ruby&lang=crystal

Thanks,

In Rails you use a params array:

<select name="job[]" size="5" multiple="multiple">

The ending [] tells it you are sending an array.

@eliasjpr I'll submit something soon and you can let me know what you think. I've looked at kemals current implementation, and played with a way to make it work without changing our end functionality.

I think I like the [] functionality like Rails and php have. At least that鈥檚 what鈥檚 familiar to me. But I assume it鈥檇 be easier to implement a known array type returning parameter method than to give the param method the possibility of returning an array for every param field.

Looking forward to a pull request!

It's difficult to add this functionality without breaking current functionality I care more about. I'm still trying a couple things though. @eliasjpr I don't understand your statement above, could you elaborate?

I believe this can be as simple as just appending to the param key @elorest

@marksiemers and I played with this last night and decided to create a fetch_all method which re-parses request and returns the entire array of params. This allows params to continue to function how they are without additional slow downs for most params which aren't expected to return arrays.

Fixed with #637

Was this page helpful?
0 / 5 - 0 ratings

Related issues

blankoworld picture blankoworld  路  7Comments

ZeroPointEnergy picture ZeroPointEnergy  路  4Comments

drujensen picture drujensen  路  6Comments

faustinoaq picture faustinoaq  路  6Comments

conradwt picture conradwt  路  3Comments