diff --git a/package.json b/package.json index 37c744ff8..c9a01246a 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "prof": "node bench/prof", "test": "npm run test:sources && npm run test:types", "test:sources": "tape -r ./lib/tape-adapter tests/*.js tests/node/*.js", - "test:types": "tsc tests/comp_typescript.ts --lib es2015 --esModuleInterop --strictNullChecks --experimentalDecorators --emitDecoratorMetadata && tsc tests/data/test.js.ts --lib es2015 --esModuleInterop --noEmit --strictNullChecks && tsc tests/data/*.ts --lib es2015 --esModuleInterop --noEmit --strictNullChecks", + "test:types": "tsc tests/comp_typescript.ts --lib es2015 --esModuleInterop --strictNullChecks --experimentalDecorators --emitDecoratorMetadata && tsc tests/data/test.js.ts --lib es2015 --esModuleInterop --noEmit --strictNullChecks && tsc --project tests/data/all_files.tsconfig.json --lib es2015 --esModuleInterop --noEmit --strictNullChecks", "make": "npm run lint:sources && npm run build && npm run lint:types && node ./scripts/gentests.js && npm test" }, "dependencies": { diff --git a/src/field.js b/src/field.js index e0feb8b43..1af2fa55d 100644 --- a/src/field.js +++ b/src/field.js @@ -221,6 +221,17 @@ Field.prototype.setOption = function setOption(name, value, ifNotSet) { return ReflectionObject.prototype.setOption.call(this, name, value, ifNotSet); }; +Field.prototype.applyJsonName = function applyJsonName() { + if (this.parsedOptions !== null) { + var opt = this.parsedOptions.find(function (opt) { + return Object.prototype.hasOwnProperty.call(opt, "json_name"); + }); + if (opt) { + this.name = opt["json_name"]; + } + } +}; + /** * Field descriptor. * @interface IField diff --git a/src/parse.js b/src/parse.js index 739f3265e..574f3b6f9 100644 --- a/src/parse.js +++ b/src/parse.js @@ -398,6 +398,8 @@ function parse(source, root, options) { parseInlineOptions(field); }); + field.applyJsonName(); + if (rule === "proto3_optional") { // for proto3 optional fields, we create a single-member Oneof to mimic "optional" behavior var oneof = new OneOf("_" + name); diff --git a/tests/comp_options-parse.js b/tests/comp_options-parse.js index ed4a00572..b42a91f62 100644 --- a/tests/comp_options-parse.js +++ b/tests/comp_options-parse.js @@ -32,6 +32,8 @@ tape.test("Options", function (test) { test.equal(TestFieldOptionsMsg.fields.field2.options["(fo_single_msg).value"], 7, "should correctly parse single msg option"); test.equal(TestFieldOptionsMsg.fields.field2.options["(fo_single_msg).rep_value"], 9, "should take second repeated int in single msg option"); test.same(TestFieldOptionsMsg.fields.field2.parsedOptions, [{"(fo_single_msg)": {value: 7, rep_value: [8,9]}}], "should take all repeated message option"); + test.equal(TestFieldOptionsMsg.fields.Field_Three.options["json_name"], "Field_Three", "should correctly parse json_name option"); + test.equal(TestFieldOptionsMsg.fields.field3, undefined, "json_name option should change field name"); test.end(); }); diff --git a/tests/data/all_files.tsconfig.json b/tests/data/all_files.tsconfig.json new file mode 100644 index 000000000..73b9679db --- /dev/null +++ b/tests/data/all_files.tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "../../tsconfig.json", + "include": ["*.ts"] +} diff --git a/tests/data/options_test.proto b/tests/data/options_test.proto index 52f022818..08a69a3cd 100644 --- a/tests/data/options_test.proto +++ b/tests/data/options_test.proto @@ -61,6 +61,7 @@ extend google.protobuf.MethodOptions { message TestFieldOptionsMsg { string field1 = 1 [(fo_rep_msg) = {value: 1 rep_value: 2 rep_value: 3}, (fo_rep_msg) = {value: 4 rep_value: 5 rep_value: 6}]; string field2 = 2 [(fo_single_msg).value = 7, (fo_single_msg).rep_value = 8, (fo_single_msg).rep_value = 9]; + string field3 = 3 [json_name = "Field_Three"]; } message TestMessageOptionsMsg {