Search in sources :

Example 6 with BodyProcessor

use of io.vertx.ext.web.validation.impl.body.BodyProcessor in project vertx-web by vert-x3.

the class JsonBodyProcessorImplTest method testMalformedJson.

@Test
public void testMalformedJson() {
    when(mockerServerRequest.getHeader(HttpHeaders.CONTENT_TYPE)).thenReturn("application/json");
    when(mockedContext.request()).thenReturn(mockerServerRequest);
    when(mockedContext.getBody()).thenReturn(Buffer.buffer("{\"a"));
    BodyProcessor processor = Bodies.json(TestSchemas.SAMPLE_ARRAY_SCHEMA_BUILDER).create(parser);
    assertThatCode(() -> processor.process(mockedContext)).isInstanceOf(BodyProcessorException.class).hasFieldOrPropertyWithValue("actualContentType", "application/json").hasCauseInstanceOf(DecodeException.class);
}
Also used : BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) Test(org.junit.jupiter.api.Test)

Example 7 with BodyProcessor

use of io.vertx.ext.web.validation.impl.body.BodyProcessor in project vertx-web by vert-x3.

the class JsonBodyProcessorImplTest method testNullBody.

@Test
public void testNullBody() {
    when(mockerServerRequest.getHeader(HttpHeaders.CONTENT_TYPE)).thenReturn("application/json");
    when(mockedContext.request()).thenReturn(mockerServerRequest);
    when(mockedContext.getBody()).thenReturn(null);
    BodyProcessor processor = Bodies.json(schema().withKeyword("type", "null")).create(parser);
    assertThatCode(() -> processor.process(mockedContext)).isInstanceOf(BodyProcessorException.class).hasFieldOrPropertyWithValue("actualContentType", "application/json").hasCauseInstanceOf(MalformedValueException.class);
}
Also used : BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) Test(org.junit.jupiter.api.Test)

Example 8 with BodyProcessor

use of io.vertx.ext.web.validation.impl.body.BodyProcessor in project vertx-web by vert-x3.

the class OpenAPI3ValidationHandlerGenerator method create.

