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

Uri.splitQueryString only return the last value if parameter is entered multiple times #28142

Closed
rwrozelle opened this issue Dec 16, 2016 · 3 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core type-enhancement A request for a change that isn't a bug

Comments

@rwrozelle
Copy link

SplitQueryString only returns the last value if the parameter is entered multiple times, for example:
"foo=bar&fi=fo&fi=fum" returns {"foo": "bar", "fi": "fum"}
I would prefer it to return a list of strings: {"foo": "bar", "fi":["fo","fum"]}

Below is example code of the type of change I would like, this is just an example, so please ammend if a more concise change would be more appropriate:

  Static Map<String, Object> splitQueryString(String query, 
                                              {Encoding encoding: UTF8}) {
    return query.split("&").fold({}, (map, element) {
      int index = element.indexOf("=");
      if (index == -1) {
        if (element != "") {
          map[Uri.decodeQueryComponent(element, encoding: encoding)] = "";
        }
      } else if (index != 0) {
        var key = element.substring(0, index);
        var value = element.substring(index + 1);
        String decodedKey = Uri.decodeQueryComponent(key, encoding: encoding);
        String decodedValue = Uri.decodeQueryComponent(value, encoding: encoding);
        if (map[decodedKey] == null) {
          map[decodedKey] = decodedValue;
        }
        else {
          if (map[decodedKey] is List) {
            List<String> listValue = map[decodedKey] as List<String>;
            listValue.add(decodedValue);
          }
          else {
            List<String> listValue = new List<String>();
            listValue.add(map[decodedKey]);
            listValue.add(decodedValue);
            map[decodedKey] = listValue;
          }
        }
      }
      return map;
    });
  }
@lrhn
Copy link
Member

lrhn commented Dec 16, 2016

That would be a breaking change at this point, so it would be better to add a separate function with the new functionality.

That function doesn't actually need to be on Uri, it could also just be added to a helper package

@bobjackman
Copy link

+1

@lrhn
Copy link
Member

lrhn commented Feb 23, 2017

You can now use the queryParametersAll method which returns a Map<String,List<String>>. That does mean that all query values are returned as lists, even those that only have one element.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

3 participants