GRPC fallback to Rest API with custom field names

When you generate JSON for Rest API from proto-file, protoc-gen-gofast generates field names for JSON in lowerCamelCase format while most of the Rest APIs use snake_case for that.
And if you want to replace some legacy API with your new implementation without breaking backward compatibility, you need to fix it.
There could be different ways to fix it (it is really simple to implement for response fields while it’s much harder for request fields and I found only a hacky way to fix swagger’s definitions).
But the most flexible and simple way is to use field option on proto field:
string global_entity_id = 2; will become
string global_entity_id = 2 [json_name="global_entity_id"];.
And this option will affect the field name for JSON (in request or response) and swagger definitions as well.
More details here — https://developers.google.com/protocol-buffers/docs/proto3#json.
And special thanks to this comment on stackoverflow — https://stackoverflow.com/a/51836178/801426.
A lot more options available can be found here — https://blog.bullgare.com/2020/07/complete-list-of-swagger-options-to-protobuf-file/

Similar Posts

LEAVE A COMMENT