Skip to content

Error using extend with fromJSON #1080

@cusher

Description

@cusher

protobuf.js version: 6.8.7 (master e8449c4)

When calling protobuf.Root.fromJSON on a JSON descriptor of a proto that contains an extend, an error is thrown saying the extended field is a duplicate.

For example a proto like:

syntax = "proto3";
package mypackage.options;
import "google/protobuf/descriptor.proto";
enum Scope {
    ALL = 0;
    HINT = 1;
}
extend google.protobuf.FieldOptions {
    repeated Scope scope = 12345;
}

And the following in TypeScript:

import * as protobuf from 'protobufjs/light';
import * as Long from 'long';
protobuf.util.Long = Long;
protobuf.configure();

import * as protoDescriptor from './proto-reflection';
import {INamespace} from 'protobufjs';
export const protoRoot = protobuf.Root.fromJSON(protoDescriptor as INamespace);

Results in the error:

type.js:331 Uncaught Error: duplicate name '.mypackage.options.scope' in Type .google.protobuf.FieldOptions
    at Type.add (type.js:331)
    at tryHandleExtension (root.js:266)
    at Root._handleAdd (root.js:294)
    at Root._handleAdd (root.js:299)
    at Root._handleAdd (root.js:299)
    at Namespace.onAdd (object.js:120)
    at Root.add (namespace.js:243)
    at Root.addJSON (namespace.js:172)
    at Function.fromJSON (root.js:51)
    at Object../src/app/protos-helper.ts (protos-helper.ts:13)

One way to work around this is to modify tryHandleExtension in protobufjs/src/root.js to replace:

extendedType.add(sisterField);

with:

if (!extendedType.get(sisterField.name)) {
  extendedType.add(sisterField);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions