Skip to content

Commit bd14cde

Browse files
committed
Merge pull request #47 from node-modules/fix-inner-class
fix: skip innerClass ref field like this$\d
2 parents 4c439be + 75366f2 commit bd14cde

File tree

4 files changed

+67
-22
lines changed

4 files changed

+67
-22
lines changed

lib/v1/decoder.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,10 +412,13 @@ proto.readObject = function (withType) {
412412
while (label !== 'z') {
413413
this.byteBuffer.position(this.byteBuffer.position() - 1);
414414
key = this.read();
415+
var value = this.read(withType);
416+
label = this.byteBuffer.getChar();
415417
// property name will auto transfer to a String type.
416418
debug('read object prop: %j with type: %s', key, withType);
417-
result.$[key] = this.read(withType);
418-
label = this.byteBuffer.getChar();
419+
if (!/^this\$\d+$/.test(key)) {
420+
result.$[key] = value;
421+
}
419422
}
420423
debug('read object finish');
421424

lib/v2/decoder.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,6 @@ proto._readObjectDefinition = function () {
560560
proto.readObject = function (withType) {
561561
var code = this.byteBuffer.get();
562562
var ref;
563-
564563
if (code === 0x43) {
565564
// C(x43) type fields-length(writeInt) fields-names(writeString) o ref fields-values(write)
566565
this._readObjectDefinition();
@@ -572,7 +571,7 @@ proto.readObject = function (withType) {
572571
} else {
573572
this.throwError('readObject', code);
574573
}
575-
574+
576575
var cls = this.classes[ref];
577576
debug('readObject %s, ref: %s', cls.name, ref);
578577

@@ -585,7 +584,10 @@ proto.readObject = function (withType) {
585584
var fields = cls.fields;
586585
for (var i = 0; i < fields.length; i++) {
587586
var name = fields[i];
588-
result.$[name] = this.read(withType);
587+
var value = this.read(withType);
588+
if (!/^this\$\d+$/.test(name)) {
589+
result.$[name] = value;
590+
}
589591
}
590592

591593
if (/Exception$/.test(cls.name)) {

test/decode.circular.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**!
2+
* hessian.js - test/decode.circular.test.js
3+
*
4+
* Copyright(c) 2015
5+
*
6+
* Authors:
7+
* tangyao <tangyao@alipay.com> (http://tangyao.me/)
8+
*/
9+
10+
'use strict';
11+
12+
/**
13+
* Module dependencies.
14+
*/
15+
16+
var should = require('should');
17+
var hessian = require('../');
18+
var utils = require('./utils');
19+
20+
describe('test/decode.circular.test.js', function () {
21+
22+
it('v1 decode()', function () {
23+
var data = new Buffer([77, 116, 0, 49, 99, 111, 109, 46, 97, 108, 105, 112, 97, 121, 46, 99, 111, 110, 102, 105, 103, 115, 101, 114, 118, 101, 114, 46, 99, 111, 110, 102, 114, 101, 103, 95, 116, 101, 115, 116, 46, 79, 110, 108, 105, 110, 101, 77, 111, 100, 117, 108, 101, 83, 0, 6, 109, 111, 100, 117, 108, 101, 83, 0, 1, 97, 83, 0, 4, 100, 101, 115, 99, 83, 0, 1, 98, 83, 0, 8, 118, 101, 114, 115, 105, 111, 110, 115, 86, 108, 0, 0, 0, 1, 77, 116, 0, 57, 99, 111, 109, 46, 97, 108, 105, 112, 97, 121, 46, 99, 111, 110, 102, 105, 103, 115, 101, 114, 118, 101, 114, 46, 99, 111, 110, 102, 114, 101, 103, 95, 116, 101, 115, 116, 46, 79, 110, 108, 105, 110, 101, 77, 111, 100, 117, 108, 101, 36, 86, 101, 114, 115, 105, 111, 110, 83, 0, 7, 118, 101, 114, 115, 105, 111, 110, 83, 0, 1, 99, 83, 0, 6, 97, 115, 115, 101, 116, 115, 86, 108, 0, 0, 0, 1, 83, 0, 1, 105, 122, 83, 0, 6, 116, 104, 105, 115, 36, 48, 82, 0, 0, 0, 0, 122, 122, 122]);
24+
var rs = hessian.decode(data);
25+
JSON.stringify(rs).should.eql('{"module":"a","desc":"b","versions":[{"version":"c","assets":["i"]}]}');
26+
});
27+
28+
29+
it('v2 decode()', function () {
30+
var javabuf = utils.bytes('v2/object/ConnectionRequest');
31+
var connreq1 = hessian.decode(javabuf, '2.0');
32+
connreq1.should.have.keys('ctx');
33+
connreq1.ctx.should.have.keys('id');
34+
JSON.stringify(connreq1).should.eql('{"ctx":{"id":101}}');
35+
});
36+
37+
});

test/object.test.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,32 @@ describe('object.test.js', function () {
6060
it('should decode and encode ConnectionRequest', function () {
6161
var javabuf = utils.bytes('v1/object/ConnectionRequest');
6262
var connreq = hessian.decode(javabuf, '1.0', true);
63-
6463
var jsconnreq = {
6564
$class: 'hessian.ConnectionRequest',
6665
$: {
6766
ctx: {
6867
$class: 'hessian.ConnectionRequest$RequestContext',
6968
$: {
70-
id: 101,
71-
'this$0': null
69+
id: {
70+
$class: 'int',
71+
$: 101
72+
},
73+
// 'this$0': null
7274
}
7375
}
7476
}
7577
};
7678

77-
jsconnreq.$.ctx.$.this$0 = jsconnreq;
78-
79+
// jsconnreq.$.ctx.$.this$0 = jsconnreq;
80+
connreq.should.eql(jsconnreq);
7981
var jsbuf = hessian.encode(connreq, '1.0');
8082
var jsbuf2 = hessian.encode(jsconnreq, '1.0');
81-
jsbuf2.should.length(javabuf.length);
82-
jsbuf2.should.eql(javabuf);
83+
// because of skip field this$0, the length of course not eql.
84+
// jsbuf2.should.length(javabuf.length);
85+
// jsbuf2.should.eql(javabuf);
8386

84-
jsbuf.should.length(javabuf.length);
85-
jsbuf.should.eql(javabuf);
87+
// jsbuf.should.length(javabuf.length);
88+
// jsbuf.should.eql(javabuf);
8689

8790
var jsbuf2Again = hessian.encode(jsconnreq, '1.0');
8891
jsbuf2Again.should.eql(jsbuf2);
@@ -389,7 +392,7 @@ describe('object.test.js', function () {
389392
var javabuf = utils.bytes('v2/object/ConnectionRequest');
390393
var connreq1 = hessian.decode(javabuf, '2.0');
391394
connreq1.should.have.keys('ctx');
392-
connreq1.ctx.should.have.keys('id', 'this$0');
395+
connreq1.ctx.should.have.keys('id'); // 'this$0'
393396
connreq1.ctx.id.should.equal(101);
394397

395398
var connreq = hessian.decode(javabuf, '2.0', true);
@@ -400,21 +403,21 @@ describe('object.test.js', function () {
400403
$class: 'hessian.ConnectionRequest$RequestContext',
401404
$: {
402405
id: 101,
403-
'this$0': null
406+
// 'this$0': null
404407
}
405408
}
406409
}
407410
};
408411

409-
jsconnreq.$.ctx.$.this$0 = jsconnreq;
410-
412+
// jsconnreq.$.ctx.$.this$0 = jsconnreq;
413+
jsconnreq.should.eql(connreq);
411414
var jsbuf = hessian.encode(connreq, '2.0');
412415
var jsbuf2 = hessian.encode(jsconnreq, '2.0');
413-
jsbuf2.should.length(javabuf.length);
414-
jsbuf2.should.eql(javabuf);
416+
// jsbuf2.should.length(javabuf.length);
417+
// jsbuf2.should.eql(javabuf);
415418

416-
jsbuf.should.length(javabuf.length);
417-
jsbuf.should.eql(javabuf);
419+
// jsbuf.should.length(javabuf.length);
420+
// jsbuf.should.eql(javabuf);
418421
});
419422

420423
it('should decode hessian 1.0 ConnectionRequest', function () {

0 commit comments

Comments
 (0)