Skip to content

Commit

Permalink
Fix logical type branch name
Browse files Browse the repository at this point in the history
  • Loading branch information
mtth committed Apr 20, 2020
1 parent cc10abf commit c1eff41
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/types.js
Expand Up @@ -460,10 +460,10 @@ Type.__reset = function (size) {
Object.defineProperty(Type.prototype, 'branchName', {
enumerable: true,
get: function () {
if (this.name) {
return this.name;
}
var type = Type.isType(this, 'logical') ? this.underlyingType : this;
if (type.name) {
return type.name;
}
if (Type.isType(type, 'abstract')) {
return type._concreteTypeName;
}
Expand Down
30 changes: 30 additions & 0 deletions test/test_types.js
Expand Up @@ -2952,6 +2952,36 @@ suite('types', function () {
assert(!t.isValid({int: 3}));
});

test('of records inside wrapped union', function () {
function PassThroughType(schema, opts) {
LogicalType.call(this, schema, opts);
}
util.inherits(PassThroughType, LogicalType);
PassThroughType.prototype._fromValue = function (val) { return val; };
PassThroughType.prototype._toValue = PassThroughType.prototype._fromValue;

var t = types.Type.forSchema(
[
{
type: 'record',
logicalType: 'pt',
name: 'A',
fields: [{name: 'a', type: 'int'}],
},
{
type: 'record',
logicalType: 'pt',
name: 'B',
fields: [{name: 'b', type: 'int'}],
},
],
{logicalTypes: {pt: PassThroughType}, wrapUnions: true},
);
assert(t.isValid({A: {a: 123}}));
assert(t.isValid({B: {b: 456}}));
assert(!t.isValid({B: {a: 456}}));
});

// Unions are slightly tricky to override with logical types since their
// schemas aren't represented as objects.
suite('union logical types', function () {
Expand Down

0 comments on commit c1eff41

Please sign in to comment.