Skip to content

Commit

Permalink
Write default values to oneof field anyway
Browse files Browse the repository at this point in the history
  • Loading branch information
farwayer authored and mourner committed Jul 4, 2024
1 parent 1d724b1 commit 1bd1770
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ function getDefaultWriteTest(ctx, field) {
let code = ` if (obj.${field.name}`;

if (!field.repeated && (!type || !type._proto.fields)) {
if (def === undefined || def) {
if (def === undefined || def || field.oneof) {
code += ' != null';
}
if (def) {
Expand Down
9 changes: 9 additions & 0 deletions test/compile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,15 @@ test('sets oneof field name', () => {

assert.equal(data.value, 'float');
assert.equal(data[data.value], 1.5);

pbf = new Pbf();
writeEnvelope({
float: 0
}, pbf);
data = readEnvelope(new Pbf(pbf.finish()));

assert.equal(data.value, 'float');
assert.equal(data[data.value], 0);
});

test('handles jstype=JS_STRING', () => {
Expand Down

0 comments on commit 1bd1770

Please sign in to comment.