Skip to content

Commit 543b2bc

Browse files
denghongcaihongcai.dhc
andauthored
fix: 🐛 ref should use custom handler (#126)
Co-authored-by: hongcai.dhc <hongcai.dhc@alibaba-inc.com>
1 parent 9012536 commit 543b2bc

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

lib/v1/decoder.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -529,10 +529,7 @@ proto.readList = proto.readArray;
529529
proto.readRef = function (withType) {
530530
var rid = this.readRefId();
531531
var obj = this.refMap[rid];
532-
if (!withType && obj && utils.hasOwnProperty(obj, '$')) {
533-
obj = obj.$;
534-
}
535-
return obj;
532+
return handle(obj, withType);
536533
};
537534

538535
/**

test/custom_handler.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,32 @@ describe('utils.test.js', function () {
6767
assert.deepEqual(output, { value: '100.06' });
6868
});
6969

70+
it('should decode with custom handler if has ref', function () {
71+
hessian.registerDecodeHandler('java.math.BigDecimal', function (result) {
72+
return {
73+
$class: result.$class,
74+
$: result.$.value,
75+
};
76+
});
77+
var o = { $class: 'java.math.BigDecimal', $: { value: '100.06' } };
78+
var map = new Map();
79+
map.set(1, o);
80+
map.set(2, o);
81+
var buf = hessian.encode({
82+
$class: 'java.util.HashMap',
83+
$: map
84+
}, '2.0');
85+
var output = hessian.decode(buf, '2.0');
86+
/**
87+
* fix problem like ref object reuse
88+
*/
89+
assert(output[1] === '100.06');
90+
assert(output[2] === '100.06');
91+
hessian.deregisterDecodeHandler('java.math.BigDecimal');
92+
output = hessian.decode(buf, '2.0');
93+
assert.deepEqual(output, {'1': { value: '100.06'}, '2': { value: '100.06' }});
94+
});
95+
7096
if (!supportES6Map) {
7197
return;
7298
}

0 commit comments

Comments
 (0)