use of io.micronaut.validation.routes.RouteValidationResult in project micronaut-core by micronaut-projects.
the class ClientTypesRule method validate.
@Override
public RouteValidationResult validate(List<UriMatchTemplate> templates, ParameterElement[] parameters, MethodElement method) {
String[] errors = new String[] {};
if (method.hasAnnotation(Client.class)) {
final Stream.Builder<ClassElement> builder = Stream.<ClassElement>builder().add(method.getReturnType());
for (ParameterElement param : method.getParameters()) {
builder.add(param.getType());
}
errors = builder.build().filter(type -> {
for (Class<?> clazz : SERVER_TYPES) {
if (type.isAssignable(clazz)) {
return true;
}
}
return false;
}).map(type -> "The type [" + type + "] must not be used in declarative client methods. The type is specific to server based usages.").toArray(String[]::new);
}
return new RouteValidationResult(errors);
}
use of io.micronaut.validation.routes.RouteValidationResult in project micronaut-core by micronaut-projects.
the class NullableParameterRule method validate.
@Override
public RouteValidationResult validate(List<UriMatchTemplate> templates, ParameterElement[] parameters, MethodElement method) {
List<String> errorMessages = new ArrayList<>();
boolean isClient = method.hasAnnotation("io.micronaut.http.client.annotation.Client");
// Optional variables can be required in clients
if (!isClient) {
Map<String, UriMatchVariable> variables = new HashMap<>();
Set<UriMatchVariable> required = new HashSet<>();
for (UriMatchTemplate template : templates) {
for (UriMatchVariable variable : template.getVariables()) {
if (!variable.isOptional() || variable.isExploded()) {
required.add(variable);
}
variables.compute(variable.getName(), (key, var) -> {
if (var == null) {
if (variable.isOptional() && !variable.isExploded()) {
return variable;
} else {
return null;
}
} else {
if (!var.isOptional() || var.isExploded()) {
if (variable.isOptional() && !variable.isExploded()) {
return variable;
} else {
return var;
}
} else {
return var;
}
}
});
}
}
for (UriMatchVariable variable : required) {
if (templates.stream().anyMatch(t -> !t.getVariableNames().contains(variable.getName()))) {
variables.putIfAbsent(variable.getName(), variable);
}
}
for (UriMatchVariable variable : variables.values()) {
Arrays.stream(parameters).flatMap(p -> getTypedElements(p).stream()).filter(p -> p.getName().equals(variable.getName())).forEach(p -> {
ClassElement type = p.getType();
boolean hasDefaultValue = p.findAnnotation(Bindable.class).flatMap(av -> av.stringValue("defaultValue")).isPresent();
if (!isNullable(p) && type != null && !type.isAssignable(Optional.class) && !hasDefaultValue) {
errorMessages.add(String.format("The uri variable [%s] is optional, but the corresponding method argument [%s %s] is not defined as an Optional or annotated with a Nullable annotation.", variable.getName(), p.getType().toString(), p.getName()));
}
});
}
}
return new RouteValidationResult(errorMessages.toArray(new String[0]));
}
Aggregations