ts-proto icon indicating copy to clipboard operation
ts-proto copied to clipboard

JSON format of google.protobuf.Any is not following the proto protocol

Open ilikebasic opened this issue 6 months ago • 1 comments

Eg. for proto message which we want to store as goog.protobuf.Any field:

package foo.bar;
message Person {
  string first_name = 1;
  string last_name = 2;
}

Expected JSON is:

{
  "@type": "foo.bar.Person",
  "firstName": <string>,
  "lastName": <string>
}

But we get:

{
  "typeUrl": "type.googleapis.com/google.profile.Person",
  "firstName": <string>,
  "lastName": <string>
}

Notice the "typeUrl" key name instead of "@type".

This breaks interoperability between languages when using JSON.

ilikebasic avatar Jul 04 '25 02:07 ilikebasic

Hi @ilikebasic , sorry for the late follow-up here -- honestly I am not an expert on Any encoding.

You're right, it does seem @type is the official standard now...

My guess is that we used typeUrl b/c that's the field name in the Any.proto file:

https://github.com/stephenh/ts-proto/blob/ef98faab891adc447c57587d2d2f8ce935b589da/integration/only-types/google/protobuf/any.ts#L121

I think a good slice point would probably be around in here:

https://github.com/stephenh/ts-proto/blob/main/src/main.ts#L2418

If you could submit a PR that switches us over, and add a flag like anyType=@type|typeUrl for backwards compatibility, that would be amazing!

stephenh avatar Jul 15 '25 12:07 stephenh