ts-proto icon indicating copy to clipboard operation
ts-proto copied to clipboard

fromJSON should accept both snake_case and camelCase keys

Open jdbp-classpass opened this issue 1 year ago • 1 comments

The proto3 JSON specification says that Proto3 JSON parsers are required to accept both the converted lowerCamelCase name and the proto field name.. This seems to mean that, given a message with a field like "my_field", the fromJson method should accept either {"my_field": "value"} or {"myField": "value"}. Google's standard protobuf Java library works this way.

Currently the ts-proto generated fromJSON only accepts one or the other, as determined by the snakeToCamel option. Ideally snakeToCamel would only affect toJSON (which is how the spec describes it), but fromJSON would accept either one regardless.

jdbp-classpass avatar Mar 05 '24 14:03 jdbp-classpass

Hi @jdbp-classpass , yep, that is correct. I think we've not implemented this yet because I assume most companies just pick "either snake or camel" are their company's internal standard, and then don't deviate from that.

If you'd like to submit a PR that teaches fromJSON to support either, ideally behind a flag (like another snakeToCamel option I assume), so that current users don't pay for the code size & perf of checking two keys, that would be great! Thanks!

stephenh avatar Mar 06 '24 15:03 stephenh