Search in sources :

Example 6 with DatabindException

use of com.fasterxml.jackson.databind.DatabindException 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);
}
Also used : SchemaPairCompatibility(org.apache.avro.SchemaCompatibility.SchemaPairCompatibility) Schema(org.apache.avro.Schema) FormatSchema(com.fasterxml.jackson.core.FormatSchema) DatabindException(com.fasterxml.jackson.databind.DatabindException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

DatabindException (com.fasterxml.jackson.databind.DatabindException)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Test (org.junit.Test)2 FormatSchema (com.fasterxml.jackson.core.FormatSchema)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 DummyRecord (com.fasterxml.jackson.dataformat.avro.interop.DummyRecord)1 IonObjectMapper (com.fasterxml.jackson.dataformat.ion.IonObjectMapper)1 ProtobufSchema (com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema)1 Schema (org.apache.avro.Schema)1 SchemaPairCompatibility (org.apache.avro.SchemaCompatibility.SchemaPairCompatibility)1