From e72a709885a0565efb672d8ec525251739bdeeea Mon Sep 17 00:00:00 2001 From: Michael Green Date: Wed, 12 Oct 2022 11:08:02 -0500 Subject: [PATCH 1/4] Use tsconfig file instead of wildcards to enable running on Windows --- package.json | 4 ++-- tests/data/all_files.tsconfig.json | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 tests/data/all_files.tsconfig.json diff --git a/package.json b/package.json index 864357000..311c3cee2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "protobufjs", - "version": "7.1.2", + "version": "7.1.3", "versionScheme": "~", "description": "Protocol Buffers for JavaScript (& TypeScript).", "author": "Daniel Wirtz ", @@ -36,7 +36,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/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"] +} From 996a09e4a8b729f350e0509136fe2ae316a93444 Mon Sep 17 00:00:00 2001 From: Michael Green Date: Wed, 12 Oct 2022 11:42:47 -0500 Subject: [PATCH 2/4] Honor the json_name option. This is useful when a .proto file is compiled for multiple languages, to conform to whatever random casing rules were chosen for the project: - The .proto convention is to use lower_snake_case for field names, and UPPER_SNAKE_CASE for enum names - C++ (via protoc) simply uses lower_snake_case for field names - C# (via protobuf-net) uses PascalCase for both field and enum names, and has options for customizing the names: https://github.com/protobuf-net/protobuf-net/blob/main/src/Tools/protogen.proto - JavaScript (via protobuf.js) uses camelCase for field names, and the json_name attribute can now be used to customize the name Example: message NetworkAdapter { string get_ip_address = 1 [json_name = "getIPAddress", (.protobuf_net.fieldopt).name ="GetIPAddress"]; } Default names without the options: - C++: get_ip_address (unchanged) - C#: GetIpAddress - JS: getIpAddress --- src/field.js | 10 ++++++++++ src/parse.js | 2 ++ 2 files changed, 12 insertions(+) diff --git a/src/field.js b/src/field.js index e0feb8b43..5c12198b0 100644 --- a/src/field.js +++ b/src/field.js @@ -221,6 +221,16 @@ Field.prototype.setOption = function setOption(name, value, ifNotSet) { return ReflectionObject.prototype.setOption.call(this, name, value, ifNotSet); }; +Field.prototype.applyParsedOptions = function applyParsedOptions() { + 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 be9dd5a2f..3c4f66350 100644 --- a/src/parse.js +++ b/src/parse.js @@ -388,6 +388,8 @@ function parse(source, root, options) { parseInlineOptions(field); }); + field.applyParsedOptions(); + if (rule === "proto3_optional") { // for proto3 optional fields, we create a single-member Oneof to mimic "optional" behavior var oneof = new OneOf("_" + name); From c0b68ac5c8fbb1ded261c0924bbce0538370333e Mon Sep 17 00:00:00 2001 From: Michael Green Date: Wed, 12 Oct 2022 12:59:00 -0500 Subject: [PATCH 3/4] Test for json_name option --- tests/comp_options-parse.js | 2 ++ tests/data/options_test.proto | 1 + 2 files changed, 3 insertions(+) diff --git a/tests/comp_options-parse.js b/tests/comp_options-parse.js index 18690a424..e2f26de39 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/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 { From 8777b33151569c8f170cf5e7c67cb7f4367537d3 Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Fri, 8 Dec 2023 23:23:06 +0000 Subject: [PATCH 4/4] renamed function for clarity --- src/field.js | 7 ++++--- src/parse.js | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/field.js b/src/field.js index 5c12198b0..1af2fa55d 100644 --- a/src/field.js +++ b/src/field.js @@ -221,13 +221,14 @@ Field.prototype.setOption = function setOption(name, value, ifNotSet) { return ReflectionObject.prototype.setOption.call(this, name, value, ifNotSet); }; -Field.prototype.applyParsedOptions = function applyParsedOptions() { - if(this.parsedOptions !== null) { +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) + if (opt) { this.name = opt["json_name"]; + } } }; diff --git a/src/parse.js b/src/parse.js index 08466cca8..574f3b6f9 100644 --- a/src/parse.js +++ b/src/parse.js @@ -398,7 +398,7 @@ function parse(source, root, options) { parseInlineOptions(field); }); - field.applyParsedOptions(); + field.applyJsonName(); if (rule === "proto3_optional") { // for proto3 optional fields, we create a single-member Oneof to mimic "optional" behavior