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 #699
- Fixes #700
This commit is contained in:
Janne Valkealahti
2023-04-05 18:56:09 +01:00
parent 64abc1da22
commit 2e19929a32
8 changed files with 358 additions and 24 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.command.annotation.Command;
@@ -56,6 +57,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
@@ -141,12 +149,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 -> {
@@ -183,11 +192,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 {
@@ -218,14 +222,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;
}
}
}