Skip to content

Commit

Permalink
fix: proto3 optional scalars should default to null in reflection API (
Browse files Browse the repository at this point in the history
…#1693)

#1584 made proto3 optional
scalars default to null when using static/static-module, but the old
behaviour remained when using reflection (eg json-module).
  • Loading branch information
dae authored Jul 8, 2022
1 parent e33a84a commit d9144de
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ Field.prototype.resolve = function resolve() {
this.typeDefault = null;
else // instanceof Enum
this.typeDefault = this.resolvedType.values[Object.keys(this.resolvedType.values)[0]]; // first defined
} else if (this.options && this.options.proto3_optional) {
// proto3 scalar value marked optional; should default to null
this.typeDefault = null;
}

// use explicitly set default value if present
Expand Down
4 changes: 4 additions & 0 deletions tests/comp_optional.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ tape.test("proto3 optional", function(test) {
test.equal(Message.oneofs._optionalInt32.name, '_optionalInt32');
test.deepEqual(Message.oneofs._optionalInt32.oneof, ['optionalInt32']);

var m = Message.create({});
test.strictEqual(m.regularInt32, 0);
test.strictEqual(m.optionalInt32, null);

test.end();
});

0 comments on commit d9144de

Please sign in to comment.