Because the union types are tried in-order, the num-coercable? function used for type-guarding ints and longs runs first, causing all our floats/doubles to cast to integers when serializing to avro.
Replacing the implementation with the following, fixes it:
(defn int-coercable?
"Checks whether `x` can be coerced to a integer with `coercion-fn`
(such as `long`)."
[x coercion-fn]
(try
(and (integer? x)
(coercion-fn (bigint x)))
(catch RuntimeException _e
false)))
(def ^:deprecated num-coercable? int-coercable?
We have a schema which is admittedly wonky and there's a field with the union type declared in this order:
Because the union types are tried in-order, the
num-coercable?
function used for type-guarding ints and longs runs first, causing all our floats/doubles to cast to integers when serializing to avro.Replacing the implementation with the following, fixes it: