protobuf.js icon indicating copy to clipboard operation
protobuf.js copied to clipboard

Can't properly convert structure to proto message with fromObject method.

Open KurilovDmitry opened this issue 3 years ago • 0 comments

protobuf.js version: 7.0.0

Hello, all! Try to convert JS structure in proto message. Problem is that nested structure is always empty list. In example expected that output 3 will be like TestCollection { collections: [{name: 'Test Sub Collection', collections: []}], name: 'Test Collection' }

message TestCollection {
  string name = 1;                                                                                         // Collection Name
  repeated TestCollection collections = 2 [json_name = "collectionsList"];  // List of nested collections
}
const protobuf = require('protobufjs');
const model_path = path.join(process.cwd(), 'components/integration-test/model/test_result.proto');
var root = protobuf.loadSync(model_path);
const TestCol = root.lookupType("TestCollection");

let col = new result.TestCollection()
col.setName("Test Collection")
let subCol = new result.TestCollection()
subColEx.setName("Test Sub Collection")
col.addCollections(subCol)

console.log(col)                                                     // 1
console.log(col.toObject())                                    // 2

message = TestCol.fromObject(col.toObject());
console.log(message)                                            // 3
// 1
{
  wrappers_: { '2': [ [Object] ] },
  messageId_: undefined,
  arrayIndexOffset_: -1,
  array: [ 'Test Name 1', [ [Array] ] ],
  pivot_: 1.7976931348623157e+308,
  convertedPrimitiveFields_: {}
}

// 2
{
  name: 'Test Collection',
  collectionsList: [ { name: 'Test Sub Collection', collectionsList: [] } ]
}

// 3
TestCollection { collections: [], name: 'Test Collection' }

KurilovDmitry avatar Jul 14 '22 15:07 KurilovDmitry