Skip to content

Commit 8f99ef5

Browse files
committed
fix: missing ref flag when writing type ref
1 parent 7c14b64 commit 8f99ef5

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lib/v2/encoder.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ proto.writeType = function (type) {
416416
var ref = this._typeRefs.indexOf(type);
417417
if (ref >= 0) {
418418
var TYPE_REF = 0x75; // 'u'
419+
this.byteBuffer.put(TYPE_REF);
419420
this.writeInt(ref);
420421
} else {
421422
this._typeRefs.push(type);

test/special_cases.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,39 @@ describe('test/special_cases.test', function() {
8888
ensureValidIdentifier('a+c');
8989
}, /invalid identifier\: a\+c/);
9090
});
91+
92+
it('should write type with ref for the second time', function () {
93+
const encoder = hessian.encoderV2.reset();
94+
// writeObject
95+
encoder._writeObjectBegin('org.bson.Document');
96+
encoder.writeInt(2);
97+
encoder.writeString('key1');
98+
encoder.writeString('key2');
99+
encoder._writeObjectBegin('org.bson.Document');
100+
// writeMap key1
101+
encoder.byteBuffer.put(0x4d);
102+
encoder.writeType('org.bson.Column');
103+
encoder.byteBuffer.put(0x7a);
104+
// writeMap key2
105+
encoder.byteBuffer.put(0x4d);
106+
encoder.writeType('org.bson.Column');
107+
encoder.byteBuffer.put(0x7a);
108+
109+
const buf = encoder.get();
110+
const res = hessian.decode(buf, '2.0', { withType: true });
111+
assert.deepEqual(res, {
112+
$class: 'org.bson.Document',
113+
$: {
114+
key1: {
115+
$class: 'org.bson.Column',
116+
$: {},
117+
},
118+
key2: {
119+
$class: 'org.bson.Column',
120+
$: {},
121+
},
122+
},
123+
});
124+
});
91125
});
92126
});

0 commit comments

Comments
 (0)