Skip to content

Commit 3d29969

Browse files
fix: make parsedOptions appear in method JSON representation (#1506)
1 parent ade223d commit 3d29969

File tree

3 files changed

+84
-4
lines changed

3 files changed

+84
-4
lines changed

src/method.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ var util = require("./util");
2020
* @param {boolean|Object.<string,*>} [responseStream] Whether the response is streamed
2121
* @param {Object.<string,*>} [options] Declared options
2222
* @param {string} [comment] The comment for this method
23+
* @param {Object.<string,*>} [parsedOptions] Declared options, properly parsed into an object
2324
*/
24-
function Method(name, type, requestType, responseType, requestStream, responseStream, options, comment) {
25+
function Method(name, type, requestType, responseType, requestStream, responseStream, options, comment, parsedOptions) {
2526

2627
/* istanbul ignore next */
2728
if (util.isObject(requestStream)) {
@@ -93,6 +94,11 @@ function Method(name, type, requestType, responseType, requestStream, responseSt
9394
* @type {string|null}
9495
*/
9596
this.comment = comment;
97+
98+
/**
99+
* Options properly parsed into an object
100+
*/
101+
this.parsedOptions = parsedOptions;
96102
}
97103

98104
/**
@@ -104,6 +110,8 @@ function Method(name, type, requestType, responseType, requestStream, responseSt
104110
* @property {boolean} [requestStream=false] Whether requests are streamed
105111
* @property {boolean} [responseStream=false] Whether responses are streamed
106112
* @property {Object.<string,*>} [options] Method options
113+
* @property {string} comment Method comments
114+
* @property {Object.<string,*>} [parsedOptions] Method options properly parsed into an object
107115
*/
108116

109117
/**
@@ -114,7 +122,7 @@ function Method(name, type, requestType, responseType, requestStream, responseSt
114122
* @throws {TypeError} If arguments are invalid
115123
*/
116124
Method.fromJSON = function fromJSON(name, json) {
117-
return new Method(name, json.type, json.requestType, json.responseType, json.requestStream, json.responseStream, json.options, json.comment);
125+
return new Method(name, json.type, json.requestType, json.responseType, json.requestStream, json.responseStream, json.options, json.comment, json.parsedOptions);
118126
};
119127

120128
/**
@@ -131,7 +139,8 @@ Method.prototype.toJSON = function toJSON(toJSONOptions) {
131139
"responseType" , this.responseType,
132140
"responseStream" , this.responseStream,
133141
"options" , this.options,
134-
"comment" , keepComments ? this.comment : undefined
142+
"comment" , keepComments ? this.comment : undefined,
143+
"parsedOptions" , this.parsedOptions,
135144
]);
136145
};
137146

tests/comp_options-parse.js

+39
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,44 @@ tape.test("Options", function (test) {
117117
test.end();
118118
});
119119

120+
test.test(test.name + " - rpc options (Nested)", function (test) {
121+
var TestOptionsRpc = root.lookup("TestOptionsRpc");
122+
test.equal(TestOptionsRpc.options["(method_rep_msg).value"], 1, "should merge repeated options messages");
123+
test.equal(TestOptionsRpc.options["(method_rep_msg).rep_value"], 3, "should parse in any order");
124+
test.equal(TestOptionsRpc.options["(method_rep_msg).nested.nested.value"], "x", "should correctly parse nested field options");
125+
test.equal(TestOptionsRpc.options["(method_rep_msg).rep_nested.value"], "z", "should take second repeated nested options");
126+
test.equal(TestOptionsRpc.options["(method_rep_msg).nested.value"], "w", "should merge nested options");
127+
128+
test.equal(TestOptionsRpc.options["(method_single_msg).nested.value"], "x", "should correctly parse nested property name");
129+
test.equal(TestOptionsRpc.options["(method_single_msg).rep_nested.value"], "y", "should take second repeated nested options");
130+
test.equal(TestOptionsRpc.options["(method_single_msg).rep_nested.nested.nested.value"], "y", "should correctly parse several nesting levels");
131+
132+
var expectedParsedOptions = [
133+
{
134+
"(method_rep_msg)": {
135+
value: 1,
136+
nested: {nested: {value: "x"}},
137+
rep_nested: [{value: "y"}, {value: "z"}],
138+
rep_value: 3
139+
}
140+
},
141+
{"(method_rep_msg)": {nested: {value: "w"}}},
142+
{
143+
"(method_single_msg)": {
144+
nested: {value: "x"},
145+
rep_nested: [{value: "x", nested: {nested: {value: "y"}}}, {value: "y"}]
146+
}
147+
}
148+
];
149+
150+
test.same(TestOptionsRpc.parsedOptions, expectedParsedOptions, "should correctly parse all nested message options");
151+
var jsonTestOptionsRpc = TestOptionsRpc.toJSON();
152+
test.same(jsonTestOptionsRpc.parsedOptions, expectedParsedOptions, "should correctly store all nested method options in JSON");
153+
var rootFromJson = protobuf.Root.fromJSON(root.toJSON());
154+
var TestOptionsRpcFromJson = rootFromJson.lookup("TestOptionsRpc");
155+
test.same(TestOptionsRpcFromJson.parsedOptions, expectedParsedOptions, "should correctly read all nested method options from JSON");
156+
test.end();
157+
});
158+
120159
test.end();
121160
});

tests/data/options_test.proto

+33-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ extend google.protobuf.MessageOptions {
5353
Msg mo_single_msg = 50003;
5454
}
5555

56+
extend google.protobuf.MethodOptions {
57+
repeated Msg method_rep_msg = 50002;
58+
Msg method_single_msg = 50003;
59+
}
60+
5661
message TestFieldOptionsMsg {
5762
string field1 = 1 [(fo_rep_msg) = {value: 1 rep_value: 2 rep_value: 3}, (fo_rep_msg) = {value: 4 rep_value: 5 rep_value: 6}];
5863
string field2 = 2 [(fo_single_msg).value = 7, (fo_single_msg).rep_value = 8, (fo_single_msg).rep_value = 9];
@@ -80,7 +85,6 @@ message TestFieldOptionsNested {
8085
string field3 = 3 [(fo_single_msg).nested = {value: "x" nested {nested{value: "y"}}}];
8186
}
8287

83-
8488
message TestMessageOptionsNested {
8589
option (mo_rep_msg) = {
8690
value: 1
@@ -106,3 +110,31 @@ message TestMessageOptionsNested {
106110
option (mo_single_msg).rep_nested = {value : "x" nested {nested{value: "y"}}};
107111
option (mo_single_msg).rep_nested = {value : "y"};
108112
}
113+
114+
service TestOptionsService {
115+
rpc TestOptionsRpc(Msg) returns (Msg) {
116+
option (method_rep_msg) = {
117+
value: 1
118+
nested {
119+
nested {
120+
value: "x"
121+
}
122+
}
123+
rep_nested {
124+
value: "y"
125+
}
126+
rep_nested {
127+
value: "z"
128+
}
129+
rep_value: 3
130+
};
131+
option (method_rep_msg) = {
132+
nested {
133+
value: "w"
134+
}
135+
};
136+
option (method_single_msg).nested.value = "x";
137+
option (method_single_msg).rep_nested = {value : "x" nested {nested{value: "y"}}};
138+
option (method_single_msg).rep_nested = {value : "y"};
139+
}
140+
}

0 commit comments

Comments
 (0)