0

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))
jpa
  • 10,351
  • 1
  • 28
  • 45
Pratik
  • 1,351
  • 1
  • 20
  • 37
  • 1
    How do you generate the json? Typically there's an option to output the default values (eg: `alwaysOutputDefaultValueFields` in java) – 0x26res Dec 13 '22 at 11:49
  • We are using clojure and doing `(-> (JsonFormat/printer) (.print proto) (json/parse-string true))` seems like what you pointed out should work, I just need to figure out how to pass the field in clojure code. – Pratik Dec 13 '22 at 11:58
  • I've figured a way to do that, thank you @0x26res – Pratik Dec 13 '22 at 12:04
  • 1
    good, please edit your question with the closure code, and post an answer with the correct API call. – 0x26res Dec 13 '22 at 12:22

1 Answers1

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

calling (.includingDefaultValueFields) does the trick.

Pratik
  • 1,351
  • 1
  • 20
  • 37