Skip to content

Commit

Permalink
fix: throw when unsupported fields are detected
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
achingbrain committed Jan 30, 2023
1 parent 3ac2c56 commit 0f4bb87
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/protons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,10 @@ function defineModule (def: ClassDef): ModuleDef {
fieldDef.repeated = fieldDef.rule === 'repeated'
fieldDef.optional = !fieldDef.repeated && fieldDef.options?.proto3_optional === true
fieldDef.map = fieldDef.keyType != null

if (fieldDef.rule === 'required') {
throw new Error('"required" fields are not allowed in proto3 - please convert your proto2 definitions to proto3')
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions packages/protons/test/fixtures/proto2.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
syntax = "proto2";

message Message {
required string requiredField = 1;
}
12 changes: 12 additions & 0 deletions packages/protons/test/unsupported.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* eslint-env mocha */
/* eslint-disable @typescript-eslint/restrict-template-expressions */

import { expect } from 'aegir/chai'
import { execa } from 'execa'

describe('unsupported', () => {
it('should refuse to generate source from proto2 definition', async () => {
await expect(execa('dist/bin/protons.js', ['./test/fixtures/proto2.proto'])).to.eventually.be.rejected()
.with.property('stderr').that.contain('"required" fields are not allowed in proto3')
})
})

0 comments on commit 0f4bb87

Please sign in to comment.