Skip to content

Commit

Permalink
fix: do not allow to extend same field twice to prevent the error (pr…
Browse files Browse the repository at this point in the history
…otobufjs#1784)

* do not allow to extend same field twice to prevent the error

* Ignore gitpod config

* unit test for issue protobufjs#1783

* using existing test file
  • Loading branch information
ThePlenkov authored Feb 7, 2023
1 parent 644d588 commit 14f0536
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ coverage/
sandbox/
.nyc_output
dist/
.gitpod.yml
4 changes: 4 additions & 0 deletions src/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ function tryHandleExtension(root, field) {
var extendedType = field.parent.lookup(field.extend);
if (extendedType) {
var sisterField = new Field(field.fullName, field.id, field.type, field.rule, undefined, field.options);
//do not allow to extend same field twice to prevent the error
if (extendedType.get(sisterField.name)) {
return true;
}
sisterField.declaringField = field;
field.extensionField = sisterField;
extendedType.add(sisterField);
Expand Down
18 changes: 18 additions & 0 deletions tests/comp_import_extend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var path = require("path");
var tape = require("tape");
var protobuf = require("../index");
// to extend Root
require("../ext/descriptor");
tape.test("extensions", function (test) {
// load document with extended field imported multiple times
var root = protobuf.loadSync(path.resolve(__dirname, "data/test.proto"));
root.resolveAll();
// convert to Descriptor Set
var decodedDescriptorSet = root.toDescriptor("proto3");
// load back from descriptor set
var root2 = protobuf.Root.fromDescriptor(decodedDescriptorSet);
test.pass("should parse and resolve without errors");
test.end();
});
35 changes: 35 additions & 0 deletions tests/comp_import_extend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import path = require("path");
import * as tape from "tape";

import * as protobuf from "../index";
import { IFileDescriptorSet } from "../ext/descriptor";
// to extend Root
require("../ext/descriptor");

interface Descriptor {
toDescriptor(
protoVersion: string
): protobuf.Message<IFileDescriptorSet> & IFileDescriptorSet;
fromDescriptor(
descriptor: IFileDescriptorSet | protobuf.Reader | Uint8Array
): protobuf.Root;
}

tape.test("extensions", function (test) {
// load document with extended field imported multiple times
const root = protobuf.loadSync(path.resolve(__dirname, "data/test.proto"));
root.resolveAll();

// convert to Descriptor Set
const decodedDescriptorSet = (root as unknown as Descriptor).toDescriptor(
"proto3"
);

// load back from descriptor set
const root2 = (protobuf.Root as unknown as Descriptor).fromDescriptor(
decodedDescriptorSet
);

test.pass("should parse and resolve without errors");
test.end();
});

0 comments on commit 14f0536

Please sign in to comment.