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;
}
Aggregations