Method execution should not error without value

- Do npe check when getting class type out from
  incoming value.
- Fix #572
This commit is contained in:
Janne Valkealahti
2022-11-22 07:47:52 +00:00
parent ee7a9da4aa
commit 61ae11bf94
2 changed files with 23 additions and 1 deletions

View File

@@ -183,8 +183,10 @@ public interface CommandExecution {
if (parameterName == null) {
return false;
}
Class<?> sourceType = paramValues.get(parameterName) != null ? paramValues.get(parameterName).getClass()
: null;
return paramValues.containsKey(parameterName) && conversionService
.canConvert(paramValues.get(parameterName).getClass(), parameter.getParameterType());
.canConvert(sourceType, parameter.getParameterType());
}
@Override

View File

@@ -100,6 +100,26 @@ public class CommandExecutionTests extends AbstractCommandTests {
assertThat(pojo1.method1Ctx).isNotNull();
}
@Test
public void testMethodArgWithoutValue() {
CommandRegistration r1 = CommandRegistration.builder()
.command("command1")
.description("help")
.withOption()
.longNames("arg1")
.description("some arg1")
.position(0)
.arity(OptionArity.EXACTLY_ONE)
.and()
.withTarget()
.method(pojo1, "method4")
.and()
.build();
execution.evaluate(r1, new String[]{"--arg1"});
assertThat(pojo1.method4Count).isEqualTo(1);
assertThat(pojo1.method4Arg1).isNull();
}
@Test
public void testMethodSinglePositionalArgs() {
CommandRegistration r1 = CommandRegistration.builder()