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

V2: Fix JSON error message #849

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 6 additions & 8 deletions packages/protobuf-test/extra/msg-json-names.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ package spec;


message JsonNamesMessage {

string scalar_field = 1 [json_name = "scalarFieldJsonName"];
repeated string repeated_scalar_field = 2 [json_name = "repeatedScalarFieldJsonName"];

string a = 3;
string b = 4 [json_name = ""];
string c = 5 [json_name = "@type"];

string scalar_field = 1 [json_name = "scalarFieldJsonName"];
repeated string repeated_scalar_field = 2 [json_name = "repeatedScalarFieldJsonName"];
string a = 3;
string b = 4 [json_name = ""];
string c = 5 [json_name = "@type"];
string d = 6 [json_name = "1d"];
Copy link
Member Author

Choose a reason for hiding this comment

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

This is an unrelated change. Proto field names cannot start with a digit (it's an error in the compiler), but there's no such restriction on json_name. JSON itself doesn't have that restriction either, but something like generated JSON types will have to quote the property. I'm adding this case here in advance.

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion packages/protobuf-test/src/gen/ts/extra/msg-json-names_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/protobuf/src/to-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ function reflectToJson(msg: ReflectMessage, opts: JsonWriteOptions): JsonValue {
if (!msg.isSet(f)) {
if (f.presence == LEGACY_REQUIRED) {
throw new Error(
`cannot encode field ${msg.desc.typeName}.${f.name} to binary: required field not set`,
`cannot encode field ${msg.desc.typeName}.${f.name} to JSON: required field not set`,
Copy link
Member Author

Choose a reason for hiding this comment

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

The fix.

);
}
if (!opts.emitDefaultValues || f.presence !== IMPLICIT) {
Expand Down