grpc-gateway icon indicating copy to clipboard operation
grpc-gateway copied to clipboard

Add Bytes and Enum handlers to `proto2ConvertFuncs`

Open vaishali-ntnx opened this issue 6 years ago • 12 comments

Due to backward compatibility issue, I am forced to use proto2. I am not sure if my clients can keep on using proto2 and I can move to proto3. Also it is recommended to keep using proto2 as proto3 doesn't have many new features.

So, if I am supposed to use proto2 only, is there a workaround for it given bytes_type and enums are not supported?

vaishali-ntnx avatar Oct 15 '18 14:10 vaishali-ntnx

Any workaround for this that I can do without changing from proto2 to proto3?

vaishali-ntnx avatar Oct 16 '18 05:10 vaishali-ntnx

Base64 encode the data in a string type maybe?

johanbrandhorst avatar Oct 16 '18 06:10 johanbrandhorst

rpc EntityUpdate(EntityUpdateArg) returns (EntityUpdateRet) { option (google.api.http) = { put: "/v1/tentity/{uuid}" body: "*" }; }

Given the above and given uuid in bytes, how am i supposed to achieve that?

vaishali-ntnx avatar Oct 16 '18 07:10 vaishali-ntnx

You'd have to encode it in the client anyway, you can't send raw bytes in a URL. If it's a uuid though, maybe just send the string representation of the uuid?

johanbrandhorst avatar Oct 16 '18 07:10 johanbrandhorst

Agreed but in EntityUpdateArg, uuid is in bytes. How the type conversion is done here? Apologies I am not much aware of that. Will it be something like - put: "/v1/entity/string{uuid}" ?

vaishali-ntnx avatar Oct 16 '18 08:10 vaishali-ntnx

If it's a path parameter, it has to be URL-encoded or base64-encoded and stored in a string. If it's in the body of the message, I guess you may still want to base64 encode it.

johanbrandhorst avatar Oct 16 '18 08:10 johanbrandhorst

Few things to clarify here - If its a path param, I create a string entry in EntityUpdateArg say uuid_str and use that in path param? If thats the case, then for body of the message, where is this encoding supposed to happen? Given the fact that everything needs to be handled on server end and atleast existing rpc clients are not expected to change.

Also I am only supposed to add fields in proto as interface.pb.gw.go is generated. So any such operation of encoding etc has to be part of proto definition.

vaishali-ntnx avatar Oct 16 '18 09:10 vaishali-ntnx

If you add it as a path param you don't need to add it to the body of the message. If existing clients are already sending the UUID in the path, what format is it using, since it can't be bytes?

johanbrandhorst avatar Oct 16 '18 10:10 johanbrandhorst

My existing clients are rpc clients only. On top of existing proto, I wanted to expose REST interface for new clients. But given I have to use field from existing proto (unless a new string type field is added), the generation of interface.pb.gw.go fails from existing interface.proto

vaishali-ntnx avatar Oct 16 '18 10:10 vaishali-ntnx

Ah I see. In that case, maybe just make the uuid a parameter in the body instead? So you can reuse the same types. If that doesn't work, you will need to add a uuid_str and manually handle it in the service.

johanbrandhorst avatar Oct 16 '18 12:10 johanbrandhorst

Does that mean that conversion from bytes_type is supported in body and not for path params as far as proto2 is concerned? If thats the case, is there any plan to make that enhancement in near future?

vaishali-ntnx avatar Oct 16 '18 13:10 vaishali-ntnx

I frankly don't know, I'm afraid. Try it :man_shrugging:.

johanbrandhorst avatar Oct 16 '18 13:10 johanbrandhorst