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

Order of Form Parameters is not preserved (application/x-www-form-urlencoded) #447

Open
Marc2017 opened this issue Jan 20, 2020 · 5 comments
Labels

Comments

@Marc2017
Copy link

Play WS Version (2.7.2 / etc)
JAVA

In StandaloneAhcWSRequest the order of Form-Parameters is not preserved:


                    // XXX shouldn't the encoding be same as charset?
                    Map<String, List<String>> stringListMap = FormUrlEncodedParser.parseAsJava(stringBody, "utf-8");
                    stringListMap.forEach((key, values) -> values.forEach(value -> builder.addFormParam(key, value)));

"FormUrlEncodedParser.parseAsJava" does not preserver the order, and "stringListMap" is unordered.

@wsargent
Copy link
Member

Or is it that the asJava method doesn't retain sorting order?

If you call

  def parseAsJava(data: String, encoding: String): java.util.Map[String, java.util.List[String]] = {
    import scala.collection.JavaConverters._
    val insertHashMap = new util.LinkedHashMap[String, java.util.List[String]]()
    parse(data, encoding).foreach {
      case (key, values) =>
        insertHashMap.put(key, values.asJava)
    }
    insertHashMap
  }

  def parseAsJavaArrayValues(data: String, encoding: String): java.util.Map[String, Array[String]] = {
    val insertHashMap = new util.LinkedHashMap[String, Array[String]]()
    parse(data, encoding).foreach {
      case (key, values) =>
        insertHashMap.put(key, values.toArray)
    }
    insertHashMap
  }

does that change things for you?

@Marc2017
Copy link
Author

Hi,

seems I have a different implementation:

def parseAsJava(data: String, encoding: String): java.util.Map[String, java.util.List[String]] = {
import scala.collection.JavaConverters._
parse(data, encoding).map {
case (key, values) =>
key -> values.asJava
}.asJava
}

there is no "foreach", there is a "map" - which will change the order.

I´ll check my revision.

Thanks!
Marc

@Marc2017
Copy link
Author

Did you change it to "foreach" in your example?
In the current revision there is also a "map".

@wsargent
Copy link
Member

@Marc2017 yeah, the example above is using a foreach because it uses an insertion order linked hash map. It's not what's in the master branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants