Open
Description
protobuf.js version: 6.8.3
My protobuf was defined as follows:
syntax = "proto3";
package mytest;
message CommunicationFrame {
string msg_type =1;//{String}
string msg_no = 2;//{String}
}
message User {
string user_id = 1;
string nickname = 2;
string avatar = 3;
uint32 user_type = 4;
uint32 device_type = 5;
}
message ResponsePackage {//
CommunicationFrame head_frame = 1;
ResponseMessage logical_frame = 2;
}
message ResponseMessage {
message ForbidUserListRes {
message User {
string user_id = 1;
string nickname = 2;
}
repeated User data = 1;
}
message GetGroupUserListRes {
string group_id = 1;
repeated User data = 2;
}
oneof resp {
GetGroupUserListRes get_group_user_list = 1;
User update_user_info = 2;
}
}
There is a User
message defined in ForbidUserListRes
, and a same name one defined in outer. And I expect the update_user_info
field use the outer one, but after encode a ResponsePackage
and then decode it, I found it use the inner one.
const protobuf = require('protobufjs');
const path = require('path');
const root = new protobuf.Root();
const PROTO_PATH = path.resolve(__dirname, './test.proto');
const protoRoot = root.loadSync(PROTO_PATH, { keepCase: true, enumsAsStrings:true });
const protoTypes = protoRoot.nested.mytest;
const ResponseMessage = protoTypes.ResponseMessage;
const ResponsePackage = protoTypes.ResponsePackage;
const logical_frame = ResponseMessage.create({
"update_user_info": {
"user_id": "123",
"nickname": "0.1905146204509427",
"avatar": "2018111/15ae689464499cb5f24df8535acef0fb.gif",
"user_type": 1
}
});
//console.log('logical_frame',logical_frame);
const head_frame={
"msg_type": "update_user",
"msg_no": "xxxxx",
};
const plainPackage = ResponsePackage.create({
head_frame,
logical_frame
});
//console.log('plainPackage',plainPackage);
const bb = ResponsePackage.encode(plainPackage).finish();
console.log(ResponsePackage.decode(bb));
Run the code, it will output:
ResponsePackage {
head_frame: CommunicationFrame { msg_type: 'update_user', msg_no: 'xxxxx' },
logical_frame:
ResponseMessage {
update_user_info: User { user_id: '123', nickname: '0.1905146204509427' } } }