Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Handle repeated options properly
  • Loading branch information
mkruskal-google committed May 27, 2025
commit f39bb00221b532d60be487e78fb4fbe8a59b85c0
21 changes: 13 additions & 8 deletions ext/descriptor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -863,15 +863,15 @@ function toDescriptorType(type, resolvedType, delimited) {
throw Error("illegal type: " + type);
}

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

var newKey = underScore(key);
if (field.resolvedType instanceof Type) {
val[newKey] = fixDescriptorOptionsRecursive(obj[key], field.resolvedType);
val[newKey] = fromDescriptorOptionsRecursive(obj[key], field.resolvedType);
} else if(field.resolvedType instanceof Enum) {
val[newKey] = field.resolvedType.valuesById[obj[key]];
} else {
Expand All @@ -885,19 +885,24 @@ function fixDescriptorOptionsRecursive(obj, type) {
function fromDescriptorOptions(options, type) {
if (!options)
return undefined;
return fixDescriptorOptionsRecursive(type.toObject(options), type);
return fromDescriptorOptionsRecursive(type.toObject(options), type);
}

function camelCaseRecursive(obj) {
function toDescriptorOptionsRecursive(obj, type) {
var val = {};
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
var newKey = $protobuf.util.camelCase(key);
if (typeof obj[key] !== "object") {
val[newKey] = obj[key];
if (!Object.prototype.hasOwnProperty.call(type.fields, newKey)) continue;
var field = type.fields[newKey];
if (field.resolvedType instanceof Type) {
val[newKey] = toDescriptorOptionsRecursive(obj[key], field.resolvedType);
} else {
val[newKey] = camelCaseRecursive(obj[key]);
val[newKey] = obj[key];
}
if (field.repeated && !Array.isArray(val[newKey])) {
val[newKey] = [val[newKey]];
}
}
return val;
Expand All @@ -907,7 +912,7 @@ function camelCaseRecursive(obj) {
function toDescriptorOptions(options, type) {
if (!options)
return undefined;
return type.fromObject(camelCaseRecursive(options));
return type.fromObject(toDescriptorOptionsRecursive(options, type));
}

// Calculates the shortest relative path from `from` to `to`.
Expand Down
1 change: 1 addition & 0 deletions tests/comp_import_extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ tape.test("extensions - proto2 roundtrip", function (test) {
required string required = 2;
repeated int32 packed = 3 [packed = true];
repeated int32 unpacked = 4;
optional int32 repeated_options = 5 [targets = TARGET_TYPE_SERVICE, targets = TARGET_TYPE_FILE];
}
`).root.resolveAll();

Expand Down