Search in sources :

Example 61 with RequestParameter

use of io.vertx.ext.web.api.RequestParameter in project vertx-web by vert-x3.

the class BaseValidationHandler method validateHeaderParams.

private Map<String, RequestParameter> validateHeaderParams(RoutingContext routingContext) throws ValidationException {
    // Validation process validate only params that are registered in the validation -> extra params are allowed
    Map<String, RequestParameter> parsedParams = new HashMap<>();
    MultiMap headersParams = routingContext.request().headers();
    for (ParameterValidationRule rule : headerParamsRules.values()) {
        String name = rule.getName();
        if (headersParams.contains(name)) {
            List<String> p = headersParams.getAll(name);
            if (p.size() != 0) {
                RequestParameter parsedParam = rule.validateArrayParam(p);
                if (parsedParams.containsKey(parsedParam.getName()))
                    parsedParam = parsedParam.merge(parsedParams.get(parsedParam.getName()));
                parsedParams.put(parsedParam.getName(), parsedParam);
            } else {
                throw ValidationException.ValidationExceptionFactory.generateNotMatchValidationException(name + " can't be empty");
            }
        } else if (rule.parameterTypeValidator().getDefault() != null) {
            RequestParameter parsedParam = new RequestParameterImpl(name, rule.parameterTypeValidator().getDefault());
            if (parsedParams.containsKey(parsedParam.getName()))
                parsedParam = parsedParam.merge(parsedParams.get(parsedParam.getName()));
            parsedParams.put(parsedParam.getName(), parsedParam);
        } else if (!rule.isOptional())
            throw ValidationException.ValidationExceptionFactory.generateNotFoundValidationException(name, ParameterLocation.HEADER);
    }
    return parsedParams;
}
Also used : MultiMap(io.vertx.core.MultiMap) RequestParameterImpl(io.vertx.ext.web.api.impl.RequestParameterImpl) RequestParameter(io.vertx.ext.web.api.RequestParameter)

Example 62 with RequestParameter

use of io.vertx.ext.web.api.RequestParameter in project vertx-web by vert-x3.

the class BaseValidationHandler method validateFormParams.

private Map<String, RequestParameter> validateFormParams(RoutingContext routingContext) throws ValidationException {
    // Validation process validate only params that are registered in the validation -> extra params are allowed
    Map<String, RequestParameter> parsedParams = new HashMap<>();
    MultiMap formParams = routingContext.request().formAttributes();
    for (ParameterValidationRule rule : formParamsRules.values()) {
        String name = rule.getName();
        if (formParams.contains(name)) {
            List<String> p = formParams.getAll(name);
            if (p.size() != 0) {
                RequestParameter parsedParam = rule.validateArrayParam(p);
                if (parsedParams.containsKey(parsedParam.getName()))
                    parsedParam = parsedParam.merge(parsedParams.get(parsedParam.getName()));
                parsedParams.put(parsedParam.getName(), parsedParam);
            } else {
                throw ValidationException.ValidationExceptionFactory.generateNotMatchValidationException(name + " can't be empty");
            }
        } else if (rule.parameterTypeValidator().getDefault() != null) {
            RequestParameter parsedParam = new RequestParameterImpl(name, rule.parameterTypeValidator().getDefault());
            if (parsedParams.containsKey(parsedParam.getName()))
                parsedParam = parsedParam.merge(parsedParams.get(parsedParam.getName()));
            parsedParams.put(parsedParam.getName(), parsedParam);
        } else if (!rule.isOptional())
            throw ValidationException.ValidationExceptionFactory.generateNotFoundValidationException(name, ParameterLocation.BODY_FORM);
    }
    return parsedParams;
}
Also used : MultiMap(io.vertx.core.MultiMap) RequestParameterImpl(io.vertx.ext.web.api.impl.RequestParameterImpl) RequestParameter(io.vertx.ext.web.api.RequestParameter)

Example 63 with RequestParameter

use of io.vertx.ext.web.api.RequestParameter in project vertx-web by vert-x3.

the class BaseValidationHandler method validateQueryParams.

