We’re using Vavr with Quarkus and although Quarkus supports java.util.Optional
types for query parameters, I would like to be able to support Vavr’s Option
type.
Reading Quarkus RestEasy Parameter Mapping a custom ParamConverterProvider
can be created to return a custom ParamConverter
.
import io.vavr.control.Option;
import jakarta.ws.rs.ext.ParamConverter;
import jakarta.ws.rs.ext.ParamConverterProvider;
import jakarta.ws.rs.ext.Provider;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
@Provider
public class VavrParamConverterProvider implements ParamConverterProvider {
@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
if (rawType.equals(Option.class)) {
return new ParamConverter<Option<T>>() {
@Override
public Option<T> fromString(String value) {
T optionValue = ??
return Option.of(optionValue);
}
@Override
public String toString(Option<T> option) {
return option.map(Object::toString).getOrElse("");
}
};
}
return null;
}
}
The issue is: how to convert the String value
to the proper type T?
Ideally I would like to use any of the other known ParamConverter
s but those cannot be injected. All known ParamConverter
s seems to be defined in the RestClientBase
class, but this class is abstract and all ParamConverter
s are in private properties. Also the static properties are private.
Injecting a RestClientBase
instance does not work.
Also injecting ParamConverterProvider
s does not work since that would be a cyclic dependency.
Any idea how to do this?
I cannot find the implementation for how this is done for java.util.Optional
for inspiration.