Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avro schemas with union types with int declared before float encode all floats to ints #349

Open
kevinmershon opened this issue Dec 30, 2022 · 0 comments

Comments

@kevinmershon
Copy link

kevinmershon commented Dec 30, 2022

We have a schema which is admittedly wonky and there's a field with the union type declared in this order:

{
  "doc": "Single simple value.",
  "name": "value",
  "type": ["null", "boolean", "int", "long", "float", "double", "string"]
}

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?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant