-
Notifications
You must be signed in to change notification settings - Fork 381
feat: add protoJsonFormat option to support standard Protobuf JSON message #1241
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
Conversation
README.markdown
Outdated
|
|
||
| The default behavior is `keys_json`, i.e. both will be camel cased, and `json_name` will be used if set. | ||
|
|
||
| - With `--ts_proto_opt=protoJsonFormat=true`, the `fromJSON` method will accept both the `json_name` (or camelCased name) and the original proto field name (often snake_cased), without duplication of the field if the two names are identical. This option also implies `snakeToCamel=json` to ensure that standard JSON keys are generated in lowerCamelCase (or use `json_name`), as mandated by the spec. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @guglielmo-san ; thanks for the PR, this looks great!
Given you're making ts-proto more spec compliant, I'm kinda wondering if we should default this new option to true, and then allow users who really actively don't want the fallback/spec behavior, to turn it off by setting =false.
Particularly since you made the output conditional on fieldName !== jsonName, which I really appreciate it.
Does that sound good to you?
Otherwise I think just a small diff error in the CI build, and once that is fixed I'll hit merge -- thanks again!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for the hassle here @guglielmo-san , but with the default flipped, it looks like all the codegen files will need pushed into the PR -- sorry this can seem like a pita, but its a form our of "regression tests" to see how each PR affects the generated code output, given that is our primary deliverable. :-)
Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @stephenh , yes it is a good idea to have the protoJsonFormat as default behavior. I updated the value and the README and generated again all the .ts files in the integration tests.
Now the workflows should pass successfully.
|
This is great @guglielmo-san , hitting merge -- thanks again! |
|
🎉 This PR is included in version 2.11.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
The Protobuf JSON Mapping specification requires that messages parsers accept both the lowerCamelCase name (or explicit json_name) and the original proto field name (often snake_case) when parsing JSON. Before this change, ts-proto's fromJSON generated method only supported one format based on the snakeToCamel configuration, typically prioritizing camelCase.
Mechanism: This PR introduces a new boolean option: protoJsonFormat. When enabled:
Example:
Generated fromJSON:
Changes:
Fixes #1012
This change is required to adopt the ts-proto library in the A2A-js project