Skip to content

Commit 1cac5cf

Browse files
authored
fix(descriptor): guard oneof index for non-Type parents (#2122)
1 parent 549b05e commit 1cac5cf

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

ext/descriptor/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,10 +518,11 @@ Field.prototype.toDescriptor = function toDescriptor(edition) {
518518
// Handle extension field
519519
descriptor.extendee = this.extensionField ? this.extensionField.parent.fullName : this.extend;
520520

521-
// Handle part of oneof
522-
if (this.partOf)
521+
// Handle part of oneof (only meaningful for message types)
522+
if (this.partOf && this.parent instanceof Type) {
523523
if ((descriptor.oneofIndex = this.parent.oneofsArray.indexOf(this.partOf)) < 0)
524524
throw Error("missing oneof");
525+
}
525526

526527
if (this.options) {
527528
descriptor.options = toDescriptorOptions(this.options, exports.FieldOptions);

tests/comp_import_extend.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,38 @@ tape.test("extensions - proto3 roundtrip", function (test) {
8484
test.end();
8585
});
8686

87+
tape.test("extensions - proto3 optional in extend toDescriptor", function (test) {
88+
var root = protobuf.parse(`syntax = "proto3";
89+
90+
message SomeMessage {
91+
string foo = 1;
92+
}
93+
94+
extend SomeMessage {
95+
optional string bar = 2;
96+
}
97+
`).root.resolveAll();
98+
99+
var decodedDescriptorSet;
100+
try {
101+
decodedDescriptorSet = root.toDescriptor("proto3");
102+
test.pass("toDescriptor should not throw");
103+
} catch (err) {
104+
test.fail(err);
105+
test.end();
106+
return;
107+
}
108+
109+
test.ok(decodedDescriptorSet.file[0].extension && decodedDescriptorSet.file[0].extension.length === 1,
110+
"should include extension field");
111+
112+
var ext = decodedDescriptorSet.file[0].extension[0];
113+
test.equal(ext.name, "bar", "extension field name is preserved");
114+
test.equal(ext.proto3_optional, true, "proto3_optional flag is preserved");
115+
116+
test.end();
117+
});
118+
87119
tape.test("extensions - edition 2023 file roundtrip", function (test) {
88120
var json = {
89121
nested: { Message: {

0 commit comments

Comments
 (0)