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

Is there a way to use google.protobuf.Any in NestJs (gRPC/proto3) #4621

Closed
imi187 opened this issue Apr 17, 2020 · 4 comments
Closed

Is there a way to use google.protobuf.Any in NestJs (gRPC/proto3) #4621

imi187 opened this issue Apr 17, 2020 · 4 comments
Labels
needs triage This issue has not been looked into type: question 🙌

Comments

@imi187
Copy link

imi187 commented Apr 17, 2020

I can't find a way neither with serialization method in your options while starting the server like RMQ nor a transform-interceptor.
I read there are ways:
protocolbuffers/protobuf-javascript#68
https://developers.google.com/protocol-buffers/docs/proto3#any
But nothing is provided to get the raw message only pre-formated.

If I use google.protobuf.Any in my *.proto file it return a empty object in my message while I give a object with params and value's.

*.proto

syntax = "proto3";
package inventory;
import "google/protobuf/any.proto";

message CrudRequest {
  string crudRequest = 1;
  Any crudRequestTyped = 2;
}

Interface

interface ProductServiceRpc {
  findOne(req: { crudRequest: string, crudRequestTyped: CrudRequest }): Observable<any>;
}

Method in Client

I want to remove the JSON.stringify and replace it with crudRequestTyped

getOne(req: CrudRequest): Promise<any> {
    const test = this.productServiceRpc.findOne({ crudRequest: JSON.stringify(req), crudRequestTyped: req });
    return test.toPromise();
}

Result in Server

{ crudRequest:
   '{"parsed":{"fields":[],"paramsFilter":[{"field":"id","operator":"$eq","value":"5e8e7232-5bc7-437b-9972-a2f12440877a"}],"search":{"$and":[null,{"id":{"$eq":"5e8e7232-5bc7-437b-9972-a2f12440877a"}},{}]},"filter":[],"or":[],"join":[],"sort":[]},"options":{"query":{"alwaysPaginate":false},"routes":{"getManyBase":{"interceptors":[],"decorators":[]},"getOneBase":{"interceptors":[],"decorators":[]},"createOneBase":{"interceptors":[],"decorators":[],"returnShallow":false},"createManyBase":{"interceptors":[],"decorators":[]},"updateOneBase":{"interceptors":[],"decorators":[],"allowParamsOverride":false,"returnShallow":false},"replaceOneBase":{"interceptors":[],"decorators":[],"allowParamsOverride":false,"returnShallow":false},"deleteOneBase":{"interceptors":[],"decorators":[],"returnDeleted":false}},"params":{"id":{"field":"id","type":"uuid","primary":true}}}}',
  crudRequestTyped: {}      <-------- is always empty

Thx Imi.

@imi187 imi187 added needs triage This issue has not been looked into type: question 🙌 labels Apr 17, 2020
@kamilmysliwiec
Copy link
Member

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.

@imtosss123
Copy link

@imi187 hello, did you solve this yet, I have the same issue with you, and already try to ask on discord, but I still not get an answer.

@imi187
Copy link
Author

imi187 commented May 28, 2020

(@imtosss123 )

Nope, I did not.

I put the label "Question" on it so they told me to ask it on Discord.
I don't want to use discord for technical things like this.
I spent time on it to write it proper in Github so it could help others to.

After a long search I did not find a way to get the raw message in NestJS
I don't have time for a feature request/bug report so I used RPC over RabitMQ
(@golevelup/nestjs-rabbitmq)

For the moment it helps but we will have to change it in the future.
Because this is not what I want.

Proto needs to know your type to reserve memory for that specific type.
(Example: fixed32 in proto will result in a uint32 in C++ (Four bytes (32bit)))

They provided a google.protobuf.Any. But you can only buffer it.

You can always use the library in node proto3 without using NestJs.
(I did not test it!)

Hope It helps.

@jmcdo29
Copy link
Member

jmcdo29 commented May 28, 2020

@imtosss123 @imi187 In the future, it would be helpful to have a minimum reproduction and not jsut code snippets. Something that can be cloned, have its dependencies installed, and ran. I ended up making a reproduction to test this morning, and while I saw the problem you're facing, I also found this issue from the @grpc/proto-loader library which states that it doesn't handle ANY by itself, so you'll have to come up with some way to serialize and deserialize the binary blob on your own. This isn;'t an issue with Nest per se, but with a library Nest is using under the hood. You can always change the proto loader using the protoLoader option in the microservices options object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into type: question 🙌
Projects
None yet
Development

No branches or pull requests

4 participants