Hello to everyone.

I am having a problem with MultiValueMap.

I am trying to get a MultiValueMap<MyEnum, String> map as input of a GET request, but Spring gives me a MultiValueMap<String, String> cast to MultiValueMap<MyEnum, String> (idk how he does that).

So there is an inconsistency somehow, and every string passed as a parameter is used as key even if it isn't part of MyEnum.

For instance, if I try to get the values like map.get(MyEnum.X) it returns nothing, while if I do map.get(MyEnum.X.toString()) it works.

The funny thing here is that map.get() wants a value of MyEnum and not a String...

In practice, if my enum is {banana, apple, pear} and I pass strawberry=1 as a parameter in the request, the resulting map will be:

{
   strawberry; 1
}

even if strawberry is not part of the enum.

I fixed this by getting as input MultiValueMap<String, String>, but I decided to report this issue anyway because the fact that an element that is not part of a given enum is used as key value looks suspicious and may lead to errors.

Comment From: sbrannen

This is by design.

The key type for a MultiValueMap passed as an argument to a controller's handler method is always String. Only the value type can vary.

The following are the supported types for the keys and values of a MultiValueMap passed as an argument to a handler method.

  • MultiValueMap<String, String>
  • MultiValueMap<String, MultipartFile>
  • MultiValueMap<String, javax.servlet.http.Part>

Search for "MultiValueMap" in the Web Servlet and Web Reactive sections of the reference manual for details and examples.