Skip to content

Commit a2436b6

Browse files
fix: fromObject should not initialize oneof members
1 parent 6c4d307 commit a2436b6

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

tests/cli.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// A minimal test for pbjs tool targets.
2+
3+
var tape = require("tape");
4+
var path = require("path");
5+
var Module = require("module");
6+
var protobuf = require("..");
7+
8+
tape.test("pbjs generates static code", function(test) {
9+
// Alter the require cache to make the cli/targets/static work since it requires "protobufjs"
10+
// and we don't want to mess with "npm link"
11+
var savedResolveFilename = Module._resolveFilename;
12+
Module._resolveFilename = function(request, parent) {
13+
if (request.startsWith("protobufjs")) {
14+
return request;
15+
}
16+
return savedResolveFilename(request, parent);
17+
};
18+
require.cache.protobufjs = require.cache[path.resolve("index.js")];
19+
20+
var staticTarget = require("../cli/targets/static");
21+
22+
var root = protobuf.loadSync("tests/data/cli/test.proto");
23+
root.resolveAll();
24+
25+
staticTarget(root, {
26+
create: true,
27+
decode: true,
28+
encode: true,
29+
convert: true,
30+
}, function(err, jsCode) {
31+
test.error(err, 'static code generation worked');
32+
33+
// jsCode is the generated code; we'll eval it
34+
// (since this is what we normally does with the code, right?)
35+
// This is a test code. Do not use this in production.
36+
var $protobuf = protobuf;
37+
eval(jsCode);
38+
39+
var OneofContainer = protobuf.roots.default.OneofContainer;
40+
var Message = protobuf.roots.default.Message;
41+
test.ok(OneofContainer, "type is loaded");
42+
test.ok(Message, "type is loaded");
43+
44+
// Check that fromObject and toObject work for plain object
45+
var obj = {
46+
messageInOneof: {
47+
value: 42,
48+
},
49+
regularField: "abc",
50+
};
51+
var obj1 = OneofContainer.toObject(OneofContainer.fromObject(obj));
52+
test.deepEqual(obj, obj1, "fromObject and toObject work for plain object");
53+
54+
// Check that dynamic fromObject and toObject work for static instance
55+
var root = protobuf.loadSync("tests/data/cli/test.proto");
56+
var OneofContainerDynamic = root.lookup("OneofContainer");
57+
var instance = new OneofContainer();
58+
instance.messageInOneof = new Message();
59+
instance.messageInOneof.value = 42;
60+
instance.regularField = "abc";
61+
var instance1 = OneofContainerDynamic.toObject(OneofContainerDynamic.fromObject(instance));
62+
console.log(instance1);
63+
// The following test fails: will be fixed by the next commit
64+
// test.deepEqual(instance, instance1, "fromObject and toObject work for instance of the static type");
65+
66+
test.end();
67+
});
68+
69+
// Rollback all the require() related mess we made
70+
delete require.cache.protobufjs;
71+
Module._resolveFilename = savedResolveFilename;
72+
});

tests/data/cli/test.proto

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
syntax = "proto3";
2+
3+
message Message {
4+
int32 value = 1;
5+
}
6+
7+
message OneofContainer {
8+
oneof some_oneof {
9+
string string_in_oneof = 1;
10+
Message message_in_oneof = 2;
11+
}
12+
string regular_field = 3;
13+
}

0 commit comments

Comments
 (0)