grpc / grpc/grpc-node

TypeError when using optional on custom field options

Open
#2,047 15 comments 0 reactions 0 assignees View on GitHub
package: @grpc/proto-loader
Dominant language
TypeScript
Stars
4.8k
Forks
716
Avg merge
2d 3h
Merged PRs (30d)
10

Description

### Problem description

When marking a custom field option as optional, `loadSync` throws a the following error

```
TypeError: Cannot read properties of undefined (reading 'indexOf')
```

Note this error only occurs when using `optional`

### Reproduction steps

1. Clone https://github.com/ehyland/tmp-proto-loader-issue
2. Install dependencies `npm i`
3. Run test script `node test-with-optional.js`

### Environment
- OS & Arch: MacOS 12.2 - amd64
- Node: `v16.13.0`
- Node installation method: `nvm`
- Package name and version: `@grpc/proto-loader@0.6.9`

### Additional context

**Error with stack**

```
./node_modules/protobufjs/ext/descriptor/index.js:488
if ((descriptor.oneofIndex = this.parent.oneofsArray.indexOf(this.partOf)) < 0)
^

TypeError: Cannot read properties of undefined (reading 'indexOf')
at Field.toDescriptor (./node_modules/protobufjs/ext/descriptor/index.js:488:62)
at Root_toDescriptorRecursive (./node_modules/protobufjs/ext/descriptor/index.js:142:40)
at Root_toDescriptorRecursive (./node_modules/protobufjs/ext/descriptor/index.js:146:13)
at Root.toDescriptor (./node_modules/protobufjs/ext/descriptor/index.js:121:5)
at createPackageDefinition (./node_modules/@grpc/proto-loader/build/src/index.js:148:33)
at Object.loadSync (./node_modules/@grpc/proto-loader/build/src/index.js:195:12)
at Object. (./test.js:6:13)
```

**Proto file that will fail to load**

See https://github.com/ehyland/tmp-proto-loader-issue to reproduce

```proto
syntax = "proto3";
package example;
import "google/protobuf/descriptor.proto";

extend google.protobuf.FieldOptions {
optional bool sensitive = 99999;
}

message MyMessage {
string id = 1;
string secret_sauce = 3 [(example.sensitive) = true];
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.