Search in sources :

Example 1 with RouteValidationResult

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);
}
Also used : List(java.util.List) RouteValidationResult(io.micronaut.validation.routes.RouteValidationResult) Stream(java.util.stream.Stream) MultipartBody(io.micronaut.http.server.multipart.MultipartBody) StreamedFile(io.micronaut.http.server.types.files.StreamedFile) MethodElement(io.micronaut.inject.ast.MethodElement) UriMatchTemplate(io.micronaut.http.uri.UriMatchTemplate) ClassElement(io.micronaut.inject.ast.ClassElement) Client(io.micronaut.http.client.annotation.Client) SystemFile(io.micronaut.http.server.types.files.SystemFile) ParameterElement(io.micronaut.inject.ast.ParameterElement) Stream(java.util.stream.Stream) ClassElement(io.micronaut.inject.ast.ClassElement) ParameterElement(io.micronaut.inject.ast.ParameterElement) RouteValidationResult(io.micronaut.validation.routes.RouteValidationResult)

Example 2 with RouteValidationResult

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]));
}
Also used : Bindable(io.micronaut.core.bind.annotation.Bindable) Arrays(java.util.Arrays) RouteValidationResult(io.micronaut.validation.routes.RouteValidationResult) UriMatchVariable(io.micronaut.http.uri.UriMatchVariable) Set(java.util.Set) UriMatchTemplate(io.micronaut.http.uri.UriMatchTemplate) ClassElement(io.micronaut.inject.ast.ClassElement) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) ParameterElement(io.micronaut.inject.ast.ParameterElement) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) TypedElement(io.micronaut.inject.ast.TypedElement) Map(java.util.Map) MethodElement(io.micronaut.inject.ast.MethodElement) Optional(java.util.Optional) NameUtils(io.micronaut.core.naming.NameUtils) Collections(java.util.Collections) Optional(java.util.Optional) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ClassElement(io.micronaut.inject.ast.ClassElement) UriMatchTemplate(io.micronaut.http.uri.UriMatchTemplate) UriMatchVariable(io.micronaut.http.uri.UriMatchVariable) RouteValidationResult(io.micronaut.validation.routes.RouteValidationResult) HashSet(java.util.HashSet)

Aggregations

UriMatchTemplate (io.micronaut.http.uri.UriMatchTemplate)2 ClassElement (io.micronaut.inject.ast.ClassElement)2 MethodElement (io.micronaut.inject.ast.MethodElement)2 ParameterElement (io.micronaut.inject.ast.ParameterElement)2 RouteValidationResult (io.micronaut.validation.routes.RouteValidationResult)2 List (java.util.List)2 Bindable (io.micronaut.core.bind.annotation.Bindable)1 NameUtils (io.micronaut.core.naming.NameUtils)1 Client (io.micronaut.http.client.annotation.Client)1 MultipartBody (io.micronaut.http.server.multipart.MultipartBody)1 StreamedFile (io.micronaut.http.server.types.files.StreamedFile)1 SystemFile (io.micronaut.http.server.types.files.SystemFile)1 UriMatchVariable (io.micronaut.http.uri.UriMatchVariable)1 TypedElement (io.micronaut.inject.ast.TypedElement)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1