Skip to content

Commit b231db0

Browse files
authored
fix: compose cache key with class and fields length (#102)
1 parent f06859c commit b231db0

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

lib/v2/decoder.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,17 +519,20 @@ var INNER_CLASS_LABEL = '$$ignore_inner_property$$';
519519

520520
proto._readObjectDefinition = function () {
521521
var classname = this.readString();
522+
var fieldsLength = this.readInt();
523+
// compose cache key with class name and fields length
524+
// more safely
525+
var cachekey = classname + '##' + fieldsLength;
522526

523527
// get class definition from cache
524-
var cacheClz = this.classCache && this.classCache.get(classname);
528+
var cacheClz = this.classCache && this.classCache.get(cachekey);
525529
if (cacheClz) {
526530
this.byteBuffer.skip(cacheClz.length);
527531
this.classes.push(cacheClz);
528532
return cacheClz;
529533
}
530534

531535
var pos = this.byteBuffer.position();
532-
var fieldsLength = this.readInt();
533536
var fields = [];
534537
for (var i = 0; i < fieldsLength; i++) {
535538
var name = this.readString();
@@ -548,7 +551,7 @@ proto._readObjectDefinition = function () {
548551
this.classes.push(clz);
549552
// set class definition into cache
550553
if (this.classCache) {
551-
this.classCache.set(clz.name, clz);
554+
this.classCache.set(cachekey, clz);
552555
}
553556
return clz;
554557
};

test/v2.decode.cache.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ describe('v2.decode.cache.test.js', function () {
117117
]);
118118
});
119119

120-
it('should write "{$class: "hessian.test.demo.Car", $: {a: 1}}"', function () {
120+
it('should write "{$class: "hessian.test.demo.Car", $: {a: 1, b: "map"}}"', function () {
121121
var obj = {
122122
$class: 'hessian.test.demo.Car',
123123
$: {a: 1, b: 'map'}
@@ -128,6 +128,17 @@ describe('v2.decode.cache.test.js', function () {
128128
assert.deepEqual(hessian.decode(buf, '2.0', true), obj);
129129
});
130130

131+
it('should write "{$class: "hessian.test.demo.Car", $: {a: 1, b: "map", c: 2}}"', function () {
132+
var obj = {
133+
$class: 'hessian.test.demo.Car',
134+
$: {a: 1, b: 'map', c: 2}
135+
};
136+
var buf = hessian.encode(obj, '2.0');
137+
assert(buf[0] === 0x43);
138+
assert.deepEqual(hessian.decode(buf, '2.0'), obj.$);
139+
assert.deepEqual(hessian.decode(buf, '2.0', true), obj);
140+
});
141+
131142
it('should read one car list', function () {
132143
assert.deepEqual(hessian.decode(utils.bytes('v2/map/one_car_list'), '2.0'), [
133144
{ a: 'a',

0 commit comments

Comments
 (0)