Generate a response without stripping off default values in protobuf

368 Views Asked by At

We have a API where we send the response by parsing it with protobuf contracts and we send the result to the consumers. It strips of some of the fields which has default values(0 for integer, false for boolean etc.) but we don't want to allow that behaviour.

Is there a way that we can either stop protobuf from stripping the default values in response or maybe construct a response with all the default values and merge it with our regular response so that we can send back those fields with default values?

For example, if we have

{"foo": 0.0, "bar": false, "baz": "abc"}

in the response we only get

{"baz": "abc"}

because protobuf strips off the values. But, we also want to get full response with zero values.

We are using below clojure snippet to generate the JSON:

(-> (JsonFormat/printer)
    (.print proto)
    (json/parse-string true))
1

There are 1 best solutions below

0
On BEST ANSWER
(-> (JsonFormat/printer)
    (.includingDefaultValueFields)
    (.print proto)
    (json/parse-string true))

calling (.includingDefaultValueFields) does the trick.