golang / protobuf

Go support for Google's protocol buffers
BSD 3-Clause "New" or "Revised" License
9.74k stars 1.58k forks source link

Option to customize or omit @type discriminator when serializing Any to JSON #1563

Closed jalaziz closed 1 year ago

jalaziz commented 1 year ago

I apologize if this has been asked before or if what I'm asking is crazy.

When serializing the WKT Any to json, the TypeURL field is required, which ultimately means we end up with the @type field in the resulting JSON.

Normally this is not a big deal, but when serializing to JSON for a customer-facing API, the @type field is not always the most convenient and it would be ideal if a custom type discriminator could be used or, at least, @type could be omitted.

Is there any way to accomplish this today? I realize this would make deserializing back into protos far more complicated, but custom type discriminator support is not uncommon in JSON deserialization libraries and I'd rather not link proto type information in a public API.

jalaziz commented 1 year ago

Based on https://github.com/golang/protobuf/issues/1409, looks like I need to move this to the official protobuf repo. Researching around shows a lot of heartache trying to map Any and oneof to JSON schema's anyOf and oneOf.