Fix ShellOption with required

- Take 2
- Fix case with optional.
- Fixes #436
This commit is contained in:
Janne Valkealahti
2022-06-02 09:55:55 +01:00
parent f7992a682e
commit 2ba288cb54
2 changed files with 24 additions and 1 deletions

View File

@@ -140,7 +140,7 @@ public class StandardMethodTargetRegistrar implements MethodTargetRegistrar, App
&& !ObjectUtils.nullSafeEquals(so.defaultValue(), ShellOption.NULL)) {
optionSpec.defaultValue(so.defaultValue());
}
else {
if (ObjectUtils.nullSafeEquals(so.defaultValue(), ShellOption.NONE)) {
optionSpec.required();
}
}

View File

@@ -119,6 +119,29 @@ public class StandardMethodTargetRegistrarTests {
}
}
@Test
public void testOptionOptionalWithAnnotation() {
applicationContext = new AnnotationConfigApplicationContext(Sample3.class);
registrar.setApplicationContext(applicationContext);
registrar.register(catalog);
Map<String, CommandRegistration> registrations = catalog.getRegistrations();
assertThat(registrations).hasSize(1);
assertThat(registrations.get("say-hello")).isNotNull();
assertThat(registrations.get("say-hello").getOptions()).hasSize(1);
assertThat(registrations.get("say-hello").getOptions().get(0).isRequired()).isFalse();
assertThat(registrations.get("say-hello").getOptions().get(0).getDefaultValue()).isNull();
}
@ShellComponent
public static class Sample3 {
@ShellMethod("some command")
public String sayHello(@ShellOption( defaultValue = ShellOption.NULL) String what) {
return "hello " + what;
}
}
@Test
public void testAvailabilityIndicators() {
applicationContext = new AnnotationConfigApplicationContext(SampleWithAvailability.class);