public ValidationHandlerImpl create(OperationImpl operation) {
    // TODO error handling of this function?
    Map<ParameterLocation, List<ParameterProcessor>> parameterProcessors = new HashMap<>();
    List<BodyProcessor> bodyProcessors = new ArrayList<>();
    GeneratorContext context = new GeneratorContext(this.schemaParser, holder, operation);
    // Parse parameter processors
    for (Map.Entry<JsonPointer, JsonObject> pe : operation.getParameters().entrySet()) {
        ParameterLocation parsedLocation = ParameterLocation.valueOf(pe.getValue().getString("in").toUpperCase());
        String parsedStyle = OpenAPI3Utils.resolveStyle(pe.getValue());
        if (pe.getValue().getBoolean("allowReserved", false))
            throw RouterBuilderException.createUnsupportedSpecFeature("You are using allowReserved keyword in parameter " + pe.getKey() + " which " + "is not supported");
        JsonObject fakeSchema = context.fakeSchema(pe.getValue().getJsonObject("schema", new JsonObject()));
        ParameterProcessorGenerator generator = parameterProcessorGenerators.stream().filter(g -> g.canGenerate(pe.getValue(), fakeSchema, parsedLocation, parsedStyle)).findFirst().orElseThrow(() -> RouterBuilderException.cannotFindParameterProcessorGenerator(pe.getKey(), pe.getValue()));
        try {
            ParameterProcessor generated = generator.generate(pe.getValue(), fakeSchema, pe.getKey(), parsedLocation, parsedStyle, context);
            if (!parameterProcessors.containsKey(generated.getLocation()))
                parameterProcessors.put(generated.getLocation(), new ArrayList<>());
            parameterProcessors.get(generated.getLocation()).add(generated);
        } catch (Exception e) {
            throw RouterBuilderException.createErrorWhileGeneratingValidationHandler(String.format("Cannot generate parameter validator for parameter %s in %s", pe.getKey().toURI(), parsedLocation), e);
        }
    }
    // Parse body required predicate
    if (parseIsBodyRequired(operation))
        context.addPredicate(RequestPredicate.BODY_REQUIRED);
    // Parse body processors
    for (Map.Entry<String, Object> mediaType : (JsonObject) REQUEST_BODY_CONTENT_POINTER.queryOrDefault(operation.getOperationModel(), iteratorWithLoader, new JsonObject())) {
        JsonObject mediaTypeModel = (JsonObject) mediaType.getValue();
        JsonPointer mediaTypePointer = operation.getPointer().copy().append("requestBody").append("content").append(mediaType.getKey());
        BodyProcessor generated = bodyProcessorGenerators.stream().filter(g -> g.canGenerate(mediaType.getKey(), mediaTypeModel)).findFirst().orElse(NoopBodyProcessorGenerator.INSTANCE).generate(mediaType.getKey(), mediaTypeModel, mediaTypePointer, context);
        bodyProcessors.add(generated);
    }
    return new ValidationHandlerImpl(parameterProcessors, bodyProcessors, context.getPredicates());
}
Also used : ParameterProcessor(io.vertx.ext.web.validation.impl.parameter.ParameterProcessor) BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) SchemaParser(io.vertx.json.schema.SchemaParser) HashMap(java.util.HashMap) RequestPredicate(io.vertx.ext.web.validation.RequestPredicate) ArrayList(java.util.ArrayList) ValidationHandlerImpl(io.vertx.ext.web.validation.impl.ValidationHandlerImpl) RouterBuilderException(io.vertx.ext.web.openapi.RouterBuilderException) List(java.util.List) ParameterLocation(io.vertx.ext.web.validation.impl.ParameterLocation) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) JsonPointer(io.vertx.core.json.pointer.JsonPointer) ParameterProcessor(io.vertx.ext.web.validation.impl.parameter.ParameterProcessor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ParameterLocation(io.vertx.ext.web.validation.impl.ParameterLocation) JsonObject(io.vertx.core.json.JsonObject) ValidationHandlerImpl(io.vertx.ext.web.validation.impl.ValidationHandlerImpl) JsonPointer(io.vertx.core.json.pointer.JsonPointer) RouterBuilderException(io.vertx.ext.web.openapi.RouterBuilderException) ArrayList(java.util.ArrayList) List(java.util.List) JsonObject(io.vertx.core.json.JsonObject) BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with BodyProcessor

use of io.vertx.ext.web.validation.impl.body.BodyProcessor in project vertx-web by vert-x3.

the class FormBodyProcessorImplTest method testFormBodyProcessor.

@Test
public void testFormBodyProcessor(VertxTestContext testContext) {
    ObjectSchemaBuilder schemaBuilder = TestSchemas.SAMPLE_OBJECT_SCHEMA_BUILDER;
    MultiMap map = MultiMap.caseInsensitiveMultiMap();
    map.add("someNumbers", "1.1");
    map.add("someNumbers", "2.2");
    map.add("oneNumber", "3.3");
    map.add("someIntegers", "1");
    map.add("someIntegers", "2");
    map.add("oneInteger", "3");
    map.add("aBoolean", "true");
    when(mockedServerRequest.formAttributes()).thenReturn(map);
    when(mockedContext.request()).thenReturn(mockedServerRequest);
    BodyProcessor processor = Bodies.formUrlEncoded(schemaBuilder).create(parser);
    assertThat(processor.canProcess("application/x-www-form-urlencoded")).isTrue();
    processor.process(mockedContext).onComplete(testContext.succeeding(rp -> {
        testContext.verify(() -> {
            assertThat(rp.isJsonObject()).isTrue();
            assertThat(rp.getJsonObject()).isEqualTo(new JsonObject().put("someNumbers", new JsonArray().add(1.1d).add(2.2d)).put("oneNumber", 3.3d).put("someIntegers", new JsonArray().add(1L).add(2L)).put("oneInteger", 3L).put("aBoolean", true));
        });
        testContext.completeNow();
    }));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) HttpServerRequest(io.vertx.core.http.HttpServerRequest) Mock(org.mockito.Mock) BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MultiMap(io.vertx.core.MultiMap) RoutingContext(io.vertx.ext.web.RoutingContext) BodyProcessorException(io.vertx.ext.web.validation.BodyProcessorException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) JsonObject(io.vertx.core.json.JsonObject) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Vertx(io.vertx.core.Vertx) HttpHeaders(io.vertx.core.http.HttpHeaders) SchemaParser(io.vertx.json.schema.SchemaParser) Mockito.when(org.mockito.Mockito.when) Bodies(io.vertx.ext.web.validation.builder.Bodies) VertxExtension(io.vertx.junit5.VertxExtension) SchemaRouter(io.vertx.json.schema.SchemaRouter) TestSchemas(io.vertx.ext.web.validation.testutils.TestSchemas) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) ObjectSchemaBuilder(io.vertx.json.schema.common.dsl.ObjectSchemaBuilder) MalformedValueException(io.vertx.ext.web.validation.MalformedValueException) SchemaRouterOptions(io.vertx.json.schema.SchemaRouterOptions) Draft7SchemaParser(io.vertx.json.schema.draft7.Draft7SchemaParser) JsonArray(io.vertx.core.json.JsonArray) ObjectSchemaBuilder(io.vertx.json.schema.common.dsl.ObjectSchemaBuilder) MultiMap(io.vertx.core.MultiMap) JsonObject(io.vertx.core.json.JsonObject) BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) Test(org.junit.jupiter.api.Test)

