jooby-project / jooby

The modular web framework for Java and Kotlin
https://jooby.io
Apache License 2.0
1.71k stars 197 forks source link

Empty parameters are treated as missing #431

Closed mdesharnais closed 8 years ago

mdesharnais commented 8 years ago

Given the following server:

import org.jooby.*;

public class Main extends Jooby {
  {
    get("/test", req -> req.param("foo").toOptional().toString() + "\n");
  }

  public static void main(final String[] args) throws Throwable {
    run(Main::new, args);
  }
}

Empty parameters are treated as missing:

$ curl localhost:8080/test
Optional.empty
$ curl localhost:8080/test?foo
Optional.empty
$ curl localhost:8080/test?foo=
Optional.empty
$ curl localhost:8080/test?foo=abc
Optional[abc]

One sometime wants to distinguish between an absent and empty parameter. I believe the result should be:

Any thought on this?

jknack commented 8 years ago

I introduced this change in previous release. The motivation was form submits where if you leave some field blank you get an empty string for me this should be treat as missing, simplify form validation.

What is the use case for treating empty parameter as empty list? For example:

get("/test", req -> req.param("foo").toList() + "\n");

Give you [] for ?foo or ?foo=

wenerme commented 8 years ago

Elasticsearch have a ?pretty to pretty format the json result.

jknack commented 8 years ago

Odd, but how an empty list will help there? Still not sure if ?pretty is a valid use case... feel empty string as optional works better in general.

jknack commented 8 years ago

well, got some issues/questions about this in the google group... think going to revert the changes I did... will that work for you guys?

mdesharnais commented 8 years ago

Hello, I am a little bit late on the train, but @wenerme gave the exact example I was referring to: a flag to enable a functionality.

jknack commented 8 years ago

was reverted found another way of handling absent/empty string while submitting forms

👍