use of org.apache.avro.SchemaCompatibility.SchemaPairCompatibility in project avro by a0x8o.
the class TestSchemaCompatibility method testValidateSchemaNewField.
@Test
public void testValidateSchemaNewField() {
final List<Schema.Field> readerFields = list(new Schema.Field("oldfield1", INT_SCHEMA, null, null), new Schema.Field("newfield1", INT_SCHEMA, null, null));
final Schema reader = Schema.createRecord(readerFields);
SchemaPairCompatibility compatibility = checkReaderWriterCompatibility(reader, WRITER_SCHEMA);
// Test new field without default value.
assertEquals(SchemaCompatibility.SchemaCompatibilityType.INCOMPATIBLE, compatibility.getType());
assertEquals(SchemaCompatibility.SchemaCompatibilityResult.incompatible(SchemaIncompatibilityType.READER_FIELD_MISSING_DEFAULT_VALUE, reader, WRITER_SCHEMA, "newfield1", asList("", "fields", "1")), compatibility.getResult());
assertEquals(String.format("Data encoded using writer schema:%n%s%n" + "will or may fail to decode using reader schema:%n%s%n", WRITER_SCHEMA.toString(true), reader.toString(true)), compatibility.getDescription());
assertEquals(reader, compatibility.getReader());
assertEquals(WRITER_SCHEMA, compatibility.getWriter());
}
use of org.apache.avro.SchemaCompatibility.SchemaPairCompatibility in project jackson-dataformats-binary by FasterXML.
the class AvroSchema method withReaderSchema.
/**
* Method that will consider this schema instance (used as so-called "Writer Schema"),
* and specified "Reader Schema" instance, and will either construct a new schema
* with appropriate translations, to use for reading (if reader and writer schemas are
* not same); or, if schemas are the same, return `this`.
*<p>
* Note that neither `this` instance nor `readerSchema` is ever modified: if an altered
* version is needed, a new schema object will be constructed.
*<p>
* NOTE: this is a relatively expensive operation due to validation (although significant
* part of cost is deferred until the first call to {@link #getReader}) so it is recommended
* that these instances are reused whenever possible.
*
* @param readerSchema "Reader Schema" to use (in Avro terms): schema that specified how
* reader wants to see the data; specifies part of translation needed along with this
* schema (which would be "Writer Schema" in Avro terms).
*
* @throws DatabindException If given reader schema is incompatible with (writer-)
* schema this instance was constructed with,
*/
public AvroSchema withReaderSchema(AvroSchema readerSchema) throws DatabindException {
Schema w = _writerSchema;
Schema r = readerSchema.getAvroSchema();
if (r.equals(w)) {
return this;
}
// First: apply simple renamings:
w = Schema.applyAliases(w, r);
// to match; so let's check that first
if (r.getType() == w.getType()) {
if (!_schemaNamesEqual(w, r)) {
throw DatabindException.from((JsonParser) null, String.format("Incompatible writer/reader schemas: root %ss have different names (\"%s\" vs \"%s\"), no match via aliases", r.getType().getName(), w.getFullName(), r.getFullName()));
}
}
SchemaPairCompatibility comp;
try {
comp = SchemaCompatibility.checkReaderWriterCompatibility(r, w);
} catch (Exception e) {
throw DatabindException.from((JsonParser) null, String.format("Failed to resolve given writer/reader schemas, problem: (%s) %s", e.getClass().getName(), e.getMessage()));
}
if (comp.getType() != SchemaCompatibilityType.COMPATIBLE) {
throw DatabindException.from((JsonParser) null, String.format("Incompatible writer/reader schemas: %s", comp.getDescription()));
}
return Resolving.create(w, r);
}
use of org.apache.avro.SchemaCompatibility.SchemaPairCompatibility in project avro by apache.
the class TestSchemaCompatibility method testReaderWriterCompatibility.
// -----------------------------------------------------------------------------------------------
/**
* Tests reader/writer compatibility validation.
*/
@Test
public void testReaderWriterCompatibility() {
for (ReaderWriter readerWriter : COMPATIBLE_READER_WRITER_TEST_CASES) {
final Schema reader = readerWriter.getReader();
final Schema writer = readerWriter.getWriter();
LOG.debug("Testing compatibility of reader {} with writer {}.", reader, writer);
final SchemaPairCompatibility result = checkReaderWriterCompatibility(reader, writer);
assertEquals(String.format("Expecting reader %s to be compatible with writer %s, but tested incompatible.", reader, writer), SchemaCompatibilityType.COMPATIBLE, result.getType());
}
}
use of org.apache.avro.SchemaCompatibility.SchemaPairCompatibility in project avro by apache.
the class TestSchemaCompatibility method validateIncompatibleSchemas.
// -----------------------------------------------------------------------------------------------
public static void validateIncompatibleSchemas(Schema reader, Schema writer, List<SchemaIncompatibilityType> incompatibilityTypes, List<String> messages, List<String> locations) {
SchemaPairCompatibility compatibility = checkReaderWriterCompatibility(reader, writer);
SchemaCompatibilityResult compatibilityResult = compatibility.getResult();
assertEquals(reader, compatibility.getReader());
assertEquals(writer, compatibility.getWriter());
assertEquals(SchemaCompatibilityType.INCOMPATIBLE, compatibilityResult.getCompatibility());
assertEquals(incompatibilityTypes.size(), compatibilityResult.getIncompatibilities().size());
for (int i = 0; i < incompatibilityTypes.size(); i++) {
Incompatibility incompatibility = compatibilityResult.getIncompatibilities().get(i);
assertSchemaContains(incompatibility.getReaderFragment(), reader);
assertSchemaContains(incompatibility.getWriterFragment(), writer);
assertEquals(incompatibilityTypes.get(i), incompatibility.getType());
assertEquals(messages.get(i), incompatibility.getMessage());
assertEquals(locations.get(i), incompatibility.getLocation());
}
String description = String.format("Data encoded using writer schema:%n%s%n" + "will or may fail to decode using reader schema:%n%s%n", writer.toString(true), reader.toString(true));
assertEquals(description, compatibility.getDescription());
}
use of org.apache.avro.SchemaCompatibility.SchemaPairCompatibility in project avro by apache.
the class TestSchemaCompatibility method testUnionReaderWriterSubsetIncompatibility.
/**
* Reader union schema must contain all writer union branches.
*/
@Test
public void testUnionReaderWriterSubsetIncompatibility() {
final Schema unionWriter = Schema.createUnion(list(INT_SCHEMA, STRING_SCHEMA, LONG_SCHEMA));
final Schema unionReader = Schema.createUnion(list(INT_SCHEMA, STRING_SCHEMA));
final SchemaPairCompatibility result = checkReaderWriterCompatibility(unionReader, unionWriter);
assertEquals(SchemaCompatibilityType.INCOMPATIBLE, result.getType());
}
Aggregations