org.apache.avro.SchemaValidator Java Examples

The following examples show how to use org.apache.avro.SchemaValidator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AvroCompatibilityChecker.java    From apicurio-registry with Apache License 2.0 6 votes vote down vote up
/**
 * @see CompatibilityChecker#isCompatibleWith(io.apicurio.registry.rules.compatibility.CompatibilityLevel, java.util.List, java.lang.String)
 */
@Override
public boolean isCompatibleWith(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 true;
    }

    List<Schema> existingSchemas = existingSchemaStrings.stream().map(s -> new Schema.Parser().parse(s)).collect(Collectors.toList());
    Collections.reverse(existingSchemas); // the most recent must come first, i.e. reverse-chronological.
    Schema toValidate = new Schema.Parser().parse(proposedSchemaString);

    try {
        schemaValidator.validate(toValidate, existingSchemas);
        return true;
    } catch (SchemaValidationException e) {
        return false;
    }
}
 
Example #2
Source File: AvroCompatibilityChecker.java    From apicurio-registry with Apache License 2.0 6 votes vote down vote up
private SchemaValidator validatorFor(CompatibilityLevel compatibilityLevel) {
    switch (compatibilityLevel) {
        case BACKWARD:
            return new SchemaValidatorBuilder().canReadStrategy().validateLatest();
        case BACKWARD_TRANSITIVE:
            return new SchemaValidatorBuilder().canReadStrategy().validateAll();
        case FORWARD:
            return new SchemaValidatorBuilder().canBeReadStrategy().validateLatest();
        case FORWARD_TRANSITIVE:
            return new SchemaValidatorBuilder().canBeReadStrategy().validateAll();
        case FULL:
            return new SchemaValidatorBuilder().mutualReadStrategy().validateLatest();
        case FULL_TRANSITIVE:
            return new SchemaValidatorBuilder().mutualReadStrategy().validateAll();
        default:
            return null;
    }

}
 
Example #3
Source File: AvroSchemaBasedCompatibilityCheck.java    From pulsar with Apache License 2.0 6 votes vote down vote up
static SchemaValidator createSchemaValidator(SchemaCompatibilityStrategy compatibilityStrategy) throws IncompatibleSchemaException {
    final SchemaValidatorBuilder validatorBuilder = new SchemaValidatorBuilder();
    switch (compatibilityStrategy) {
        case BACKWARD_TRANSITIVE:
            return createLatestOrAllValidator(validatorBuilder.canReadStrategy(), false);
        case BACKWARD:
            return createLatestOrAllValidator(validatorBuilder.canReadStrategy(), true);
        case FORWARD_TRANSITIVE:
            return createLatestOrAllValidator(validatorBuilder.canBeReadStrategy(), false);
        case FORWARD:
            return createLatestOrAllValidator(validatorBuilder.canBeReadStrategy(), true);
        case FULL_TRANSITIVE:
            return createLatestOrAllValidator(validatorBuilder.mutualReadStrategy(), false);
        case FULL:
            return createLatestOrAllValidator(validatorBuilder.mutualReadStrategy(), true);
        case ALWAYS_COMPATIBLE:
            return AlwaysSchemaValidator.INSTANCE;
        default:
            return NeverSchemaValidator.INSTANCE;
    }
}
 
Example #4
Source File: AvroSchemaBasedCompatibilityCheck.java    From pulsar with Apache License 2.0 4 votes vote down vote up
static SchemaValidator createLatestOrAllValidator(SchemaValidatorBuilder validatorBuilder, boolean onlyLatest) {
    return onlyLatest ? validatorBuilder.validateLatest() : validatorBuilder.validateAll();
}
 
Example #5
Source File: AvroSchemaTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test
public void testSchemaDefinition() throws SchemaValidationException {
    org.apache.avro.Schema schema1 = ReflectData.get().getSchema(DefaultStruct.class);
    AvroSchema<StructWithAnnotations> schema2 = AvroSchema.of(StructWithAnnotations.class);

    String schemaDef1 = schema1.toString();
    String schemaDef2 = new String(schema2.getSchemaInfo().getSchema(), UTF_8);
    assertNotEquals(
        schemaDef1, schemaDef2,
        "schema1 = " + schemaDef1 + ", schema2 = " + schemaDef2);

    SchemaValidator validator = new SchemaValidatorBuilder()
        .mutualReadStrategy()
        .validateLatest();
    try {
        validator.validate(
            schema1,
            Arrays.asList(
                new Schema.Parser().setValidateDefaults(false).parse(schemaDef2)
            )
        );
        fail("Should fail on validating incompatible schemas");
    } catch (SchemaValidationException sve) {
        // expected
    }

    AvroSchema<StructWithAnnotations> schema3 = AvroSchema.of(SchemaDefinition.<StructWithAnnotations>builder().withJsonDef(schemaDef1).build());
    String schemaDef3 = new String(schema3.getSchemaInfo().getSchema(), UTF_8);
    assertEquals(schemaDef1, schemaDef3);
    assertNotEquals(schemaDef2, schemaDef3);

    StructWithAnnotations struct = new StructWithAnnotations();
    struct.setField1(5678);
    // schema2 is using the schema generated from POJO,
    // it allows field2 to be nullable, and field3 has default value.
    schema2.encode(struct);
    try {
        // schema3 is using the schema passed in, which doesn't allow nullable
        schema3.encode(struct);
        fail("Should fail to write the record since the provided schema is incompatible");
    } catch (SchemaSerializationException sse) {
        // expected
    }
}