Document bean validation constraints in help()

Fixes #147
This commit is contained in:
Eric Bottard
2017-09-08 14:19:01 +02:00
parent 665f319d5f
commit 32c18bec75
10 changed files with 307 additions and 120 deletions

View File

@@ -23,6 +23,8 @@ import java.util.Optional;
import org.springframework.core.MethodParameter;
import javax.validation.metadata.ElementDescriptor;
/**
* Encapsulates information about a shell invokable method parameter, so that it can be documented.
*
@@ -75,6 +77,14 @@ public class ParameterDescription {
*/
private String help = "";
/**
* Allows discovery of bean validation constraints for the command parameter.
* <p>Note that most often, constraints will directly come from parameter constraints,
* but sometimes (<em>e.g.</em> in case of one method argument mapping to multiple
* command options) may come from property constraints.</p>
*/
private ElementDescriptor elementDescriptor;
public ParameterDescription(MethodParameter parameter, String type) {
this.parameter = parameter;
this.type = type;
@@ -123,6 +133,17 @@ public class ParameterDescription {
return this;
}
/**
* @return an ElementDescriptor used to discover constraints. May be {@literal null}.
*/
public ElementDescriptor elementDescriptor() {
return this.elementDescriptor;
}
public ParameterDescription elementDescriptor(ElementDescriptor descriptor) {
this.elementDescriptor = descriptor;
return this;
}
public String type() {
return type;

View File

@@ -29,6 +29,7 @@ import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.executable.ExecutableValidator;
import org.springframework.beans.factory.annotation.Autowired;
@@ -54,6 +55,9 @@ public class Shell implements CommandRegistry {
@Autowired
protected ApplicationContext applicationContext;
@Autowired(required = false)
private Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
protected Map<String, MethodTarget> methodTargets = new HashMap<>();
protected List<ParameterResolver> parameterResolvers;
@@ -63,9 +67,6 @@ public class Shell implements CommandRegistry {
*/
protected static final Object UNRESOLVED = new Object();
private final ExecutableValidator executableValidator = Validation
.buildDefaultValidatorFactory().getValidator().forExecutables();
public Shell(ResultHandler resultHandler) {
this.resultHandler = resultHandler;
}
@@ -232,7 +233,7 @@ public class Shell implements CommandRegistry {
throw new IllegalStateException("Could not resolve " + methodParameter);
}
}
Set<ConstraintViolation<Object>> constraintViolations = executableValidator.validateParameters(
Set<ConstraintViolation<Object>> constraintViolations = validator.forExecutables().validateParameters(
methodTarget.getBean(),
methodTarget.getMethod(),
args

View File

@@ -27,6 +27,9 @@ import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.shell.result.ResultHandlerConfig;
import javax.validation.Validation;
import javax.validation.Validator;
/**
* Creates supporting beans for running the Shell
*/
@@ -40,10 +43,15 @@ public class SpringShellAutoConfiguration {
return new DefaultConversionService();
}
@Bean
@ConditionalOnMissingBean(Validator.class)
public Validator validator() {
return Validation.buildDefaultValidatorFactory().getValidator();
}
@Bean
public Shell shell(@Qualifier("main") ResultHandler resultHandler) {
return new Shell(resultHandler);
}
}