private Map<String, RequestParameter> validateQueryParams(RoutingContext routingContext) throws ValidationException {
    // Validation process validate only params that are registered in the validation -> extra params are allowed
    Map<String, RequestParameter> parsedParams = new HashMap<>();
    MultiMap queryParams = routingContext.queryParams();
    for (ParameterValidationRule rule : queryParamsRules.values()) {
        String name = rule.getName();
        if (queryParams.contains(name)) {
            List<String> p = queryParams.getAll(name);
            if (p.size() != 0) {
                RequestParameter parsedParam = rule.validateArrayParam(p);
                if (parsedParams.containsKey(parsedParam.getName()))
                    parsedParam = parsedParam.merge(parsedParams.get(parsedParam.getName()));
                parsedParams.put(parsedParam.getName(), parsedParam);
            } else {
                throw ValidationException.ValidationExceptionFactory.generateNotMatchValidationException(name + " can't be empty");
            }
        } else if (rule.parameterTypeValidator().getDefault() != null) {
            RequestParameter parsedParam = new RequestParameterImpl(name, rule.parameterTypeValidator().getDefault());
            if (parsedParams.containsKey(parsedParam.getName()))
                parsedParam = parsedParam.merge(parsedParams.get(parsedParam.getName()));
            parsedParams.put(parsedParam.getName(), parsedParam);
        } else if (!rule.isOptional())
            throw ValidationException.ValidationExceptionFactory.generateNotFoundValidationException(name, ParameterLocation.QUERY);
    }
    return parsedParams;
}
Also used : MultiMap(io.vertx.core.MultiMap) RequestParameterImpl(io.vertx.ext.web.api.impl.RequestParameterImpl) RequestParameter(io.vertx.ext.web.api.RequestParameter)

Example 64 with RequestParameter

use of io.vertx.ext.web.api.RequestParameter in project vertx-web by vert-x3.

the class BaseValidationHandler method validatePathParams.

private Map<String, RequestParameter> validatePathParams(RoutingContext routingContext) throws ValidationException {
    // Validation process validate only params that are registered in the validation -> extra params are allowed
    Map<String, RequestParameter> parsedParams = new HashMap<>();
    Map<String, String> pathParams = routingContext.pathParams();
    for (ParameterValidationRule rule : pathParamsRules.values()) {
        String name = rule.getName();
        if (pathParams.containsKey(name)) {
            RequestParameter parsedParam = rule.validateSingleParam(pathParams.get(name));
            if (parsedParams.containsKey(parsedParam.getName()))
                parsedParam = parsedParam.merge(parsedParams.get(parsedParam.getName()));
            parsedParams.put(parsedParam.getName(), parsedParam);
        } else
            // Path params are required!
            throw ValidationException.ValidationExceptionFactory.generateNotFoundValidationException(name, ParameterLocation.PATH);
    }
    return parsedParams;
}
Also used : RequestParameter(io.vertx.ext.web.api.RequestParameter)

Example 65 with RequestParameter

use of io.vertx.ext.web.api.RequestParameter in project vertx-web by vert-x3.

the class OpenAPI3ValidationTest method testQueryParameterArrayDefaultStyle.

@Test
public void testQueryParameterArrayDefaultStyle() throws Exception {
    Operation op = testSpec.getPaths().get("/queryTests/arrayTests/default").getGet();
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
    loadHandlers("/queryTests/arrayTests/default", HttpMethod.GET, false, validationHandler, (routingContext) -> {
        RequestParameters params = routingContext.get("parsedParameters");
        List<String> result = new ArrayList<>();
        for (RequestParameter r : params.queryParameter("parameter").getArray()) result.add(r.getInteger().toString());
        routingContext.response().setStatusMessage(serializeInCSVStringArray(result)).end();
    });
    List<String> values = new ArrayList<>();
    values.add("4");
    values.add("2");
    values.add("26");
    testRequest(HttpMethod.GET, "/queryTests/arrayTests/default?parameter=" + serializeInCSVStringArray(values), 200, serializeInCSVStringArray(values));
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) RequestParameter(io.vertx.ext.web.api.RequestParameter) ArrayList(java.util.ArrayList) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Aggregations

RequestParameter (io.vertx.ext.web.api.RequestParameter)65 RequestParameters (io.vertx.ext.web.api.RequestParameters)57 Test (org.junit.Test)54 JsonObject (io.vertx.core.json.JsonObject)49 AsyncResult (io.vertx.core.AsyncResult)48 CountDownLatch (java.util.concurrent.CountDownLatch)48 HashMap (java.util.HashMap)34 ArrayList (java.util.ArrayList)21 HttpServerOptions (io.vertx.core.http.HttpServerOptions)18 RouterFactoryOptions (io.vertx.ext.web.api.contract.RouterFactoryOptions)18 Map (java.util.Map)18 OpenAPI (io.swagger.v3.oas.models.OpenAPI)17 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)17 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)17 Handler (io.vertx.core.Handler)17 JsonArray (io.vertx.core.json.JsonArray)17 RoutingContext (io.vertx.ext.web.RoutingContext)17 OpenAPI3RouterFactoryImpl (io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RouterFactoryImpl)17 WebTestValidationBase (io.vertx.ext.web.api.validation.WebTestValidationBase)17 HttpResponse (io.vertx.ext.web.client.HttpResponse)17