Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: proto3 optional scalars should default to null in reflection API #1693

Merged
merged 1 commit into from Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/field.js
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
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();
});