-
Notifications
You must be signed in to change notification settings - Fork 356
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
Incompatible Wrapper Types with Nest.js (protobufjs) #69
Comments
@stephenh I've opened up a reproduction repo here: https://github.com/ssilve1989/ts-proto-issue-69 |
Ah shoot. Yeah... So, one of my pet goals with I did this by treating proto primitives as required (b/c they come back with default values if unset) and then the well-known types I purposefully make into Then in the However, you're using NestJS's protobuf support to do the actual serde, which doesn't know about my "haha well-known types are my hack to get optional fields back into protobuf". So, not sure the best solution here. You either need to:
I don't know much about how NestJS's protobuf internals works. Obviously my bias would be to teach it about ts-proto's approach because I think it makes a better API for application programmers. :-) |
I need a way to "@" the regular NestJS contributors, i.e. @toonvanstrijp @iangregsondev @lukas-andre @jessequinn to get your thoughts/input on things like this as actual-NestJS-users. Are you guys following all ts-proto issues, and so already see comments/issues like this? If so that's great. Otherwise maybe time for a gitter/spectrum/slack/something setup? Also, let me know if you have any ideas about this particular issue, i.e. is adding this |
There seems to be an option in Nest's grpc options to use a different proto-loader, but there's no documentation on what implementing a custom-loader would look like. |
I have ran into a similar issue when using google timestamp. May i suggest Discord as NestJS has a large community already there. |
This is definitely an issue. A lack of documentation in some cases. Again, many of the NestJS contributors are on Discord. |
According to the google docs,
See wrappers.proto and the reference. The proto3 language guide has a compact table showing all JSON mappings. |
@timostamm I believe that ts-proto itself faithfully implements that JSON mapping you reference, specifically for the AFAIU this issue isn't about JSON serialization, it's that the NestJS binary protobuf encoders/decoders don't know about ts-proto's internal decision to "not wrap" the wrapper types. I haven't had time to look into this, but I'm hoping that the Nest grpc custom proto loader option that @ssilve1989 mentioned would allow the ts-proto-generated clients to tell Nest's grpc internals defer to ts-proto's own |
@stephenh My bad, I thought this was about the JSON rep. Still getting to know ts-proto, but so far I am positively surprised by the Protobuf representation. |
The following Google well known types also do not serialize correctly.
|
@ssilve1989 I just ran into the exact same issue. Did you find a working solution? (custom proto-loader or anything) |
@tomer-friedman No we are just avoiding using wrapper types for now. |
@stephenh hi, I have the same issue as @ssilve1989. I didn’t understand your answer: |
@ofekshalom the gist is that ts-proto already turns
From the For non-NestJS users of ts-proto this is just fine, b/c those users manually call The "bug" is that NestJS doesn't know to call these ts-proto So ideally we'd find a way to tell NestJS to rely on ts-proto's serialization logic, instead of grpc-nodes. I don't use NestJS personally so am not sure how to do this or if its possible. |
Any updates on this? Is it possible to have an option that will generate for example for an optional
to generate a typescript as follows: propertyId?: Int64Value; I've manually set the property to this type and it works as expected. Although I have to manually check if the input property has value sent the Int64Value object and send null if otherwise. |
Could we get option to turn off ts-proto flattening wrapper types? |
@radarsu I'm not against that per se, other than worrying how much code might need to change (maybe not a lot, not sure), and ts-proto's already-a-lot-of-flags slippery slope. Although I wonder if what we're actually doing at that point is: NestJS assumes "basically protobufjs objects", and so while it seems like we're asking ts-proto to "not flatten wrapper types", fundamentally what we're doing is asking ts-proto to match one-for-one the protobufjs objects, so that NestJS is none the wiser. At which point, why use ts-proto and instead just use protobufjs which is what NestJS expects? (Disclaimer I'm not a NestJS expert, so could have ^ wrong/be missing something.) Basically I'd rather teach NestJS to use ts-proto's own |
Are there any workaround for this issue yet? |
I think i finally found a workaround for this problem after spending the whole day on that:D
This can, of course, be extended to incorporate other well known protobuf messages. To automatically run this code, I created a small package, with only one file (
this package can then be used in nest, by setting the |
@lucasmaehn awesome! Yes, this is the key we've been waiting for someone to just spend enough time digging to find. :-) In retrospect, that actually looks pretty easy to get at. I don't have time to look in to it / work on it, but it certainly seems feasible that ts-proto should be able to generate some sort of:
That would install the Timestamp/etc. functions into protobufjs's It might be a little tricky to decide where in the output to put this... Like instead of a single So, a few things to solve, but seems doable. If anyone wants to poke at it, that'd be great. |
Fwiw I think with #762 the wrapper types of Feel free to open issues for other specific wrapper types if they don't work. Like FieldMask probably, but I'm not sure. Also ts-proto's wrapper types could use some love in general, just to clean up the internal handling of them to be easier to reason about and maintain, but that's unfortunately not something I have time to do at the moment. Thanks all! |
The issue still persists, at least for |
When using Google's Well-Known Types with Nest (or just plain protobufjs) the types generated from ts-proto fail to be parsed.
Given this protobuf
we get a typescript interface of
which seems fine. But when attempting to use that as a message that is parsed by a server using protobufjs (nest in this case)
You get the error
This is because protobufjs is actually expecting that interface to be
it is unclear to me who is right here as I can see the argument for either. This was referenced on protobufjs/protobuf.js#1042 and several other issues but there does not seem to be any update in a very long time from protobufjs on the matter
The text was updated successfully, but these errors were encountered: