Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0f4bb87

Browse files
committedJan 30, 2023
fix: throw when unsupported fields are detected
`required` is a field in proto2 but not in proto3 so throw if it is encountered while generating `.ts` from `.proto`. It would be better to detect `proto2` from the `syntax` directive but it's not in the output of the `pbjs -t json` command. Fixes #34
1 parent 3ac2c56 commit 0f4bb87

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed
 

‎packages/protons/src/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,10 @@ function defineModule (def: ClassDef): ModuleDef {
595595
fieldDef.repeated = fieldDef.rule === 'repeated'
596596
fieldDef.optional = !fieldDef.repeated && fieldDef.options?.proto3_optional === true
597597
fieldDef.map = fieldDef.keyType != null
598+
599+
if (fieldDef.rule === 'required') {
600+
throw new Error('"required" fields are not allowed in proto3 - please convert your proto2 definitions to proto3')
601+
}
598602
}
599603
}
600604

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
syntax = "proto2";
2+
3+
message Message {
4+
required string requiredField = 1;
5+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* eslint-env mocha */
2+
/* eslint-disable @typescript-eslint/restrict-template-expressions */
3+
4+
import { expect } from 'aegir/chai'
5+
import { execa } from 'execa'
6+
7+
describe('unsupported', () => {
8+
it('should refuse to generate source from proto2 definition', async () => {
9+
await expect(execa('dist/bin/protons.js', ['./test/fixtures/proto2.proto'])).to.eventually.be.rejected()
10+
.with.property('stderr').that.contain('"required" fields are not allowed in proto3')
11+
})
12+
})

0 commit comments

Comments
 (0)
Please sign in to comment.