Skip to content

Request parameters cannot bind to Optional<List<String>> [SPR-13418] #17997

Closed

Description

@spring-projects-issues

Kazuki Shimizu opened SPR-13418 and commented

Request parameter cannot bind to Optional<List<String>> at the @RequestMapping method.

I define the following method as handler method(@RequestMapping method).

@RequestMapping(path="search")
public String search(@RequestParam(defaultValue="word1,word2,word3") Optional<List<String>> words, Model model) {
    List<String> searchWords = words.get();
    System.out.println(searchWords);
    //...
}

I submit a request to http://localhost:8080/myApp/search?words=aaa&words=bbb.
In this case, i was expected a printing as follow:

[aaa, bbb]

But actually, printed as follow:

[aaa]

In this case, request parameters can be bind to first element only.

In addition, i submit a request to http://localhost:8080/myApp/search. (without request parameters)
In this case, printed as follow:

[word1, word2, word3]

In this case,default value converted by conversion service was applied.

This behavior is bug or specification ?

I will submit a repro project at the later.


Affects: 4.2.1

Issue Links:

0 votes, 5 watchers

Activity

  1. spring-projects-issues commented on Sep 1, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Kazuki Shimizu commented

    I've submit PR of repro project.

  2. spring-projects-issues commented on Sep 2, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    It looks like when a request parameter has 2 values the ArrayToObjectConverter kicks in, which tries to convert the first value in the array to the target Object. Since ObjectToOptionalConverter can do just that, we end up with only the first request parameter value used. The case when the @RequestParam default value is used works because in that scenario the conversion is from a comma-separated String.

    As a workaround in this case you can remove Optional. The use of defaultValue implies its optional and it will always have a value.

  3. spring-projects-issues commented on Sep 2, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    Juergen Hoeller, it seems that ObjectToOptionalConverter ought to be registered earlier, perhaps ahead of the collection converters?

  4. spring-projects-issues commented on Sep 2, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Rossen Stoyanchev, indeed, worth a try...

    Juergen

  5. spring-projects-issues commented on Sep 2, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Kazuki Shimizu commented

    Thanks for comments!!

    As a workaround in this case you can remove Optional.

    I know the workaround and tried it.
    Actually, i want to use without defaultValue attribute as follow.
    (in other words, i want to apply a default value that decided by programmatic)

    e.g.)

    @RequestMapping(path="search")
    public String search(@RequestParam String type, @RequestParam Optional<List<String>> words, Model model) {
        model.addAttribute("words", String.valueOf(words.orElseGet(()->defaultValue(type))));
        // ...
    }
    
    private List<String> defaultValue(String type){
        // it is dummy code.
        return "1".equals(type) ?
                Arrays.asList("word1", "word2", "word3") :
                Arrays.asList("word4", "word5", "word6");
    }
  6. spring-projects-issues commented on Sep 4, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Let's do this in a broader fashion for 4.3, making sure Optional declarations work for collections across the framework. In the end, we never really defined the semantics there.

    For the time being, when not using defaultValue, you could also declare @RequestParam(required=false). Optional is effectively just a syntactic variation of that.

    Juergen

  7. spring-projects-issues commented on Aug 26, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Tor Arne Kvaløy commented

    As of version 4.3.2 I still only get the first element in the list.

    @RequestParam(required = false) Optional<List<String>> locations
  8. added
    type: enhancement
    in: web
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webtype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions