Permalink
Browse files

fix: encode enum defaultValue issue & list generic issue

  • Loading branch information...
gxcsoccer committed Oct 29, 2018
1 parent d25f8ea commit 3ce2c930b325d89f976c43ac0906f6a9a579537b
Showing with 69 additions and 7 deletions.
  1. +1 −2 lib/serialize/hessian/compile.js
  2. +10 −5 lib/serialize/hessian/utils.js
  3. +13 −0 test/fixtures/class_map.js
  4. +45 −0 test/hessian.test.js
@@ -106,7 +106,7 @@ function compile(uniqueId, info, classMap, version) {
};
const uniqueId = utils.normalizeUniqId(item, version);
gen('for (const item of obj) {');
gen(' compile(\'%s\', %j, classMap, version)(item, encoder, appClassMap);', uniqueId, item);
gen(' compile(\'%s\', %j, classMap, version)(item && item.$class ? item.$ : item, encoder, appClassMap);', uniqueId, item);
gen('}');
gen('if (hasEnd) { encoder.byteBuffer.putChar(\'z\'); }');
}
@@ -161,7 +161,6 @@ function compile(uniqueId, info, classMap, version) {
}
} else if (info.isEnum) {
gen('if (obj == null) { return encoder.writeNull(); }');
gen('if (encoder._checkRef(obj)) { return; }');
if (version === '1.0') {
gen('encoder.byteBuffer.put(0x4d);');
@@ -3,18 +3,23 @@
const defaultValueMap = new Map();
let defaultValueId = 0;
function normalizeGeneric(type) {
if (!type.generic) return '';
let str = '';
for (const item of type.generic) {
str += ('#' + item.type + normalizeGeneric(item));
}
return str;
}
function normalizeUniqId(info, version) {
let type = info.type || info.$class || info.$abstractClass;
if (info.isArray) {
let arrayDepth = info.arrayDepth || 1;
while (arrayDepth--) type = '[' + type;
}
let fnKey = type;
if (info.generic) {
for (const item of info.generic) {
fnKey += ('#' + item.type);
}
}
fnKey += normalizeGeneric(info);
if (info.defaultValue) {
if (!defaultValueMap.has(info.defaultValue)) {
defaultValueMap.set(info.defaultValue, defaultValueId++);
@@ -260,4 +260,17 @@ module.exports = {
'typeAliasIndex': 0,
},
},
'com.sofa.TestObject': {
'oneEnum': {
'type': 'com.sofa.OneEnum',
'defaultValue': 'DEFAULT',
'isEnum': true,
},
'twoEnum': {
'type': 'com.sofa.TwoEnum',
'defaultValue': 'DEFAULT',
'isEnum': true,
},
},
};
@@ -6,6 +6,7 @@ const java = require('js-to-java');
const hessian = require('hessian.js');
const classMap = require('./fixtures/class_map');
const { encoderV1, encoderV2 } = require('hessian.js');
const utils = require('../lib/serialize/hessian/utils');
const compile = require('../lib/serialize/hessian/compile');
const versions = [ '1.0', '2.0' ];
@@ -807,6 +808,20 @@ describe('test/hessian.test.js', () => {
assert.deepEqual(buf1, buf3);
});
it('should encode array 2', () => {
const obj = {
$class: 'int',
$: [ 1, 2, 3 ],
isArray: true,
};
const buf1 = hessian.encode({ $class: '[int', $: [{ $class: 'int', $: 1 }, { $class: 'int', $: 2 }, { $class: 'int', $: 3 }] }, version);
const buf2 = encode(obj, version, {});
assert.deepEqual(buf1, buf2);
const buf3 = encode({ $class: '[int', $: [{ $class: 'int', $: 1 }, { $class: 'int', $: 2 }, { $class: 'int', $: 3 }] }, version, {});
assert.deepEqual(buf1, buf3);
});
it('should encode multi-dimentional array', () => {
const obj = {
$class: 'java.lang.String',
@@ -1134,6 +1149,36 @@ describe('test/hessian.test.js', () => {
const buf3 = encode(obj, version, classMap);
assert.deepEqual(buf1, buf3);
});
it('should handle same enum defaultValue', () => {
const buf1 = hessian.encode({
$class: 'com.sofa.TestObject',
$: {
oneEnum: { $class: 'com.sofa.OneEnum', $: { name: 'DEFAULT' } },
twoEnum: { $class: 'com.sofa.TwoEnum', $: { name: 'DEFAULT' } },
},
}, version);
const buf2 = encode({
$class: 'com.sofa.TestObject',
$: {},
}, version, classMap);
assert.deepEqual(buf1, buf2);
});
});
});
it('should handle generic', () => {
const info = {
$class: 'java.util.List',
$: [],
generic: [{
type: 'java.util.List',
generic: [{ type: 'com.sofa.testObject' }],
}],
};
let id = utils.normalizeUniqId(info, '2.0');
assert(id === 'java.util.List#java.util.List#com.sofa.testObject#2.0');
id = utils.normalizeUniqId(info, '1.0');
assert(id === 'java.util.List#java.util.List#com.sofa.testObject#1.0');
});
});

0 comments on commit 3ce2c93

Please sign in to comment.