Protobuf.js: pbts CLI returns wrong output for "repeated" declaration

Created on 5 Mar 2020  Â·  3Comments  Â·  Source: protobufjs/protobuf.js

protobuf.js version: 6.8.8

Description

I defined a Composite message which has repeated items. The generated TypeScript definition file from pbts creates the following definiton:

export class Composite implements IComposite {
    constructor(properties?: IComposite);
    public items: [ 'Array' ].<IItem>;
    // ...
}

When trying to compile it I get the following error:

error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.

What pbts should generate to return a valid output is:

export class Composite implements IComposite {
    constructor(properties?: IComposite);
    public items: IItem[];
    // ...
}

Demo Code

messages.proto

// syntax = "proto2";
option java_package = "com.waz.model";

message Composite {
  repeated Item items = 1;
}

message Item {
  oneof content {
      Button button = 1;
  }
}

message Button {
  required string text = 1;
  required string id = 2;
}

compile.js

const fs = require('fs');
const path = require('path');
const {pbjs, pbts} = require('protobufjs/cli');

const dtsOutput = path.join(__dirname, 'Protobuf.d.ts');
const jsOutput = path.join(__dirname, 'Protobuf.js');
const protoBufferFile = path.join(__dirname, 'messages.proto');

pbjs.main(['--target', 'static-module', '--wrap', 'commonjs', protoBufferFile], (error, output) => {
  if (error) {
    throw error;
  }
  fs.writeFileSync(jsOutput, output, {encoding: 'utf8'});
  pbts.main(['--out', dtsOutput, '--no-comments', jsOutput]);
});

Most helpful comment

I also noticed that it generates:

public static verify(message: [ 'object' ].<string, any>): (string|null);
public static fromObject(object: [ 'object' ].<string, any>): Composite;
public static toObject(message: Composite, options?: $protobuf.IConversionOptions): [ 'object' ].<string, any>;
public toJSON(): [ 'object' ].<string, any>;

Instead of:

public static verify(message: { [k: string]: any }): (string|null);
public static fromObject(object: { [k: string]: any }): Text;
public static toObject(message: Composite, options?: $protobuf.IConversionOptions): { [k: string]: any };
public toJSON(): { [k: string]: any };

All 3 comments

I also noticed that it generates:

public static verify(message: [ 'object' ].<string, any>): (string|null);
public static fromObject(object: [ 'object' ].<string, any>): Composite;
public static toObject(message: Composite, options?: $protobuf.IConversionOptions): [ 'object' ].<string, any>;
public toJSON(): [ 'object' ].<string, any>;

Instead of:

public static verify(message: { [k: string]: any }): (string|null);
public static fromObject(object: { [k: string]: any }): Text;
public static toObject(message: Composite, options?: $protobuf.IConversionOptions): { [k: string]: any };
public toJSON(): { [k: string]: any };

Because of #1414 I force protobufjs to use [email protected]

The above error appears for me when using node 12.x but not using 10.x.

Upgrading from protobufjs v6.8.8 to protobufjs v6.10.1 solved the issue for me (using Node.js v14.13.1). ✨

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mj-mehdizadeh picture mj-mehdizadeh  Â·  5Comments

yavin5 picture yavin5  Â·  5Comments

spiderkeys picture spiderkeys  Â·  5Comments

ghost picture ghost  Â·  3Comments

wesleytodd picture wesleytodd  Â·  4Comments