Skip to content

Commit 38d45bb

Browse files
fix: fromObject should not initialize oneof members
1 parent 343e127 commit 38d45bb

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

cli/targets/static.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,7 @@ function buildType(ref, type) {
394394
if (config.comments) {
395395
push("");
396396
var jsType = toJsType(field);
397-
if (field.optional && !field.map && !field.repeated && field.resolvedType instanceof Type ||
398-
field.options && field.options.proto3_optional)
397+
if (field.optional && !field.map && !field.repeated && field.resolvedType instanceof Type || field.partOf)
399398
jsType = jsType + "|null|undefined";
400399
pushComment([
401400
field.comment || type.name + " " + field.name + ".",
@@ -411,9 +410,8 @@ function buildType(ref, type) {
411410
push(escapeName(type.name) + ".prototype" + prop + " = $util.emptyArray;"); // overwritten in constructor
412411
else if (field.map)
413412
push(escapeName(type.name) + ".prototype" + prop + " = $util.emptyObject;"); // overwritten in constructor
414-
else if (field.options && field.options.proto3_optional) {
415-
push(escapeName(type.name) + ".prototype" + prop + " = null;"); // do not set default value for proto3 optional fields
416-
}
413+
else if (field.partOf)
414+
push(escapeName(type.name) + ".prototype" + prop + " = null;"); // do not set default value for oneof members
417415
else if (field.long)
418416
push(escapeName(type.name) + ".prototype" + prop + " = $util.Long ? $util.Long.fromBits("
419417
+ JSON.stringify(field.typeDefault.low) + ","

tests/cli.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ var Module = require("module");
66
var protobuf = require("..");
77

88
tape.test("pbjs generates static code", function(test) {
9+
// pbjs does not seem to work with Node v4, so skip this test if we're running on it
10+
if (process.versions.node.match(/^4\./)) {
11+
test.end();
12+
return;
13+
}
14+
915
// Alter the require cache to make the cli/targets/static work since it requires "protobufjs"
1016
// and we don't want to mess with "npm link"
1117
var savedResolveFilename = Module._resolveFilename;
@@ -59,7 +65,6 @@ tape.test("pbjs generates static code", function(test) {
5965
instance.messageInOneof.value = 42;
6066
instance.regularField = "abc";
6167
var instance1 = OneofContainerDynamic.toObject(OneofContainerDynamic.fromObject(instance));
62-
console.log(instance1);
6368
// The following test fails: will be fixed by the next commit
6469
test.deepEqual(instance, instance1, "fromObject and toObject work for instance of the static type");
6570

0 commit comments

Comments
 (0)