Skip to content

Commit

Permalink
Allow ignoring namespaces during resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
mtth committed Aug 14, 2021
1 parent cf53f11 commit c639014
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
30 changes: 25 additions & 5 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1685,11 +1685,11 @@ EnumType.prototype.compare = function (val1, val2) {
return utils.compare(this._indices[val1], this._indices[val2]);
};

EnumType.prototype._update = function (resolver, type) {
EnumType.prototype._update = function (resolver, type, opts) {
var symbols = this.symbols;
if (
type.typeName === 'enum' &&
(!type.name || ~getAliases(this).indexOf(type.name)) &&
hasCompatibleName(this, type, !opts.ignoreNamespaces) &&
(
type.symbols.every(function (s) { return ~symbols.indexOf(s); }) ||
this.default !== undefined
Expand Down Expand Up @@ -1760,11 +1760,11 @@ FixedType.prototype._match = function (tap1, tap2) {

FixedType.prototype.compare = Buffer.compare;

FixedType.prototype._update = function (resolver, type) {
FixedType.prototype._update = function (resolver, type, opts) {
if (
type.typeName === 'fixed' &&
this.size === type.size &&
(!type.name || ~getAliases(this).indexOf(type.name))
hasCompatibleName(this, type, !opts.ignoreNamespaces)
) {
resolver.size = this.size;
resolver._read = this._read;
Expand Down Expand Up @@ -2343,7 +2343,7 @@ RecordType.prototype._createWriter = function () {

RecordType.prototype._update = function (resolver, type, opts) {
// jshint -W054
if (type.name && !~getAliases(this).indexOf(type.name)) {
if (!hasCompatibleName(this, type, !opts.ignoreNamespaces)) {
throw new Error(f('no alias found for %s', type.name));
}

Expand Down Expand Up @@ -2920,6 +2920,26 @@ function getAliases(obj) {
return Object.keys(names);
}

/** Checks if a type can be read as another based on name resolution rules. */
function hasCompatibleName(reader, writer, strict) {
if (!writer.name) {
return true;
}
var name = strict ? writer.name : utils.unqualify(writer.name);
var aliases = getAliases(reader);
var i, l, alias;
for (i = 0, l = aliases.length; i < l; i++) {
alias = aliases[i];
if (!strict) {
alias = utils.unqualify(alias);
}
if (alias === name) {
return true;
}
}
return false;
}

/**
* Check whether a type's name is a primitive.
*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "avsc",
"version": "5.7.2",
"version": "5.7.3",
"description": "Avro for JavaScript",
"homepage": "https://github.com/mtth/avsc",
"keywords": [
Expand Down
16 changes: 16 additions & 0 deletions test/test_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3583,6 +3583,22 @@ suite('types', function () {
assert.doesNotThrow(function () { t3.createResolver(t1); });
});

test('ignore namespaces', function () {
var t1 = Type.forSchema({type: 'fixed', name: 'foo.Two', size: 2});
var t2 = Type.forSchema(
{type: 'fixed', size: 2, name: 'bar.Deux', aliases: ['bar.Two']}
);
assert.throws(function () { t1.createResolver(t2); });
assert.doesNotThrow(function () {
t2.createResolver(t1, {ignoreNamespaces: true});
});
var t3 = Type.forSchema({type: 'fixed', size: 2, name: 'Two'});
assert.throws(function () { t3.createResolver(t1); });
assert.doesNotThrow(function () {
t3.createResolver(t1, {ignoreNamespaces: true});
});
});

});

suite('type references', function () {
Expand Down

0 comments on commit c639014

Please sign in to comment.