Skip to content

Commit d9144de

Browse files
authored
fix: proto3 optional scalars should default to null in reflection API (#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).
1 parent e33a84a commit d9144de

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

src/field.js

+3
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ Field.prototype.resolve = function resolve() {
270270
this.typeDefault = null;
271271
else // instanceof Enum
272272
this.typeDefault = this.resolvedType.values[Object.keys(this.resolvedType.values)[0]]; // first defined
273+
} else if (this.options && this.options.proto3_optional) {
274+
// proto3 scalar value marked optional; should default to null
275+
this.typeDefault = null;
273276
}
274277

275278
// use explicitly set default value if present

tests/comp_optional.js

+4
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,9 @@ tape.test("proto3 optional", function(test) {
2222
test.equal(Message.oneofs._optionalInt32.name, '_optionalInt32');
2323
test.deepEqual(Message.oneofs._optionalInt32.oneof, ['optionalInt32']);
2424

25+
var m = Message.create({});
26+
test.strictEqual(m.regularInt32, 0);
27+
test.strictEqual(m.optionalInt32, null);
28+
2529
test.end();
2630
});

0 commit comments

Comments
 (0)