Search in sources :

Example 1 with ParameterLocation

use of io.vertx.ext.web.validation.impl.ParameterLocation 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)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)1 JsonPointer (io.vertx.core.json.pointer.JsonPointer)1 RouterBuilderException (io.vertx.ext.web.openapi.RouterBuilderException)1 RequestPredicate (io.vertx.ext.web.validation.RequestPredicate)1 ParameterLocation (io.vertx.ext.web.validation.impl.ParameterLocation)1 ValidationHandlerImpl (io.vertx.ext.web.validation.impl.ValidationHandlerImpl)1 BodyProcessor (io.vertx.ext.web.validation.impl.body.BodyProcessor)1 ParameterProcessor (io.vertx.ext.web.validation.impl.parameter.ParameterProcessor)1 SchemaParser (io.vertx.json.schema.SchemaParser)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1