Search in sources :

Example 31 with RequestParameter

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

the class OpenAPI3ValidationTest method testDefaultIntQueryParameter.

@Test
public void testDefaultIntQueryParameter() throws Exception {
    Operation op = testSpec.getPaths().get("/queryTests/defaultInt").getGet();
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
    loadHandlers("/queryTests/defaultInt", HttpMethod.GET, false, validationHandler, (routingContext) -> {
        RequestParameters params = routingContext.get("parsedParameters");
        RequestParameter requestParameter = params.queryParameter("parameter");
        assertTrue(requestParameter.isInteger());
        routingContext.response().setStatusMessage(requestParameter.toString()).end();
    });
    testRequest(HttpMethod.GET, "/queryTests/defaultInt", 200, "1");
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) RequestParameter(io.vertx.ext.web.api.RequestParameter) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 32 with RequestParameter

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

the class OpenAPI3ValidationTest method testFormArrayParameter.

@Test
public void testFormArrayParameter() throws Exception {
    Operation op = testSpec.getPaths().get("/formTests/arraytest").getPost();
    if (op.getParameters() == null)
        op.setParameters(new ArrayList<>());
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
    loadHandlers("/formTests/arraytest", HttpMethod.POST, false, validationHandler, (routingContext) -> {
        RequestParameters params = routingContext.get("parsedParameters");
        List<String> result = new ArrayList<>();
        for (RequestParameter r : params.formParameter("values").getArray()) result.add(r.getInteger().toString());
        routingContext.response().setStatusMessage(params.formParameter("id").getString() + serializeInCSVStringArray(result)).end();
    });
    String id = "anId";
    List<String> valuesArray = new ArrayList<>();
    for (int i = 0; i < 4; i++) valuesArray.add(getSuccessSample(ParameterType.INT).getInteger().toString());
    String values = serializeInCSVStringArray(valuesArray);
    MultiMap form = MultiMap.caseInsensitiveMultiMap();
    form.add("id", id);
    form.add("values", values);
    testRequestWithForm(HttpMethod.POST, "/formTests/arraytest", FormType.FORM_URLENCODED, form, 200, id + values);
}
Also used : MultiMap(io.vertx.core.MultiMap) 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)

Example 33 with RequestParameter

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

the class ValidationExamples method example3.

public void example3(RoutingContext routingContext) {
    RequestParameters params = routingContext.get("parsedParameters");
    RequestParameter awesomeParameter = params.queryParameter("awesomeParameter");
    if (awesomeParameter != null) {
        if (!awesomeParameter.isEmpty()) {
            // Parameter exists and isn't empty
            // ParameterTypeValidator mapped the parameter in equivalent language object
            Integer awesome = awesomeParameter.getInteger();
        } else {
        // Parameter exists, but it's empty
        }
    } else {
    // Parameter doesn't exist (it's not required)
    }
}
Also used : RequestParameter(io.vertx.ext.web.api.RequestParameter) RequestParameters(io.vertx.ext.web.api.RequestParameters)

Example 34 with RequestParameter

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

the class ExpandedObjectFieldValidator method isValid.

/**
 * Function that checks if parameter is valid. It returns a RequestParameter object that will be linked inside
 * {@link RequestParameters}. For more info, check {@link RequestParameter}.
 *
 * @param value value of parameter to test
 * @return request parameter value
 */
@Override
public RequestParameter isValid(String value) throws ValidationException {
    RequestParameter requestParameter = innerValidator.isValid(value);
    requestParameter.setName(fieldName);
    Map<String, RequestParameter> map = new HashMap<>();
    map.put(fieldName, requestParameter);
    return new RequestParameterImpl(objectName, map);
}
Also used : HashMap(java.util.HashMap) RequestParameterImpl(io.vertx.ext.web.api.impl.RequestParameterImpl) RequestParameter(io.vertx.ext.web.api.RequestParameter)

Example 35 with RequestParameter

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

the class ObjectTypeValidator method validate.

@Override
protected RequestParameter validate(Map<String, String> values) throws ValidationException {
    Map<String, RequestParameter> parsedParams = new HashMap<>();
    for (Map.Entry<String, ObjectField> field : fieldsMap.entrySet()) {
        if (!values.containsKey(field.getKey())) {
            if (field.getValue().required)
                throw ValidationException.ValidationExceptionFactory.generateObjectFieldNotFound(field.getKey());
            else if (field.getValue().validator.getDefault() != null)
                parsedParams.put(field.getKey(), RequestParameter.create(field.getKey(), field.getValue().validator.getDefault()));
        } else {
            RequestParameter param = field.getValue().validator.isValid(values.get(field.getKey()));
            param.setName(field.getKey());
            parsedParams.put(field.getKey(), param);
        }
    }
    return RequestParameter.create(parsedParams);
}
Also used : HashMap(java.util.HashMap) RequestParameter(io.vertx.ext.web.api.RequestParameter) Map(java.util.Map) HashMap(java.util.HashMap)

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