Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing ref flag when writing type ref #143

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/node-modules/hessian.js @gxkl master 分支是否也有一样的 bug?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/node-modules/hessian.js/blob/master/lib/v2/decoder.js#L493
master 上的实现是 default 为 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',
$: {},
},
},
});
});
});
});
Loading