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

fromObject force assign unset array field with empty array

Open summer-ji-eng opened this issue 4 years ago • 1 comments

protobuf.js version: 6.11.2

when transfer a JSON payload to message object using fromObject, message force assign unset array field with empty array. expected unset field no change.

const protobuf = require('protobufjs');

const root = protobuf.loadSync('test.proto');
const Type = root.lookupType('TestArray');
const request = {name: 'test'};
const message = Type.fromObject(request)

console.log(message) // --> output: TestArray { allowed: [], name: 'test' } --> expected: TestArray{name:'test}
console.log(message.name === request.name)// --> output:true
console.log(request.allowed === undefined)// --> output:true
console.log(message.allowed.length === 0)// --> output: true

--- test.proto

message TestArray {
  string name = 1;
  repeated Allowed allowed = 2;
}

message Allowed {
  optional string content = 1;
}

summer-ji-eng avatar Sep 17 '21 19:09 summer-ji-eng

Did you solve this problem? I found fromObject will push data to prototype like this image I think i could delete it to solve this problem

chunbin1 avatar Jul 01 '22 07:07 chunbin1