Spring-hateoas: Custom converter is not used for enum list query parameter

Created on 27 Aug 2021  路  3Comments  路  Source: spring-projects/spring-hateoas

We have a REST endpoint that uses a query parameter being a list of enum values. The values of the corresponding enum type have labels that should be used in the url. Here is an example in Kotlin:

enum class MyEnum(val label: String) {
    VALUE_1("value1"),
    VALUE_2("value2"),
    VALUE_3("value3");
}

We added a custom converter to Spring's FormatterRegistry that converts a value of MyEnum to a string using the label.

Conversion works fine for a query parameter being a single value. Unfortunately, for collections converter is not invoked and our MyEnum values are converted to their names. We get something like that:

http://myserver.com/path/file.html?param=VALUE_1&param=VALUE_2

instead of

http://myserver.com/path/file.html?param=value1&param=value2

We think the problem lies in bindRequestParameters() method of org.springframework.hateoas.server.core.WebHandler. In case of parameter values being a collection the conversion service is not used like it is for single values.

Our current workaround for this is to overwrite the toString() method of the Enum to get the label.

We currently use HATEOAS library version 1.3.2. We checked out milestone version 1.4.0-M2 but the problem is still there.

bug core

Most helpful comment

Lovely, I'll keep it in 1.4 then. Happy coding! 馃コ

All 3 comments

Would you mind trying the 1.4 snapshots? I've refactored the way we prepare the parameter values, and they're consistently routed through the conversion service now. We could try to port this back, but the changes are based on the completely revamped encoding infrastructure introduced in 1.4 earlier. So unless urgently needed, I'd like to avoid that.

yes, 1.4.0-SNAPSHOT works!
It's not that urgently needed - it is ok for us to use the toString workaround until then.

Lovely, I'll keep it in 1.4 then. Happy coding! 馃コ

Was this page helpful?
0 / 5 - 0 ratings