Skip to content

Commit 228c882

Browse files
committed
New: Optional fields handle null just like undefined regardless of type see #709
1 parent fe91765 commit 228c882

File tree

7 files changed

+127
-127
lines changed

7 files changed

+127
-127
lines changed

src/encoder.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function encoder(mtype) {
110110
else if (field.bytes || field.resolvedType && !(field.resolvedType instanceof Enum)) gen
111111
("if(%s&&m.hasOwnProperty(%j))", ref, field.name);
112112
else gen
113-
("if(%s!==undefined&&m.hasOwnProperty(%j))", ref, field.name);
113+
("if(%s!==undefined&&%s!==null&&m.hasOwnProperty(%j))", ref, ref, field.name);
114114

115115
}
116116

tests/data/comments.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ $root.Test1 = (function() {
6666
Test1.encode = function encode(message, writer) {
6767
if (!writer)
6868
writer = $Writer.create();
69-
if (message.field1 !== undefined && message.hasOwnProperty("field1"))
69+
if (message.field1 !== undefined && message.field1 !== null && message.hasOwnProperty("field1"))
7070
writer.uint32(/* id 1, wireType 2 =*/10).string(message.field1);
71-
if (message.field2 !== undefined && message.hasOwnProperty("field2"))
71+
if (message.field2 !== undefined && message.field2 !== null && message.hasOwnProperty("field2"))
7272
writer.uint32(/* id 2, wireType 0 =*/16).uint32(message.field2);
73-
if (message.field3 !== undefined && message.hasOwnProperty("field3"))
73+
if (message.field3 !== undefined && message.field3 !== null && message.hasOwnProperty("field3"))
7474
writer.uint32(/* id 3, wireType 0 =*/24).bool(message.field3);
7575
return writer;
7676
};

tests/data/convert.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ $root.Message = (function() {
104104
Message.encode = function encode(message, writer) {
105105
if (!writer)
106106
writer = $Writer.create();
107-
if (message.stringVal !== undefined && message.hasOwnProperty("stringVal"))
107+
if (message.stringVal !== undefined && message.stringVal !== null && message.hasOwnProperty("stringVal"))
108108
writer.uint32(/* id 1, wireType 2 =*/10).string(message.stringVal);
109109
if (message.stringRepeated !== undefined && message.hasOwnProperty("stringRepeated"))
110110
for (var i = 0; i < message.stringRepeated.length; ++i)
@@ -122,7 +122,7 @@ $root.Message = (function() {
122122
if (message.bytesRepeated !== undefined && message.hasOwnProperty("bytesRepeated"))
123123
for (var i = 0; i < message.bytesRepeated.length; ++i)
124124
writer.uint32(/* id 6, wireType 2 =*/50).bytes(message.bytesRepeated[i]);
125-
if (message.enumVal !== undefined && message.hasOwnProperty("enumVal"))
125+
if (message.enumVal !== undefined && message.enumVal !== null && message.hasOwnProperty("enumVal"))
126126
writer.uint32(/* id 7, wireType 0 =*/56).uint32(message.enumVal);
127127
if (message.enumRepeated && message.enumRepeated.length && message.hasOwnProperty("enumRepeated")) {
128128
writer.uint32(/* id 8, wireType 2 =*/66).fork();

tests/data/mapbox/vector_tile.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -302,19 +302,19 @@ $root.vector_tile = (function() {
302302
Value.encode = function encode(message, writer) {
303303
if (!writer)
304304
writer = $Writer.create();
305-
if (message.stringValue !== undefined && message.hasOwnProperty("stringValue"))
305+
if (message.stringValue !== undefined && message.stringValue !== null && message.hasOwnProperty("stringValue"))
306306
writer.uint32(/* id 1, wireType 2 =*/10).string(message.stringValue);
307-
if (message.floatValue !== undefined && message.hasOwnProperty("floatValue"))
307+
if (message.floatValue !== undefined && message.floatValue !== null && message.hasOwnProperty("floatValue"))
308308
writer.uint32(/* id 2, wireType 5 =*/21).float(message.floatValue);
309-
if (message.doubleValue !== undefined && message.hasOwnProperty("doubleValue"))
309+
if (message.doubleValue !== undefined && message.doubleValue !== null && message.hasOwnProperty("doubleValue"))
310310
writer.uint32(/* id 3, wireType 1 =*/25).double(message.doubleValue);
311311
if (message.intValue !== undefined && message.intValue !== null && message.hasOwnProperty("intValue"))
312312
writer.uint32(/* id 4, wireType 0 =*/32).int64(message.intValue);
313313
if (message.uintValue !== undefined && message.uintValue !== null && message.hasOwnProperty("uintValue"))
314314
writer.uint32(/* id 5, wireType 0 =*/40).uint64(message.uintValue);
315315
if (message.sintValue !== undefined && message.sintValue !== null && message.hasOwnProperty("sintValue"))
316316
writer.uint32(/* id 6, wireType 0 =*/48).sint64(message.sintValue);
317-
if (message.boolValue !== undefined && message.hasOwnProperty("boolValue"))
317+
if (message.boolValue !== undefined && message.boolValue !== null && message.hasOwnProperty("boolValue"))
318318
writer.uint32(/* id 7, wireType 0 =*/56).bool(message.boolValue);
319319
return writer;
320320
};
@@ -620,7 +620,7 @@ $root.vector_tile = (function() {
620620
writer.uint32(message.tags[i]);
621621
writer.ldelim();
622622
}
623-
if (message.type !== undefined && message.hasOwnProperty("type"))
623+
if (message.type !== undefined && message.type !== null && message.hasOwnProperty("type"))
624624
writer.uint32(/* id 3, wireType 0 =*/24).uint32(message.type);
625625
if (message.geometry && message.geometry.length && message.hasOwnProperty("geometry")) {
626626
writer.uint32(/* id 4, wireType 2 =*/34).fork();
@@ -949,7 +949,7 @@ $root.vector_tile = (function() {
949949
if (message.values !== undefined && message.hasOwnProperty("values"))
950950
for (var i = 0; i < message.values.length; ++i)
951951
$types[4].encode(message.values[i], writer.uint32(/* id 4, wireType 2 =*/34).fork()).ldelim();
952-
if (message.extent !== undefined && message.hasOwnProperty("extent"))
952+
if (message.extent !== undefined && message.extent !== null && message.hasOwnProperty("extent"))
953953
writer.uint32(/* id 5, wireType 0 =*/40).uint32(message.extent);
954954
writer.uint32(/* id 15, wireType 0 =*/120).uint32(message.version);
955955
return writer;

tests/data/package.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -157,26 +157,26 @@ $root.Package = (function() {
157157
Package.encode = function encode(message, writer) {
158158
if (!writer)
159159
writer = $Writer.create();
160-
if (message.name !== undefined && message.hasOwnProperty("name"))
160+
if (message.name !== undefined && message.name !== null && message.hasOwnProperty("name"))
161161
writer.uint32(/* id 1, wireType 2 =*/10).string(message.name);
162-
if (message.version !== undefined && message.hasOwnProperty("version"))
162+
if (message.version !== undefined && message.version !== null && message.hasOwnProperty("version"))
163163
writer.uint32(/* id 2, wireType 2 =*/18).string(message.version);
164-
if (message.description !== undefined && message.hasOwnProperty("description"))
164+
if (message.description !== undefined && message.description !== null && message.hasOwnProperty("description"))
165165
writer.uint32(/* id 3, wireType 2 =*/26).string(message.description);
166-
if (message.author !== undefined && message.hasOwnProperty("author"))
166+
if (message.author !== undefined && message.author !== null && message.hasOwnProperty("author"))
167167
writer.uint32(/* id 4, wireType 2 =*/34).string(message.author);
168-
if (message.license !== undefined && message.hasOwnProperty("license"))
168+
if (message.license !== undefined && message.license !== null && message.hasOwnProperty("license"))
169169
writer.uint32(/* id 5, wireType 2 =*/42).string(message.license);
170170
if (message.repository && message.hasOwnProperty("repository"))
171171
$types[6].encode(message.repository, writer.uint32(/* id 6, wireType 2 =*/50).fork()).ldelim();
172-
if (message.bugs !== undefined && message.hasOwnProperty("bugs"))
172+
if (message.bugs !== undefined && message.bugs !== null && message.hasOwnProperty("bugs"))
173173
writer.uint32(/* id 7, wireType 2 =*/58).string(message.bugs);
174-
if (message.homepage !== undefined && message.hasOwnProperty("homepage"))
174+
if (message.homepage !== undefined && message.homepage !== null && message.hasOwnProperty("homepage"))
175175
writer.uint32(/* id 8, wireType 2 =*/66).string(message.homepage);
176176
if (message.keywords !== undefined && message.hasOwnProperty("keywords"))
177177
for (var i = 0; i < message.keywords.length; ++i)
178178
writer.uint32(/* id 9, wireType 2 =*/74).string(message.keywords[i]);
179-
if (message.main !== undefined && message.hasOwnProperty("main"))
179+
if (message.main !== undefined && message.main !== null && message.hasOwnProperty("main"))
180180
writer.uint32(/* id 10, wireType 2 =*/82).string(message.main);
181181
if (message.bin && message.hasOwnProperty("bin"))
182182
for (var keys = Object.keys(message.bin), i = 0; i < keys.length; ++i)
@@ -193,12 +193,12 @@ $root.Package = (function() {
193193
if (message.devDependencies && message.hasOwnProperty("devDependencies"))
194194
for (var keys = Object.keys(message.devDependencies), i = 0; i < keys.length; ++i)
195195
writer.uint32(/* id 15, wireType 2 =*/122).fork().uint32(/* id 1, wireType 2 =*/10).string(keys[i]).uint32(/* id 2, wireType 2 =*/18).string(message.devDependencies[keys[i]]).ldelim();
196-
if (message.types !== undefined && message.hasOwnProperty("types"))
196+
if (message.types !== undefined && message.types !== null && message.hasOwnProperty("types"))
197197
writer.uint32(/* id 17, wireType 2 =*/138).string(message.types);
198198
if (message.cliDependencies !== undefined && message.hasOwnProperty("cliDependencies"))
199199
for (var i = 0; i < message.cliDependencies.length; ++i)
200200
writer.uint32(/* id 18, wireType 2 =*/146).string(message.cliDependencies[i]);
201-
if (message.versionScheme !== undefined && message.hasOwnProperty("versionScheme"))
201+
if (message.versionScheme !== undefined && message.versionScheme !== null && message.hasOwnProperty("versionScheme"))
202202
writer.uint32(/* id 19, wireType 2 =*/154).string(message.versionScheme);
203203
return writer;
204204
};
@@ -682,9 +682,9 @@ $root.Package = (function() {
682682
Repository.encode = function encode(message, writer) {
683683
if (!writer)
684684
writer = $Writer.create();
685-
if (message.type !== undefined && message.hasOwnProperty("type"))
685+
if (message.type !== undefined && message.type !== null && message.hasOwnProperty("type"))
686686
writer.uint32(/* id 1, wireType 2 =*/10).string(message.type);
687-
if (message.url !== undefined && message.hasOwnProperty("url"))
687+
if (message.url !== undefined && message.url !== null && message.hasOwnProperty("url"))
688688
writer.uint32(/* id 2, wireType 2 =*/18).string(message.url);
689689
return writer;
690690
};

tests/data/rpc.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ $root.MyRequest = (function() {
108108
MyRequest.encode = function encode(message, writer) {
109109
if (!writer)
110110
writer = $Writer.create();
111-
if (message.path !== undefined && message.hasOwnProperty("path"))
111+
if (message.path !== undefined && message.path !== null && message.hasOwnProperty("path"))
112112
writer.uint32(/* id 1, wireType 2 =*/10).string(message.path);
113113
return writer;
114114
};
@@ -274,7 +274,7 @@ $root.MyResponse = (function() {
274274
MyResponse.encode = function encode(message, writer) {
275275
if (!writer)
276276
writer = $Writer.create();
277-
if (message.status !== undefined && message.hasOwnProperty("status"))
277+
if (message.status !== undefined && message.status !== null && message.hasOwnProperty("status"))
278278
writer.uint32(/* id 2, wireType 0 =*/16).int32(message.status);
279279
return writer;
280280
};

0 commit comments

Comments
 (0)