Skip to content
This repository has been archived by the owner on Feb 4, 2025. It is now read-only.

Commit

Permalink
Merge pull request #3 from reifyhealth/258-union-coercion-fix
Browse files Browse the repository at this point in the history
[FundingCircle#258] fix type union coercion error when record types come before ot…
  • Loading branch information
darin-reify authored Aug 23, 2022
2 parents 21226cb + c297ff3 commit 62807ab
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Unreleased

- Add `divert` stream operation
- Fix type union coercion fails when record types come before others

### [0.9.6] - [2022-08-01]

Expand Down
19 changes: 10 additions & 9 deletions src/jackdaw/serdes/avro.clj
Original file line number Diff line number Diff line change
Expand Up @@ -397,15 +397,16 @@
(defrecord RecordType [^Schema schema field->schema+coercion]
SchemaCoercion
(match-clj? [_ clj-map]
(let [[_ unknown-fields _] (clojure.data/diff (set (keys field->schema+coercion))
(set (keys clj-map)))]
(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)
(match-clj? field-coercion field-value))))
field->schema+coercion)
(empty? unknown-fields))))
(when clj-map
(let [[_ unknown-fields _] (clojure.data/diff (set (keys field->schema+coercion))
(set (keys clj-map)))]
(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)
(match-clj? field-coercion field-value))))
field->schema+coercion)
(empty? unknown-fields)))))

(match-avro? [_ avro-record]
(cond
Expand Down
4 changes: 2 additions & 2 deletions test/jackdaw/serdes/avro_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,10 @@
enum-schema {:name "enum"
:type "enum"
:symbols ["a" "b" "c"]}
avro-schema (parse-schema ["long"
avro-schema (parse-schema [record-1-schema
"long"
"string"
enum-schema
record-1-schema
record-2-schema
record-3-schema])
schema-type (schema-type avro-schema)
Expand Down

0 comments on commit 62807ab

Please sign in to comment.