Skip to content

Commit 8c5060f

Browse files
authored
fix: Parse empty repeated options (#2161)
1 parent 648b760 commit 8c5060f

3 files changed

Lines changed: 12 additions & 7 deletions

File tree

src/parse.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -765,13 +765,15 @@ function parse(source, root, options) {
765765
value = [];
766766
var lastValue;
767767
if (skip("[", true)) {
768-
do {
769-
lastValue = readValue(true);
770-
value.push(lastValue);
771-
} while (skip(",", true));
772-
skip("]");
773-
if (typeof lastValue !== "undefined") {
774-
setOption(parent, name + "." + token, lastValue);
768+
if (!skip("]", true)) {
769+
do {
770+
lastValue = readValue(true);
771+
value.push(lastValue);
772+
} while (skip(",", true));
773+
skip("]");
774+
if (typeof lastValue !== "undefined") {
775+
setOption(parent, name + "." + token, lastValue);
776+
}
775777
}
776778
}
777779
} else {

tests/comp_options-parse.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ tape.test("Options", function (test) {
134134
{
135135
"(method_rep_msg)": {
136136
value: 1,
137+
empty_repeated: [],
137138
nested: {nested: {value: "x"}},
138139
rep_nested: [{value: "y"}, {value: "z"}],
139140
rep_value: 3
@@ -149,6 +150,7 @@ tape.test("Options", function (test) {
149150
];
150151

151152
test.same(TestOptionsRpc.parsedOptions, expectedParsedOptions, "should correctly parse all nested message options");
153+
test.equal(TestOptionsRpc.options["(method_rep_msg).empty_repeated"], undefined, "should not set a last value for empty repeated options");
152154
var jsonTestOptionsRpc = TestOptionsRpc.toJSON();
153155
test.same(jsonTestOptionsRpc.parsedOptions, expectedParsedOptions, "should correctly store all nested method options in JSON");
154156
var rootFromJson = protobuf.Root.fromJSON(root.toJSON());

tests/data/options_test.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ service TestOptionsService {
119119
rpc TestOptionsRpc(Msg) returns (Msg) {
120120
option (method_rep_msg) = {
121121
value: 1
122+
empty_repeated: []
122123
nested {
123124
nested {
124125
value: "x"

0 commit comments

Comments
 (0)