From f64215eea3403c1a161caefbbb0da3a406b3e3a8 Mon Sep 17 00:00:00 2001 From: Dave Martin Date: Mon, 15 Jan 2024 14:39:59 +0000 Subject: [PATCH 1/2] Fix for nil or false default values --- src/jackdaw/serdes/avro.clj | 2 +- test/jackdaw/serdes/avro_test.clj | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) 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" From c85fbac6bef8db252c7ed8c927613ab960302bf9 Mon Sep 17 00:00:00 2001 From: Dave Martin Date: Mon, 15 Jan 2024 16:14:20 +0000 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) 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.