Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JSON types for fields with uncommon json_name #1004

Merged
merged 4 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/protobuf-test/extra/json_types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ import "google/protobuf/wrappers.proto";

message JsonTypesMessage {
bool bool_field = 1 [json_name = "booleanFieldWithCustomName"];
bool json_name_ok = 30 [json_name = "Foo123_bar$"];
bool json_name_at = 31 [json_name = "foo@"];
bool json_name_hyphen = 32 [json_name = "foo-bar"];
bool json_name_start_with_digit = 33 [json_name = "1foo"];
bool json_name_space = 34 [json_name = "foo bar"];
bool json_name_tab = 35 [json_name = "foo\tbar"];
bool json_name_non_ascii = 36 [json_name = "你好"];
bool json_name_escape = 37 [json_name = "foo\nbar\\n"];
double double_field = 2;
bytes bytes_field = 3;
int64 int64_field = 4;
Expand Down
16 changes: 16 additions & 0 deletions packages/protobuf-test/src/generate-code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ test("source retention options are unavailable in generated code", () => {
describe("JSON types", () => {
const ok_ts: json_types_ts_json.JsonTypesMessageJson = {
booleanFieldWithCustomName: true,
Foo123_bar$: true,
"foo@": true,
"foo-bar": true,
"1foo": true,
"foo bar": true,
"foo\tbar": true,
你好: true,
"foo\nbar\\n": true,
doubleField: "Infinity",
bytesField: "aGVsbG8gd29ybGQ=",
int64Field: "123",
Expand All @@ -110,6 +118,14 @@ describe("JSON types", () => {
expect(ok_ts).toBeDefined();
const ok_js: json_types_js_json.JsonTypesMessageJson = {
booleanFieldWithCustomName: true,
Foo123_bar$: true,
"foo@": true,
"foo-bar": true,
"1foo": true,
"foo bar": true,
"foo\tbar": true,
你好: true,
"foo\nbar\\n": true,
doubleField: "Infinity",
bytesField: "aGVsbG8gd29ybGQ=",
int64Field: "123",
Expand Down
8 changes: 6 additions & 2 deletions packages/protoc-gen-es/src/protoc-gen-es-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,13 @@ function generateMessageJsonShape(f: GeneratedFile, message: DescMessage, target
f.print(f.jsDoc(field, " "));
// eslint-disable-next-line no-case-declarations
let jsonName: Printable = field.jsonName;
const startWithNumber = /^[0-9]/;
const containsSpecialChar = /[^a-zA-Z0-9_$]/;
const isAscii = /^[\x00-\x7F]*$/;
if (jsonName === ""
|| /^[0-9]/.test(jsonName)
|| jsonName.indexOf("@") > -1) {
|| startWithNumber.test(jsonName)
|| containsSpecialChar.test(jsonName)
|| isAscii.test(jsonName)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the first regex ensures a subset of ASCII, we don't need the ASCII check. Removed it in f095a7d

jsonName = f.string(jsonName);
}
f.print(" ", jsonName, "?: ", fieldJsonType(field), ";");
Expand Down