All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gs.api.accelrx.web.client.validation.WebResponseValidation Maven / Gradle / Ivy

package com.gs.api.accelrx.web.client.validation;

import com.gs.api.accelrx.APIContract;
import com.gs.api.accelrx.APIOperation;
import com.gs.api.accelrx.web.client.AccelRxWebClientConfig;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SchemaValidatorsConfig;
import com.networknt.schema.SpecVersion;
import io.vertx.rxjava3.core.Vertx;
import io.vertx.rxjava3.core.WorkerExecutor;

import java.util.HashMap;
import java.util.Map;

public class WebResponseValidation {
    private final AccelRxWebClientConfig webClientConfig;
    private final APIContract apiContract;
    private final WorkerExecutor executor;

    private final Map validators = new HashMap<>();

    private WebResponseValidation(Vertx vertx, AccelRxWebClientConfig webClientConfig, APIContract apiContract) {
        this.webClientConfig = webClientConfig;
        this.apiContract = apiContract;
        this.executor = vertx.createSharedWorkerExecutor("client_validation_response_executor_" + apiContract.serviceName(), webClientConfig.validationExecutorPoolSize());
    }

    public static WebResponseValidation create(Vertx vertx, AccelRxWebClientConfig webClientConfig, APIContract apiContract) {
        return new WebResponseValidation(vertx, webClientConfig, apiContract);
    }

    public ResponseValidator validatorFor(String operationId) {
        if (!validators.containsKey(operationId)) {
            validators.put(operationId, new ResponseValidator(executor, webClientConfig, responseJsonSchema(operationId)));
        }
        return validators.get(operationId);
    }

    private Map responseJsonSchema(String operationId) {
        SchemaValidatorsConfig config = new SchemaValidatorsConfig();
        config.setTypeLoose(true);
        config.setHandleNullableField(true);

        APIOperation apiOperation = apiContract.operation(operationId);

        Map responseSchemas = new HashMap<>();
        apiOperation.statusCodes()
                .forEach(statusCode -> {
                    String responseSchemaString = apiOperation.responseBodyJsonSchema(statusCode);
                    if (responseSchemaString != null) {
                        responseSchemas.put(statusCode, JsonSchemaFactory
                                .getInstance(SpecVersion.VersionFlag.V201909)
                                .getSchema(responseSchemaString, config));
                    }
                });
        return responseSchemas;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy