CommandRegistration should return same options
- Cache created CommandOption(s) to return same instance. - Backport #648 - Fixes #649
This commit is contained in:
@@ -812,6 +812,7 @@ public interface CommandRegistration {
|
||||
private String description;
|
||||
private Supplier<Availability> availability;
|
||||
private List<DefaultOptionSpec> optionSpecs;
|
||||
private List<CommandOption> options;
|
||||
private DefaultTargetSpec targetSpec;
|
||||
private List<DefaultAliasSpec> aliasSpecs;
|
||||
private DefaultExitCodeSpec exitCodeSpec;
|
||||
@@ -857,11 +858,15 @@ public interface CommandRegistration {
|
||||
|
||||
@Override
|
||||
public List<CommandOption> getOptions() {
|
||||
return optionSpecs.stream()
|
||||
if (options != null) {
|
||||
return options;
|
||||
}
|
||||
options = optionSpecs.stream()
|
||||
.map(o -> CommandOption.of(o.getLongNames(), o.getShortNames(), o.getDescription(), o.getType(),
|
||||
o.isRequired(), o.getDefaultValue(), o.getPosition(), o.getArityMin(), o.getArityMax(),
|
||||
o.getLabel(), o.getCompletion()))
|
||||
.collect(Collectors.toList());
|
||||
return options;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.shell.command;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -455,4 +456,20 @@ public class CommandRegistrationTests extends AbstractCommandTests {
|
||||
assertThat(registration.getOptions()).hasSize(1);
|
||||
assertThat(registration.getOptions().get(0).getCompletion()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void optionShouldBeSameInstance() {
|
||||
CommandRegistration registration = CommandRegistration.builder()
|
||||
.command("command1")
|
||||
.withOption()
|
||||
.longNames("arg1")
|
||||
.and()
|
||||
.withTarget()
|
||||
.consumer(ctx -> {})
|
||||
.and()
|
||||
.build();
|
||||
List<CommandOption> options1 = registration.getOptions();
|
||||
List<CommandOption> options2 = registration.getOptions();
|
||||
assertThat(options1).isEqualTo(options2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user