10000 fromObject breaks oneof content · Issue #1596 · protobufjs/protobuf.js · GitHub
[go: up one dir, main page]

Skip to content
fromObject breaks oneof content #1596
Closed
@alexander-fenster

Description

@alexander-fenster

protobuf.js v6.10.2

In a kind of special case when oneof contains both a primitive field and a message, fromObject that is run on the instance of the static object would break the oneof by populating the primitive field with its default value.

Example:

test.proto

syntax = "proto3";

message TargetSpend {
  int32 micros = 1;
}

message OneofContainer {
  oneof campaign_bidding_strategy {
    string bidding_strategy = 1;
    TargetSpend target_spend = 2;
  }
}

repro.js

const protobuf = require('protobufjs');
const protos = require('./test.js');

const root = protobuf.Root.fromJSON(require('./test.json'));
const OneofContainer = root.lookup('OneofContainer');

const request = new protos.OneofContainer();
const targetSpend = new protos.TargetSpend();
targetSpend.micros = 1234;
request.targetSpend = targetSpend;

console.log(request);

const object = OneofContainer.fromObject(request);

console.log(object);

Commands to reproduce:

echo {} > package.json
npm install protobufjs
npx pbjs -t static-module -o test.js test.proto
npx pbjs -t json -o test.json test.proto
node repro.js

Result:

OneofContainer { targetSpend: TargetSpend { micros: 1234 } }    // before .fromObject
OneofContainer {
  biddingStrategy: '',
  targetSpend: TargetSpend { micros: 1234 }
}   // after .fromObject - the oneof is broken

Note that it work properly when fromObject is applied to a plain object:

const request1 = {
  targetSpend: {
    micros: 1234,
  }
};
const object1 = OneofContainer.fromObject(request1);
console.log(object1); // works

(I'm going to look into this, filing an issue just in case)

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0