protobuf.js icon indicating copy to clipboard operation
protobuf.js copied to clipboard

pbjs generating wrong javascript

Open alanliang opened this issue 8 years ago • 1 comments

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;

alanliang avatar Jul 16 '17 00:07 alanliang

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.

ljluestc avatar Sep 10 '23 17:09 ljluestc