Nullable fields in json generated from protobuf file

Native support

With grpc v2 for go, protofile’s optional option is supported, so you don’t need to import proto extensions anymore and use things like google.protobuf.StringValue.

my.proto

go mapper

Legacy way with extensions

Null for nil value

my.proto

go mapper

Which will lead to this json response

Omit a field with nil value

If you want to omit the field completely, you can use oneof approach

proto

go mapper

More info on json options for grpc — https://blog.bullgare.com/2020/07/complete-list-of-swagger-options-to-protobuf-file/

Similar Posts

LEAVE A COMMENT