protobuf.js
protobuf.js copied to clipboard
pbjs generating wrong javascript
protobuf.js version: master
Generated javascript seems to be wrong:
test.proto:
package awesomepackage;
syntax = "proto3";
message AwesomeMessage {
string awesome_field = 1; // becomes awesomeField
}
I ran the following command:
bin/pbjs -t static-module -w commonjs -o output.js test.proto
output.js:
/*eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins*/
"use strict";
var $protobuf = require("protobufjs/minimal");
// Common aliases
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
// Exported root namespace
var $root = $protobuf.roots["default"] || ($protobuf.roots["default"] = {});
module.exports = $root;
package awesomepackage;
syntax = "proto3";
option javascript_field_style = "snake_case"; // Preserve snake_case
message AwesomeMessage {
string awesome_field = 1; // remains as awesome_field
}
option javascript_field_style = "snake_case"; line, which instructs the Protocol Buffers compiler (protoc) to preserve the snake_case naming convention in the generated JavaScript code.
After making this change, you can recompile your Protocol Buffers file using the protoc compiler:
protoc --js_out=import_style=commonjs,binary:. test.proto
This command generates the JavaScript code with snake_case field names as specified in your test.proto file.