Fix option type parsing

- In `CommandRegistration` add `ResolvableType` for `OptionSpec` giving
  more spesific handling of a type.
- In `CommandParser` handle source and target types so that we
  have generics with `List`, `Set` and arrays working better.
- In `HandlerMethodArgumentResolver` add better handling for
  `ConversionService` for generic types.
- In `StandardMethodTargetRegistrar` add better types via `ResolvableType`
  now that `CommandRegistration` support it.
- In `OptionConversionCommands` remove converter from `String` to `Set` as
  now things should work as is if generic in a `Set` has a converter.
- Backport #694
- Fixes #699
This commit is contained in:
Janne Valkealahti
2023-04-02 14:43:18 +01:00
parent 8ce7a11aa0
commit cd9651eea8
9 changed files with 388 additions and 28 deletions

View File

@@ -16,11 +16,12 @@
package org.springframework.shell.samples.e2e;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.ResolvableType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.shell.command.CommandRegistration;
import org.springframework.shell.standard.ShellComponent;
@@ -54,6 +55,13 @@ public class OptionConversionCommands {
return "Hello " + arg1;
}
@ShellMethod(key = LEGACY_ANNO + "option-conversion-customlist", group = GROUP)
public String optionConversionCustomListAnnotation(
@ShellOption List<MyPojo> arg1
) {
return "Hello " + arg1;
}
@ShellMethod(key = LEGACY_ANNO + "option-conversion-customarray", group = GROUP)
public String optionConversionCustomArrayAnnotation(
@ShellOption MyPojo[] arg1
@@ -103,12 +111,13 @@ public class OptionConversionCommands {
@Bean
public CommandRegistration optionConversionCustomSetRegistration() {
return getBuilder()
ResolvableType rtype = ResolvableType.forClassWithGenerics(Set.class, MyPojo.class);
return CommandRegistration.builder()
.command(REG, "option-conversion-customset")
.group(GROUP)
.withOption()
.longNames("arg1")
.type(Set.class)
.type(rtype)
.and()
.withTarget()
.function(ctx -> {
@@ -145,11 +154,6 @@ public class OptionConversionCommands {
public Converter<String, MyPojo> stringToMyPojoConverter() {
return new StringToMyPojoConverter();
}
@Bean
public Converter<String, Set<MyPojo>> stringToMyPojoSetConverter() {
return new StringToMyPojoSetConverter();
}
}
public static class MyPojo {
@@ -180,14 +184,4 @@ public class OptionConversionCommands {
return new MyPojo(from);
}
}
static class StringToMyPojoSetConverter implements Converter<String, Set<MyPojo>> {
@Override
public Set<MyPojo> convert(String from) {
Set<MyPojo> set = new HashSet<>();
set.add(new MyPojo(from));
return set;
}
}
}