Skip to content

Commit f39bb00

Browse files
Handle repeated options properly
1 parent a48bd5b commit f39bb00

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

ext/descriptor/index.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -863,15 +863,15 @@ function toDescriptorType(type, resolvedType, delimited) {
863863
throw Error("illegal type: " + type);
864864
}
865865

866-
function fixDescriptorOptionsRecursive(obj, type) {
866+
function fromDescriptorOptionsRecursive(obj, type) {
867867
var val = {};
868868
for (var i = 0, field, key; i < type.fieldsArray.length; ++i) {
869869
if ((key = (field = type._fieldsArray[i]).name) === "uninterpretedOption") continue;
870870
if (!Object.prototype.hasOwnProperty.call(obj, key)) continue;
871871

872872
var newKey = underScore(key);
873873
if (field.resolvedType instanceof Type) {
874-
val[newKey] = fixDescriptorOptionsRecursive(obj[key], field.resolvedType);
874+
val[newKey] = fromDescriptorOptionsRecursive(obj[key], field.resolvedType);
875875
} else if(field.resolvedType instanceof Enum) {
876876
val[newKey] = field.resolvedType.valuesById[obj[key]];
877877
} else {
@@ -885,19 +885,24 @@ function fixDescriptorOptionsRecursive(obj, type) {
885885
function fromDescriptorOptions(options, type) {
886886
if (!options)
887887
return undefined;
888-
return fixDescriptorOptionsRecursive(type.toObject(options), type);
888+
return fromDescriptorOptionsRecursive(type.toObject(options), type);
889889
}
890890

891-
function camelCaseRecursive(obj) {
891+
function toDescriptorOptionsRecursive(obj, type) {
892892
var val = {};
893893
var keys = Object.keys(obj);
894894
for (var i = 0; i < keys.length; ++i) {
895895
var key = keys[i];
896896
var newKey = $protobuf.util.camelCase(key);
897-
if (typeof obj[key] !== "object") {
898-
val[newKey] = obj[key];
897+
if (!Object.prototype.hasOwnProperty.call(type.fields, newKey)) continue;
898+
var field = type.fields[newKey];
899+
if (field.resolvedType instanceof Type) {
900+
val[newKey] = toDescriptorOptionsRecursive(obj[key], field.resolvedType);
899901
} else {
900-
val[newKey] = camelCaseRecursive(obj[key]);
902+
val[newKey] = obj[key];
903+
}
904+
if (field.repeated && !Array.isArray(val[newKey])) {
905+
val[newKey] = [val[newKey]];
901906
}
902907
}
903908
return val;
@@ -907,7 +912,7 @@ function camelCaseRecursive(obj) {
907912
function toDescriptorOptions(options, type) {
908913
if (!options)
909914
return undefined;
910-
return type.fromObject(camelCaseRecursive(options));
915+
return type.fromObject(toDescriptorOptionsRecursive(options, type));
911916
}
912917

913918
// Calculates the shortest relative path from `from` to `to`.

tests/comp_import_extend.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ tape.test("extensions - proto2 roundtrip", function (test) {
3434
required string required = 2;
3535
repeated int32 packed = 3 [packed = true];
3636
repeated int32 unpacked = 4;
37+
optional int32 repeated_options = 5 [targets = TARGET_TYPE_SERVICE, targets = TARGET_TYPE_FILE];
3738
}
3839
`).root.resolveAll();
3940

0 commit comments

Comments
 (0)