Skip to content

Commit df19da0

Browse files
committed
Method execution should not error without value
- Do npe check when getting class type out from incoming value. - Backport #572 - Fix #573
1 parent bc0b4a1 commit df19da0

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

spring-shell-core/src/main/java/org/springframework/shell/command/CommandExecution.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,10 @@ public boolean supportsParameter(MethodParameter parameter) {
183183
if (parameterName == null) {
184184
return false;
185185
}
186+
Class<?> sourceType = paramValues.get(parameterName) != null ? paramValues.get(parameterName).getClass()
187+
: null;
186188
return paramValues.containsKey(parameterName) && conversionService
187-
.canConvert(paramValues.get(parameterName).getClass(), parameter.getParameterType());
189+
.canConvert(sourceType, parameter.getParameterType());
188190
}
189191

190192
@Override

spring-shell-core/src/test/java/org/springframework/shell/command/CommandExecutionTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,26 @@ public void testMethodExecution2() {
100100
assertThat(pojo1.method1Ctx).isNotNull();
101101
}
102102

103+
@Test
104+
public void testMethodArgWithoutValue() {
105+
CommandRegistration r1 = CommandRegistration.builder()
106+
.command("command1")
107+
.description("help")
108+
.withOption()
109+
.longNames("arg1")
110+
.description("some arg1")
111+
.position(0)
112+
.arity(OptionArity.EXACTLY_ONE)
113+
.and()
114+
.withTarget()
115+
.method(pojo1, "method4")
116+
.and()
117+
.build();
118+
execution.evaluate(r1, new String[]{"--arg1"});
119+
assertThat(pojo1.method4Count).isEqualTo(1);
120+
assertThat(pojo1.method4Arg1).isNull();
121+
}
122+
103123
@Test
104124
public void testMethodSinglePositionalArgs() {
105125
CommandRegistration r1 = CommandRegistration.builder()

0 commit comments

Comments
 (0)