org.apache.avro.SchemaValidationException Java Examples

The following examples show how to use org.apache.avro.SchemaValidationException. 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: ExpressionCallTest.java    From depends with MIT License 5 votes vote down vote up
public void validate(Schema toValidate, Iterable<Schema> schemasInOrder)
    throws SchemaValidationException {
  Iterator<Schema> schemas = schemasInOrder.iterator();
  while (schemas.hasNext()) {
    Schema existing = schemas.next();
    strategy.validate(toValidate, existing);
  }
}
 
Example #3
Source File: NeverSchemaValidator.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public void validate(Schema toValidate, Iterable<Schema> existing)
        throws SchemaValidationException {
    for (Schema s : existing) {
        // only throw exception if there are existing schemas
        throw new SchemaValidationException(toValidate, toValidate);
    }
}
 
Example #4
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
    }
}
 
Example #5
Source File: AvroUtils.java    From kareldb with Apache License 2.0 2 votes vote down vote up
/**
 * Check the compatibility between the new schema and the specified schemas
 *
 * @param newSchema       The new schema
 * @param previousSchemas Full schema history in chronological order
 */
public static void checkCompatibility(Schema newSchema, List<Schema> previousSchemas)
    throws SchemaValidationException {
    List<Schema> previousSchemasCopy = new ArrayList<>(previousSchemas);
    BACKWARD_TRANSITIVE_VALIDATOR.validate(newSchema, previousSchemasCopy);
}
 
Example #6
Source File: ExpressionCallTest.java    From depends with MIT License 2 votes vote down vote up
/**
 * Validates that one schema is compatible with another.
 *
 * @throws SchemaValidationException if the schemas are not compatible.
 */
void validate(Schema toValidate, Schema existing)
    throws SchemaValidationException;