Skip to content

Commit 895fa83

Browse files
denghongcaihongcai.dhc
andauthored
test: 💍 fix ref should use custom handler circular bug (#128)
Co-authored-by: hongcai.dhc <hongcai.dhc@alibaba-inc.com>
1 parent 5c633be commit 895fa83

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

lib/v1/decoder.js

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

535538
/**

test/custom_handler.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,34 @@ describe('utils.test.js', function () {
9393
assert.deepEqual(output, {'1': { value: '100.06'}, '2': { value: '100.06' }});
9494
});
9595

96+
it('should decode with custom handler if has ref and has circular ref', function () {
97+
var circularObj = { value: '100.06' };
98+
circularObj.ref = circularObj;
99+
hessian.registerDecodeHandler('java.test.circular', function (result) {
100+
// must modify result in place to avoid circular problem
101+
return {
102+
$class: result.$class,
103+
$: result.$,
104+
};
105+
});
106+
var o = { $class: 'java.test.circular', $: circularObj };
107+
var map = new Map();
108+
map.set(1, o);
109+
map.set(2, o);
110+
var buf = hessian.encode({
111+
$class: 'java.util.HashMap',
112+
$: map
113+
}, '2.0');
114+
var output = hessian.decode(buf, '2.0');
115+
/**
116+
* fix problem like ref object reuse
117+
*/
118+
assert(output[1].value === '100.06');
119+
assert(output[2].value === '100.06');
120+
assert(output[1].ref.ref.ref.ref.value === '100.06');
121+
hessian.deregisterDecodeHandler('java.test.circular');
122+
});
123+
96124
if (!supportES6Map) {
97125
return;
98126
}

0 commit comments

Comments
 (0)