Example 10 with BodyProcessor

use of io.vertx.ext.web.validation.impl.body.BodyProcessor in project vertx-web by vert-x3.

the class JsonBodyProcessorImplTest method testInvalidJsonObject.

@Test
public void testInvalidJsonObject(VertxTestContext testContext) {
    when(mockerServerRequest.getHeader(HttpHeaders.CONTENT_TYPE)).thenReturn("application/json");
    when(mockedContext.request()).thenReturn(mockerServerRequest);
    when(mockedContext.getBody()).thenReturn(TestSchemas.INVALID_OBJECT.toBuffer());
    BodyProcessor processor = Bodies.json(TestSchemas.SAMPLE_OBJECT_SCHEMA_BUILDER).create(parser);
    processor.process(mockedContext).onComplete(testContext.failing(err -> {
        testContext.verify(() -> {
            assertThat(err).isInstanceOf(BodyProcessorException.class).hasFieldOrPropertyWithValue("actualContentType", "application/json").hasCauseInstanceOf(ValidationException.class);
        });
        testContext.completeNow();
    }));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) HttpServerRequest(io.vertx.core.http.HttpServerRequest) DecodeException(io.vertx.core.json.DecodeException) Mock(org.mockito.Mock) BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RoutingContext(io.vertx.ext.web.RoutingContext) BodyProcessorException(io.vertx.ext.web.validation.BodyProcessorException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ValidationException(io.vertx.json.schema.ValidationException) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Schemas.schema(io.vertx.json.schema.draft7.dsl.Schemas.schema) Vertx(io.vertx.core.Vertx) HttpHeaders(io.vertx.core.http.HttpHeaders) SchemaParser(io.vertx.json.schema.SchemaParser) Mockito.when(org.mockito.Mockito.when) Bodies(io.vertx.ext.web.validation.builder.Bodies) VertxExtension(io.vertx.junit5.VertxExtension) SchemaRouter(io.vertx.json.schema.SchemaRouter) TestSchemas(io.vertx.ext.web.validation.testutils.TestSchemas) Test(org.junit.jupiter.api.Test) Buffer(io.vertx.core.buffer.Buffer) MalformedValueException(io.vertx.ext.web.validation.MalformedValueException) SchemaRouterOptions(io.vertx.json.schema.SchemaRouterOptions) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) Draft7SchemaParser(io.vertx.json.schema.draft7.Draft7SchemaParser) ValidationException(io.vertx.json.schema.ValidationException) BodyProcessorException(io.vertx.ext.web.validation.BodyProcessorException) BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) Test(org.junit.jupiter.api.Test)

Aggregations

BodyProcessor (io.vertx.ext.web.validation.impl.body.BodyProcessor)13 Test (org.junit.jupiter.api.Test)12 SchemaParser (io.vertx.json.schema.SchemaParser)9 Vertx (io.vertx.core.Vertx)8 HttpHeaders (io.vertx.core.http.HttpHeaders)8 HttpServerRequest (io.vertx.core.http.HttpServerRequest)8 RoutingContext (io.vertx.ext.web.RoutingContext)8 BodyProcessorException (io.vertx.ext.web.validation.BodyProcessorException)8 MalformedValueException (io.vertx.ext.web.validation.MalformedValueException)8 Bodies (io.vertx.ext.web.validation.builder.Bodies)8 TestSchemas (io.vertx.ext.web.validation.testutils.TestSchemas)8 SchemaRouter (io.vertx.json.schema.SchemaRouter)8 SchemaRouterOptions (io.vertx.json.schema.SchemaRouterOptions)8 Draft7SchemaParser (io.vertx.json.schema.draft7.Draft7SchemaParser)8 VertxExtension (io.vertx.junit5.VertxExtension)8 VertxTestContext (io.vertx.junit5.VertxTestContext)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)8 Mock (org.mockito.Mock)8