Skip to content

Commit

Permalink
fix: nested generic bug
Browse files Browse the repository at this point in the history
  • Loading branch information
gxcsoccer committed Jan 17, 2019
1 parent f31504b commit e26b2cb
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 5 deletions.
6 changes: 6 additions & 0 deletions lib/serialize/hessian/compile.js
Expand Up @@ -153,6 +153,9 @@ function compile(uniqueId, info, classMap, version) {
const attr = Object.assign({}, classInfo[key]);
if (has(attr, 'typeAliasIndex') && Array.isArray(info.generic)) {
attr.type = info.generic[attr.typeAliasIndex].type;
if (info.generic[attr.typeAliasIndex].generic) {
attr.generic = info.generic[attr.typeAliasIndex].generic;
}
}
const uniqueId = utils.normalizeUniqId(attr, version);
gen('compile(\'%s\', %j, classMap, version)(obj[\'%s\'], encoder, appClassMap);', uniqueId, attr, key);
Expand All @@ -171,6 +174,9 @@ function compile(uniqueId, info, classMap, version) {
const attr = Object.assign({}, classInfo[key]);
if (has(attr, 'typeAliasIndex') && Array.isArray(info.generic)) {
attr.type = info.generic[attr.typeAliasIndex].type;
if (info.generic[attr.typeAliasIndex].generic) {
attr.generic = info.generic[attr.typeAliasIndex].generic;
}
}
const uniqueId = utils.normalizeUniqId(attr, version);
gen('compile(\'%s\', %j, classMap, version)(obj[\'%s\'], encoder, appClassMap);', uniqueId, attr, key);
Expand Down
10 changes: 5 additions & 5 deletions package.json
Expand Up @@ -32,7 +32,7 @@
"homepage": "https://github.com/dubbo/dubbo-remoting-js#readme",
"dependencies": {
"@protobufjs/codegen": "^2.0.4",
"debug": "^4.1.0",
"debug": "^4.1.1",
"hessian.js": "^2.8.1",
"is-type-of": "^1.2.1",
"long": "^4.0.0",
Expand All @@ -43,12 +43,12 @@
"await-event": "^2.1.0",
"beautify-benchmark": "^0.2.4",
"benchmark": "^2.1.4",
"egg-bin": "^4.9.0",
"egg-ci": "^1.10.0",
"eslint": "^5.9.0",
"egg-bin": "^4.10.0",
"egg-ci": "^1.11.0",
"eslint": "^5.12.0",
"eslint-config-egg": "^7.1.0",
"js-to-java": "^2.6.0",
"sofa-rpc-node": "^1.6.0"
"sofa-rpc-node": "^1.7.0"
},
"engines": {
"node": ">= 8.0.0"
Expand Down
27 changes: 27 additions & 0 deletions test/fixtures/class_map.js
Expand Up @@ -273,4 +273,31 @@ module.exports = {
'isEnum': true,
},
},

'com.eggjs.dubbo.GenericResult': {
'success': {
'type': 'java.lang.Boolean'
},
'result': {
'type': 'T',
'typeAliasIndex': 0
},
'error': {
'type': 'U',
'typeAliasIndex': 1
}
},
'com.eggjs.dubbo.HelloResponse': {
'hello': {
'type': 'java.lang.String'
}
},
'com.eggjs.dubbo.HelloError': {
'name': {
'type': 'java.lang.String'
},
'message': {
'type': 'java.lang.String'
}
}
};
63 changes: 63 additions & 0 deletions test/hessian.test.js
Expand Up @@ -1119,6 +1119,69 @@ describe('test/hessian.test.js', () => {
assert.deepEqual(buf1, buf3);
});

it('should support nested generic with typeAliasIndex', () => {
const obj = {
$class: 'com.eggjs.dubbo.GenericResult',
$: {
success: true,
result: [{
hello: 'world',
}],
error: {
name: 'MockError',
message: 'mock MockError',
},
},
generic: [{
type: 'java.util.List',
generic: [{
type: 'com.eggjs.dubbo.HelloResponse',
}],
}, {
type: 'com.eggjs.dubbo.HelloError',
}],
};
const buf1 = hessian.encode({
$class: 'com.eggjs.dubbo.GenericResult',
$: {
success: {
$class: 'java.lang.Boolean',
$: true,
},
result: {
$class: 'java.util.List',
$: [{
$class: 'com.eggjs.dubbo.HelloResponse',
$: {
hello: {
$class: 'java.lang.String',
$: 'world',
},
},
}],
},
error: {
$class: 'com.eggjs.dubbo.HelloError',
$: {
name: {
$class: 'java.lang.String',
$: 'MockError',
},
message: {
$class: 'java.lang.String',
$: 'mock MockError',
},
},
},
},
}, version);
const buf2 = encode(obj, version, classMap);
assert.deepEqual(buf1, buf2);

const buf3 = encode(obj, version, classMap);
assert.deepEqual(buf1, buf3);
});

it('should class inheritance', () => {
const obj = {
$class: 'com.alipay.test.Father',
Expand Down

0 comments on commit e26b2cb

Please sign in to comment.