diff --git a/CHANGELOG.md b/CHANGELOG.md index 68c93a2a..58a75ce7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Unreleased +- Fixed a bug with avro serialization, where default values within a union type would not be used correctly. + ### [0.9.12] - [2023-12-05] - Support for Foreign Key joins [#365](https://github.com/FundingCircle/jackdaw/pull/365) (Issue [#364]) - add manifold and keep aleph in dev dependencies [#360](https://github.com/FundingCircle/jackdaw/pull/360). Users of test-machine will have to add aleph to the test deps in their app. diff --git a/src/jackdaw/serdes/avro.clj b/src/jackdaw/serdes/avro.clj index a9e253e3..b8daaf5c 100644 --- a/src/jackdaw/serdes/avro.clj +++ b/src/jackdaw/serdes/avro.clj @@ -402,7 +402,7 @@ (and (every? (fn [[field-key [^Schema$Field field field-coercion]]] (let [field-value (get clj-map field-key ::missing)] (if (= field-value ::missing) - (.defaultVal field) + (.hasDefaultValue field) (match-clj? field-coercion field-value)))) field->schema+coercion) (empty? unknown-fields)))) diff --git a/test/jackdaw/serdes/avro_test.clj b/test/jackdaw/serdes/avro_test.clj index 7f4d4a2b..7fb70dda 100644 --- a/test/jackdaw/serdes/avro_test.clj +++ b/test/jackdaw/serdes/avro_test.clj @@ -463,6 +463,21 @@ (is (= {:myunion {:field2 "hello"}} (round-trip serde "whatever" {:myunion {:field2 "hello"}}))))) +(deftest default-values-are-used-when-coercing-union-record + (let [schema {:type "record", + :name "myrecord", + :fields [{:name "myunion", + :type [{:type :null} + {:type "record", + :name "recordtype", + :fields [{:name "foo" + :type "boolean" + :default false}]}]}]} + serde (->serde (json/write-str schema))] + + (is (= {:myunion {:foo false}} + (round-trip serde "whatever" {:myunion {}}))))) + (deftest record-serde-test (let [serde (->serde complex-schema-str) valid-map {:string-field "hello"