Search in sources :

Example 1 with OpenAPI3RequestValidationHandlerImpl

use of io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl in project vertx-web by vert-x3.

the class OpenAPI3ValidationTest method testJsonBody.

@Test
public void testJsonBody() throws Exception {
    Operation op = testSpec.getPaths().get("/jsonBodyTest/sampleTest").getPost();
    if (op.getParameters() == null)
        op.setParameters(new ArrayList<>());
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
    loadHandlers("/jsonBodyTest/sampleTest", HttpMethod.POST, false, validationHandler, (routingContext) -> {
        RequestParameters params = routingContext.get("parsedParameters");
        routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("Content-Type", "application/json").end(params.body().getJsonObject().encode());
    });
    JsonObject object = new JsonObject();
    object.put("id", "anId");
    List<Integer> valuesArray = new ArrayList<>();
    for (int i = 0; i < 4; i++) valuesArray.add(getSuccessSample(ParameterType.INT).getInteger());
    object.put("values", valuesArray);
    testRequestWithJSON(HttpMethod.POST, "/jsonBodyTest/sampleTest", object, 200, "OK", object);
    testRequestWithJSONAndCustomContentType(HttpMethod.POST, "/jsonBodyTest/sampleTest", "application/json; charset=utf-8", object, 200, "OK", object);
    testRequestWithJSONAndCustomContentType(HttpMethod.POST, "/jsonBodyTest/sampleTest", "application/superapplication+json", object, 200, "OK", object);
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 2 with OpenAPI3RequestValidationHandlerImpl

use of io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl in project vertx-web by vert-x3.

the class OpenAPI3ValidationTest method testQueryParameterArrayExploded.

@Test
public void testQueryParameterArrayExploded() throws Exception {
    Operation op = testSpec.getPaths().get("/queryTests/arrayTests/formExploded").getGet();
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
    loadHandlers("/queryTests/arrayTests/formExploded", 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");
    StringBuilder stringBuilder = new StringBuilder();
    for (String s : values) {
        stringBuilder.append("parameter=" + s + "&");
    }
    stringBuilder.deleteCharAt(stringBuilder.length() - 1);
    testRequest(HttpMethod.GET, "/queryTests/arrayTests/formExploded?" + stringBuilder, 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)

Example 3 with OpenAPI3RequestValidationHandlerImpl

use of io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl in project vertx-web by vert-x3.

the class OpenAPI3ValidationTest method testAllowEmptyValueQueryParameter.

@Test
public void testAllowEmptyValueQueryParameter() throws Exception {
    Operation op = testSpec.getPaths().get("/queryTests/defaultString").getGet();
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
    loadHandlers("/queryTests/defaultString", HttpMethod.GET, false, validationHandler, (routingContext) -> {
        RequestParameters params = routingContext.get("parsedParameters");
        routingContext.response().setStatusMessage(params.queryParameter("parameter").getString()).end();
    });
    // Empty value should not be overwritten
    testRequest(HttpMethod.GET, "/queryTests/defaultString?parameter=", 200, "");
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 4 with OpenAPI3RequestValidationHandlerImpl

use of io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl in project vertx-web by vert-x3.

the class OpenAPI3ValidationTest method testAllOfQueryParam.

@Test
public void testAllOfQueryParam() throws Exception {
    Operation op = testSpec.getPaths().get("/queryTests/allOfTest").getGet();
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
    loadHandlers("/queryTests/allOfTest", HttpMethod.GET, false, validationHandler, (routingContext) -> {
        RequestParameters params = routingContext.get("parsedParameters");
        routingContext.response().setStatusMessage(params.queryParameter("parameter").getObjectValue("a").getInteger().toString() + params.queryParameter("parameter").getObjectValue("b").getBoolean().toString()).end();
    });
    String a = "5";
    String b = "false";
    String parameter = "parameter=a," + a + ",b," + b;
    testRequest(HttpMethod.GET, "/queryTests/allOfTest?" + parameter, 200, a + b);
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 5 with OpenAPI3RequestValidationHandlerImpl

use of io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl in project vertx-web by vert-x3.

the class OpenAPI3ValidationTest method testAdditionalPropertiesJson.

@Test
public void testAdditionalPropertiesJson() throws Exception {
    Operation op = testSpec.getPaths().get("/additionalProperties").getPost();
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
    loadHandlers("/additionalProperties", HttpMethod.POST, true, validationHandler, (routingContext) -> {
        RequestParameters params = routingContext.get("parsedParameters");
        routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("Content-Type", "application/json").end(params.body().getJsonObject().encode());
    });
    JsonObject pet = new JsonObject();
    pet.put("id", 14612);
    pet.put("name", "Willy");
    pet.put("lazyness", "Highest");
    testRequestWithJSON(HttpMethod.POST, "/additionalProperties", pet, 400, errorMessage(ValidationException.ErrorType.JSON_INVALID));
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) JsonObject(io.vertx.core.json.JsonObject) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Aggregations

Operation (io.swagger.v3.oas.models.Operation)28 OpenAPI3RequestValidationHandlerImpl (io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl)28 Test (org.junit.Test)28 RequestParameters (io.vertx.ext.web.api.RequestParameters)21 ArrayList (java.util.ArrayList)10 JsonObject (io.vertx.core.json.JsonObject)7 RequestParameter (io.vertx.ext.web.api.RequestParameter)7 MultiMap (io.vertx.core.MultiMap)5 Ignore (org.junit.Ignore)2 OpenAPI (io.swagger.v3.oas.models.OpenAPI)1 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)1 HttpMethod (io.vertx.core.http.HttpMethod)1 OpenApi3Utils (io.vertx.ext.web.api.contract.openapi3.impl.OpenApi3Utils)1 ParameterType (io.vertx.ext.web.api.validation.ParameterType)1 ValidationException (io.vertx.ext.web.api.validation.ValidationException)1 WebTestValidationBase (io.vertx.ext.web.api.validation.WebTestValidationBase)1 List (java.util.List)1 Rule (org.junit.Rule)1 ExternalResource (org.junit.rules.ExternalResource)1