Search in sources :

Example 1 with AaiDocument

use of io.apicurio.datamodels.asyncapi.models.AaiDocument in project carbon-apimgt by wso2.

the class AsyncApiParser method validateAPIDefinition.

@Override
public APIDefinitionValidationResponse validateAPIDefinition(String apiDefinition, boolean returnJsonContent) throws APIManagementException {
    APIDefinitionValidationResponse validationResponse = new APIDefinitionValidationResponse();
    // import and load AsyncAPI HyperSchema for JSON schema validation
    JSONObject hyperSchema = new JSONObject(ASYNCAPI_JSON_HYPERSCHEMA);
    String protocol = StringUtils.EMPTY;
    boolean validationSuccess = false;
    List<String> validationErrorMessages = null;
    boolean isWebSocket = false;
    JSONObject schemaToBeValidated = new JSONObject(apiDefinition);
    // validate AsyncAPI using JSON schema validation
    try {
        JSONParser parser = new JSONParser();
        org.json.simple.JSONObject json = (org.json.simple.JSONObject) parser.parse(metaSchema);
        SchemaLoader schemaLoader = SchemaLoader.builder().registerSchemaByURI(new URI("http://json-schema.org/draft-07/schema#"), json).schemaJson(hyperSchema).build();
        Schema schemaValidator = schemaLoader.load().build();
        schemaValidator.validate(schemaToBeValidated);
        /*AaiDocument asyncApiDocument = (AaiDocument) Library.readDocumentFromJSONString(apiDefinition);
            validationErrorMessages = new ArrayList<>();
            if (asyncApiDocument.getServers().size() == 1) {
                if (!APIConstants.WS_PROTOCOL.equalsIgnoreCase(asyncApiDocument.getServers().get(0).protocol)) {
                    validationErrorMessages.add("#:The protocol of the server should be 'ws' for websockets");
                }
            }
            if (asyncApiDocument.getServers().size() > 1) {
                validationErrorMessages.add("#:The AsyncAPI definition should contain only a single server for websockets");
            }
            if (asyncApiDocument.getChannels().size() > 1) {
                validationErrorMessages.add("#:The AsyncAPI definition should contain only a single channel for websockets");
            }
            if (validationErrorMessages.size() == 0) {
                validationSuccess = true;
                validationErrorMessages = null;
            }*/
        // AaiDocument asyncApiDocument = (AaiDocument) Library.readDocumentFromJSONString(apiDefinition);
        /*//Checking whether it is a websocket
            validationErrorMessages = new ArrayList<>();
            if (APIConstants.WS_PROTOCOL.equalsIgnoreCase(asyncApiDocument.getServers().get(0).protocol)) {
                if (APIConstants.WS_PROTOCOL.equalsIgnoreCase(protocol)) {
                    isWebSocket = true;
                }
            }*/
        // validating channel count for websockets
        /*if (isWebSocket) {
                if (asyncApiDocument.getChannels().size() > 1) {
                    validationErrorMessages.add("#:The AsyncAPI definition should contain only a single channel for websockets");
                }
            }*/
        /*if (validationErrorMessages.size() == 0) {
                validationSuccess = true;
                validationErrorMessages = null;
            }*/
        validationSuccess = true;
    } catch (ValidationException e) {
        // validation error messages
        validationErrorMessages = e.getAllMessages();
    } catch (URISyntaxException e) {
        String msg = "Error occurred when registering the schema";
        throw new APIManagementException(msg, e);
    } catch (ParseException e) {
        String msg = "Error occurred when parsing the schema";
        throw new APIManagementException(msg, e);
    }
    // TODO: Validation is failing. Need to fix this. Therefore overriding the value as True.
    validationSuccess = true;
    if (validationSuccess) {
        AaiDocument asyncApiDocument = (AaiDocument) Library.readDocumentFromJSONString(apiDefinition);
        ArrayList<String> endpoints = new ArrayList<>();
        if (asyncApiDocument.getServers().size() == 1) {
            protocol = asyncApiDocument.getServers().get(0).protocol;
        }
        /*for (AaiServer x : asyncApiDocument.getServers()){
                endpoints.add(x.url);
            }
            AsyncApiParserUtil.updateValidationResponseAsSuccess(
                    validationResponse,
                    apiDefinition,
                    asyncApiDocument.asyncapi,
                    asyncApiDocument.info.title,
                    asyncApiDocument.info.version,
                    null,                           //asyncApiDocument.getChannels().get(0)._name,
                    asyncApiDocument.info.description,
                    endpoints
            );*/
        /*if (isWebSocket) {
                for (AaiServer x : asyncApiDocument.getServers()){
                    endpoints.add(x.url);
                }
                AsyncApiParserUtil.updateValidationResponseAsSuccess(
                        validationResponse,
                        apiDefinition,
                        asyncApiDocument.asyncapi,
                        asyncApiDocument.info.title,
                        asyncApiDocument.info.version,
                        asyncApiDocument.getChannels().get(0)._name,            //make this null
                        asyncApiDocument.info.description,
                        endpoints
                );
            } else {
                AsyncApiParserUtil.updateValidationResponseAsSuccess(
                        validationResponse,
                        apiDefinition,
                        asyncApiDocument.asyncapi,
                        asyncApiDocument.info.title,
                        asyncApiDocument.info.version,
                        null,
                        asyncApiDocument.info.description,
                        null
                );
            }*/
        AsyncApiParserUtil.updateValidationResponseAsSuccess(validationResponse, apiDefinition, asyncApiDocument.asyncapi, asyncApiDocument.info.title, asyncApiDocument.info.version, null, asyncApiDocument.info.description, null);
        validationResponse.setParser(this);
        if (returnJsonContent) {
            validationResponse.setJsonContent(apiDefinition);
        }
        if (StringUtils.isNotEmpty(protocol)) {
            validationResponse.setProtocol(protocol);
        }
    } else {
        if (validationErrorMessages != null) {
            validationResponse.setValid(false);
            for (String errorMessage : validationErrorMessages) {
                AsyncApiParserUtil.addErrorToValidationResponse(validationResponse, errorMessage);
            }
        }
    }
    return validationResponse;
}
Also used : SchemaLoader(org.everit.json.schema.loader.SchemaLoader) ValidationException(org.everit.json.schema.ValidationException) Schema(org.everit.json.schema.Schema) ArrayList(java.util.ArrayList) AaiDocument(io.apicurio.datamodels.asyncapi.models.AaiDocument) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) APIDefinitionValidationResponse(org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse) JSONObject(org.json.JSONObject) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException)

Aggregations

AaiDocument (io.apicurio.datamodels.asyncapi.models.AaiDocument)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Schema (org.everit.json.schema.Schema)1 ValidationException (org.everit.json.schema.ValidationException)1 SchemaLoader (org.everit.json.schema.loader.SchemaLoader)1 JSONObject (org.json.JSONObject)1 JSONParser (org.json.simple.parser.JSONParser)1 ParseException (org.json.simple.parser.ParseException)1 APIDefinitionValidationResponse (org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1