Search in sources :

Example 6 with ParameterProcessor

use of io.vertx.ext.web.validation.impl.parameter.ParameterProcessor in project vertx-web by vert-x3.

the class ParameterProcessorIntegrationTest method testInvalidJsonParam.

@Test
public void testInvalidJsonParam(VertxTestContext testContext) {
    ParameterProcessor processor = Parameters.jsonParam("myParam", TestSchemas.SAMPLE_OBJECT_SCHEMA_BUILDER).create(ParameterLocation.QUERY, parser);
    Map<String, List<String>> map = new HashMap<>();
    map.put("myParam", Collections.singletonList(TestSchemas.INVALID_OBJECT.encode()));
    processor.process(map).onComplete(testContext.failing(throwable -> {
        testContext.verify(() -> {
            assertThat(throwable).isInstanceOf(ParameterProcessorException.class).hasFieldOrPropertyWithValue("errorType", ParameterProcessorException.ParameterProcessorErrorType.VALIDATION_ERROR).hasFieldOrPropertyWithValue("location", ParameterLocation.QUERY).hasFieldOrPropertyWithValue("parameterName", "myParam").hasCauseInstanceOf(ValidationException.class);
        });
        testContext.completeNow();
    }));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ParameterProcessor(io.vertx.ext.web.validation.impl.parameter.ParameterProcessor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Vertx(io.vertx.core.Vertx) SchemaParser(io.vertx.json.schema.SchemaParser) HashMap(java.util.HashMap) 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) List(java.util.List) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Parameters(io.vertx.ext.web.validation.builder.Parameters) ParameterProcessorException(io.vertx.ext.web.validation.ParameterProcessorException) Map(java.util.Map) ValidationException(io.vertx.json.schema.ValidationException) Collections(java.util.Collections) SchemaRouterOptions(io.vertx.json.schema.SchemaRouterOptions) Draft7SchemaParser(io.vertx.json.schema.draft7.Draft7SchemaParser) ParameterProcessor(io.vertx.ext.web.validation.impl.parameter.ParameterProcessor) ValidationException(io.vertx.json.schema.ValidationException) HashMap(java.util.HashMap) ParameterProcessorException(io.vertx.ext.web.validation.ParameterProcessorException) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 7 with ParameterProcessor

use of io.vertx.ext.web.validation.impl.parameter.ParameterProcessor 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 8 with ParameterProcessor

use of io.vertx.ext.web.validation.impl.parameter.ParameterProcessor in project vertx-web by vert-x3.

the class ParameterProcessorUnitTest method testRequiredParam.

@Test
public void testRequiredParam() {
    ParameterProcessor processor = new ParameterProcessorImpl("myParam", ParameterLocation.QUERY, false, mockedParser, mockedValidator);
    when(mockedParser.parseParameter(any())).thenReturn(null);
    assertThatCode(() -> processor.process(new HashMap<>())).isInstanceOf(ParameterProcessorException.class).hasFieldOrPropertyWithValue("errorType", ParameterProcessorException.ParameterProcessorErrorType.MISSING_PARAMETER_WHEN_REQUIRED_ERROR).hasFieldOrPropertyWithValue("location", ParameterLocation.QUERY).hasFieldOrPropertyWithValue("parameterName", "myParam").hasNoCause();
}
Also used : ParameterProcessor(io.vertx.ext.web.validation.impl.parameter.ParameterProcessor) ParameterProcessorImpl(io.vertx.ext.web.validation.impl.parameter.ParameterProcessorImpl) Test(org.junit.jupiter.api.Test)

Example 9 with ParameterProcessor

use of io.vertx.ext.web.validation.impl.parameter.ParameterProcessor in project vertx-web by vert-x3.

the class ParameterProcessorUnitTest method testOptionalParamWithDefault.

@Test
public void testOptionalParamWithDefault(VertxTestContext testContext) {
    ParameterProcessor processor = new ParameterProcessorImpl("myParam", ParameterLocation.QUERY, true, mockedParser, mockedValidator);
    when(mockedParser.parseParameter(any())).thenReturn(null);
    when(mockedValidator.getDefault()).thenReturn(Future.succeededFuture("bla"));
    processor.process(new HashMap<>()).onComplete(testContext.succeeding(value -> {
        testContext.verify(() -> assertThat(value.getString()).isEqualTo("bla"));
        testContext.completeNow();
    }));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ValidationException(io.vertx.json.schema.ValidationException) RequestParameter(io.vertx.ext.web.validation.RequestParameter) ParameterParser(io.vertx.ext.web.validation.impl.parameter.ParameterParser) ParameterProcessorImpl(io.vertx.ext.web.validation.impl.parameter.ParameterProcessorImpl) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ParameterProcessor(io.vertx.ext.web.validation.impl.parameter.ParameterProcessor) Vertx(io.vertx.core.Vertx) SchemaParser(io.vertx.json.schema.SchemaParser) Mockito.when(org.mockito.Mockito.when) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) SchemaRouter(io.vertx.json.schema.SchemaRouter) Test(org.junit.jupiter.api.Test) ParameterProcessorException(io.vertx.ext.web.validation.ParameterProcessorException) ValueValidator(io.vertx.ext.web.validation.impl.validator.ValueValidator) 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) ParameterProcessor(io.vertx.ext.web.validation.impl.parameter.ParameterProcessor) HashMap(java.util.HashMap) ParameterProcessorImpl(io.vertx.ext.web.validation.impl.parameter.ParameterProcessorImpl) Test(org.junit.jupiter.api.Test)

Aggregations

ParameterProcessor (io.vertx.ext.web.validation.impl.parameter.ParameterProcessor)9 Test (org.junit.jupiter.api.Test)8 SchemaParser (io.vertx.json.schema.SchemaParser)7 HashMap (java.util.HashMap)7 Vertx (io.vertx.core.Vertx)6 ParameterProcessorException (io.vertx.ext.web.validation.ParameterProcessorException)6 ParameterProcessorImpl (io.vertx.ext.web.validation.impl.parameter.ParameterProcessorImpl)6 SchemaRouter (io.vertx.json.schema.SchemaRouter)6 SchemaRouterOptions (io.vertx.json.schema.SchemaRouterOptions)6 ValidationException (io.vertx.json.schema.ValidationException)6 Draft7SchemaParser (io.vertx.json.schema.draft7.Draft7SchemaParser)6 VertxExtension (io.vertx.junit5.VertxExtension)6 VertxTestContext (io.vertx.junit5.VertxTestContext)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)6 MockitoExtension (org.mockito.junit.jupiter.MockitoExtension)6 MalformedValueException (io.vertx.ext.web.validation.MalformedValueException)5 Future (io.vertx.core.Future)4 RequestParameter (io.vertx.ext.web.validation.RequestParameter)4