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

Add types for toJSON/fromJSON methods #1022

Open
thomasboyt opened this issue Mar 27, 2024 · 1 comment
Open

Add types for toJSON/fromJSON methods #1022

thomasboyt opened this issue Mar 27, 2024 · 1 comment

Comments

@thomasboyt
Copy link

We have a service that makes gRPC calls (via grpc-js) to other backend services, while serving its own HTTP endpoints. We'd like to use the JSON representation of protobufs in request and response bodies in those HTTP endpoints, while continuing to use gRPC with native protobufs for our calls to backend services. This will allow us to serve clients that can't use gRPC, but still leverage our protobuf models on either end of the call.

We have types for our HTTP request and response objects (via tsoa). The problem is that we can't simply use the default ts-proto types when defining the request and response types for HTTP endpoints: these types represent the parsed TypeScript model, not the JSON representation. There are, of course, mismatches between the two (int vs string based enums and string timestamps vs Dates are the two obvious ones - not sure if there's more?).

However, while we can't use these types, we also don't, by default, have any other types to use: the generated toJSON and fromJSON for each model use unknown/any types, rather than having defined types.

There is a useJsonWireFormat option, which generates types that match the JSON representation of protobufs. However, I am wary of using this because we are still using native protobufs with gRPC when making calls to other services. I wouldn't want to lose a nice feature like handling timestamps as Dates when interacting with those services, just to make the types work for the HTTP use case.

As a workaround for this, I'm considering generating two sets of ts-proto types - one for the native protobufs, and one for the JSON representation here. This could then be used like:

import { Person } from 'my-protobuf-definitions/person';
import { Person as PersonJSON } from 'my-protobuf-definitions/json/person';

However, I was wondering whether it'd be a reasonable feature for ts-proto to eventually generate JSON types alongside the native types, so that you'd have fully-typed toJSON and fromJSON converters. Then you would be able to do something like:

import { Person, PersonJSON } from 'my-protobuf-definitions/person';

I can imagine this would require a significant refactoring of code generation, so no expectations on my part that this is a feature that gets implemented any time soon - but I am curious whether it'd be a "PR accepted" thing, in case I happen to have any free time in the next few months to contribute it myself.

@stephenh
Copy link
Owner

Hi @thomasboyt ; what you describes makes sense, and yeah "just run ts-proto twice" sounds like a great short-term fix!

But, yeah, having propertly-typed toJSON(): PersonJSON types would be pretty cool. And, as you pointed out, that useJsonWireFormat flag would probably be useful...

If you wanted to work on a PR for this, that'd be great; thanks!

@stephenh stephenh changed the title Generate types for JSON representation of messages for usage in toJSON/fromJSON Add types for toJSON/fromJSON methods Mar 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants