Skip to content

Commit

Permalink
fix: missing ref flag when writing type ref (#143)
Browse files Browse the repository at this point in the history
v2 版本,修复写类型 ref 时,缺失标志位问题
  • Loading branch information
gxkl authored Aug 1, 2024
1 parent 7c14b64 commit 1de56e9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/v2/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ proto.writeType = function (type) {
var ref = this._typeRefs.indexOf(type);
if (ref >= 0) {
var TYPE_REF = 0x75; // 'u'
this.byteBuffer.put(TYPE_REF);
this.writeInt(ref);
} else {
this._typeRefs.push(type);
Expand Down
34 changes: 34 additions & 0 deletions test/special_cases.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,39 @@ describe('test/special_cases.test', function() {
ensureValidIdentifier('a+c');
}, /invalid identifier\: a\+c/);
});

it('should write type with ref for the second time', function () {
const encoder = hessian.encoderV2.reset();
// writeObject
encoder._writeObjectBegin('org.bson.Document');
encoder.writeInt(2);
encoder.writeString('key1');
encoder.writeString('key2');
encoder._writeObjectBegin('org.bson.Document');
// writeMap key1
encoder.byteBuffer.put(0x4d);
encoder.writeType('org.bson.Column');
encoder.byteBuffer.put(0x7a);
// writeMap key2
encoder.byteBuffer.put(0x4d);
encoder.writeType('org.bson.Column');
encoder.byteBuffer.put(0x7a);

const buf = encoder.get();
const res = hessian.decode(buf, '2.0', { withType: true });
assert.deepEqual(res, {
$class: 'org.bson.Document',
$: {
key1: {
$class: 'org.bson.Column',
$: {},
},
key2: {
$class: 'org.bson.Column',
$: {},
},
},
});
});
});
});

0 comments on commit 1de56e9

Please sign in to comment.