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);
}
Aggregations