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

Fix avro serialization of union types #367

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/jackdaw/serdes/avro.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
Expand Down
15 changes: 15 additions & 0 deletions test/jackdaw/serdes/avro_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down