use of org.apache.avro.SchemaValidationException in project hopsworks by logicalclocks.
the class SubjectsController method isCompatible.
private boolean isCompatible(Project project, String subject, Schema schema) throws SchemaException {
SchemaCompatibility sc = getSubjectOrProjectCompatibility(project, subject);
if (sc.equals(SchemaCompatibility.NONE)) {
return true;
}
SchemaValidator validator = getSchemaValidator(sc);
List<Schema> previousSchemas = subjectsFacade.findSubjectByName(project, subject).stream().sorted(Comparator.comparing(Subjects::getVersion).reversed()).map(s -> new Schema.Parser().parse(s.getSchema().getSchema())).collect(Collectors.toList());
try {
validator.validate(schema, previousSchemas);
} catch (SchemaValidationException e) {
return false;
}
return true;
}
use of org.apache.avro.SchemaValidationException in project apicurio-registry by Apicurio.
the class AvroCompatibilityChecker method testCompatibility.
/**
* @see CompatibilityChecker#testCompatibility(io.apicurio.registry.rules.compatibility.CompatibilityLevel, java.util.List, java.lang.String)
*/
@Override
public CompatibilityExecutionResult testCompatibility(CompatibilityLevel compatibilityLevel, List<String> existingSchemaStrings, String proposedSchemaString) {
requireNonNull(compatibilityLevel, "compatibilityLevel MUST NOT be null");
requireNonNull(existingSchemaStrings, "existingSchemaStrings MUST NOT be null");
requireNonNull(proposedSchemaString, "proposedSchemaString MUST NOT be null");
SchemaValidator schemaValidator = validatorFor(compatibilityLevel);
if (schemaValidator == null) {
return CompatibilityExecutionResult.compatible();
}
try {
List<Schema> existingSchemas = existingSchemaStrings.stream().map(s -> new Schema.Parser().parse(s)).collect(Collectors.toList());
// the most recent must come first, i.e. reverse-chronological.
Collections.reverse(existingSchemas);
Schema toValidate = new Schema.Parser().parse(proposedSchemaString);
schemaValidator.validate(toValidate, existingSchemas);
return CompatibilityExecutionResult.compatible();
} catch (SchemaValidationException e) {
return CompatibilityExecutionResult.incompatible(e);
} catch (SchemaParseException | AvroTypeException e) {
throw new UnprocessableSchemaException(e.getMessage());
}
}
Aggregations