Skip to content

Commit df91652

Browse files
authored
fix: Preserve null conversion behavior for fieldless messages (#2312)
1 parent 78a9576 commit df91652

4 files changed

Lines changed: 10 additions & 51 deletions

File tree

src/converter.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,15 @@ converter.fromObject = function fromObject(mtype) {
107107
var fields = mtype.fieldsArray;
108108
var gen = util.codegen(["d", "n"], mtype.name + "$fromObject")
109109
("if(d instanceof this.ctor)")
110-
("return d")
110+
("return d");
111+
if (!fields.length) return gen
112+
("return new this.ctor");
113+
gen
111114
("if(!util.isObject(d))")
112115
("throw TypeError(%j)", mtype.fullName + ": object expected")
113116
("if(n===undefined)n=0")
114117
("if(n>util.recursionLimit)")
115118
("throw Error(\"maximum nesting depth exceeded\")");
116-
if (!fields.length) return gen
117-
("return new this.ctor");
118119
gen
119120
("var m=new this.ctor");
120121
for (var i = 0; i < fields.length; ++i) {

tests/api_type.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,17 @@ tape.test("object conversion rejects null message values", function(test) {
327327
key: { type: "string", id: 1 },
328328
value: { type: "string", id: 2 }
329329
}
330+
},
331+
Empty: {
332+
fields: {}
330333
}
331334
}
332335
});
333336
var Document = root.lookupType("Document");
334337
var Metadata = root.lookupType("Metadata");
338+
var Empty = root.lookupType("Empty");
339+
340+
test.ok(Empty.fromObject(null) instanceof Empty.ctor, "should allow null top-level fieldless messages");
335341

336342
test.throws(function() {
337343
Metadata.fromObject(null);

tests/data/comments.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,6 @@ $root.Test2 = (function() {
430430
Test2.fromObject = function fromObject(object, long) {
431431
if (object instanceof $root.Test2)
432432
return object;
433-
if (!$util.isObject(object))
434-
throw TypeError(".Test2: object expected");
435-
if (long === undefined)
436-
long = 0;
437-
if (long > $util.recursionLimit)
438-
throw Error("maximum nesting depth exceeded");
439433
return new $root.Test2();
440434
};
441435

tests/data/test.js

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,6 @@ $root.jspb = (function() {
171171
Empty.fromObject = function fromObject(object, long) {
172172
if (object instanceof $root.jspb.test.Empty)
173173
return object;
174-
if (!$util.isObject(object))
175-
throw TypeError(".jspb.test.Empty: object expected");
176-
if (long === undefined)
177-
long = 0;
178-
if (long > $util.recursionLimit)
179-
throw Error("maximum nesting depth exceeded");
180174
return new $root.jspb.test.Empty();
181175
};
182176

@@ -3129,12 +3123,6 @@ $root.jspb = (function() {
31293123
OuterMessage.fromObject = function fromObject(object, long) {
31303124
if (object instanceof $root.jspb.test.OuterMessage)
31313125
return object;
3132-
if (!$util.isObject(object))
3133-
throw TypeError(".jspb.test.OuterMessage: object expected");
3134-
if (long === undefined)
3135-
long = 0;
3136-
if (long > $util.recursionLimit)
3137-
throw Error("maximum nesting depth exceeded");
31383126
return new $root.jspb.test.OuterMessage();
31393127
};
31403128

@@ -3778,12 +3766,6 @@ $root.jspb = (function() {
37783766
IndirectExtension.fromObject = function fromObject(object, long) {
37793767
if (object instanceof $root.jspb.test.IndirectExtension)
37803768
return object;
3781-
if (!$util.isObject(object))
3782-
throw TypeError(".jspb.test.IndirectExtension: object expected");
3783-
if (long === undefined)
3784-
long = 0;
3785-
if (long > $util.recursionLimit)
3786-
throw Error("maximum nesting depth exceeded");
37873769
return new $root.jspb.test.IndirectExtension();
37883770
};
37893771

@@ -7794,12 +7776,6 @@ $root.jspb = (function() {
77947776
TestReservedNamesExtension.fromObject = function fromObject(object, long) {
77957777
if (object instanceof $root.jspb.test.TestReservedNamesExtension)
77967778
return object;
7797-
if (!$util.isObject(object))
7798-
throw TypeError(".jspb.test.TestReservedNamesExtension: object expected");
7799-
if (long === undefined)
7800-
long = 0;
7801-
if (long > $util.recursionLimit)
7802-
throw Error("maximum nesting depth exceeded");
78037779
return new $root.jspb.test.TestReservedNamesExtension();
78047780
};
78057781

@@ -10039,12 +10015,6 @@ $root.jspb = (function() {
1003910015
Deeply.fromObject = function fromObject(object, long) {
1004010016
if (object instanceof $root.jspb.test.Deeply)
1004110017
return object;
10042-
if (!$util.isObject(object))
10043-
throw TypeError(".jspb.test.Deeply: object expected");
10044-
if (long === undefined)
10045-
long = 0;
10046-
if (long > $util.recursionLimit)
10047-
throw Error("maximum nesting depth exceeded");
1004810018
return new $root.jspb.test.Deeply();
1004910019
};
1005010020

@@ -10231,12 +10201,6 @@ $root.jspb = (function() {
1023110201
Nested.fromObject = function fromObject(object, long) {
1023210202
if (object instanceof $root.jspb.test.Deeply.Nested)
1023310203
return object;
10234-
if (!$util.isObject(object))
10235-
throw TypeError(".jspb.test.Deeply.Nested: object expected");
10236-
if (long === undefined)
10237-
long = 0;
10238-
if (long > $util.recursionLimit)
10239-
throw Error("maximum nesting depth exceeded");
1024010204
return new $root.jspb.test.Deeply.Nested();
1024110205
};
1024210206

@@ -21958,12 +21922,6 @@ $root.google = (function() {
2195821922
VisibilityFeature.fromObject = function fromObject(object, long) {
2195921923
if (object instanceof $root.google.protobuf.FeatureSet.VisibilityFeature)
2196021924
return object;
21961-
if (!$util.isObject(object))
21962-
throw TypeError(".google.protobuf.FeatureSet.VisibilityFeature: object expected");
21963-
if (long === undefined)
21964-
long = 0;
21965-
if (long > $util.recursionLimit)
21966-
throw Error("maximum nesting depth exceeded");
2196721925
return new $root.google.protobuf.FeatureSet.VisibilityFeature();
2196821926
};
2196921927

0 commit comments

Comments
 (0)