golang / protobuf

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

Avoid omitting default values in proto3 #1551

Closed hemalathagajendran closed 1 year ago

hemalathagajendran commented 1 year ago

What version of protobuf and what language are you using? Version: v1.3.2

What did you do? We are trying to use protobuf values from CSI driver to JSON and then use those values to display in UI

What did you expect to see? We want to display the default values (like some usage is 0) in UI but protobuf response is omitting that field

What did you see instead? While response is default value then we are not finding those particular fields in protobuf (proto3)

Make sure you include information that can help us debug (full error message, exception listing, stack trace, logs).

Anything else we should know about your project / environment?

puellanivis commented 1 year ago

When emitting JSON, you can specify that default fields should still be emitted by using:

protojson.MarshalOptions{ EmitUnpopulated: true }

https://pkg.go.dev/google.golang.org/protobuf@v1.30.0/encoding/protojson#MarshalOptions

However, if the JSON has already been marshalled without EmitUnpopulated and stored as raw JSON, then you would have to unmarshal the Protobuf JSON in order to add the default values back in.

synenka commented 1 year ago

Feel free to reopen if you have more questions.