lib/avromatic/io/datum_reader.rb in avromatic-0.21.0 vs lib/avromatic/io/datum_reader.rb in avromatic-0.21.1

- old
+ new

@@ -18,10 +18,19 @@ if writers_schema.type_sym != :union && readers_schema.type_sym == :union rs_index = readers_schema.schemas.find_index do |s| self.class.match_schemas(writers_schema, s) end - union_info = { UNION_MEMBER_INDEX => rs_index } + optional = readers_schema.schemas.first.type_sym == :null + union_info = if readers_schema.schemas.size == 2 && optional + # Avromatic does not treat the union of null and 1 other type as a union + {} + elsif optional + # Avromatic does not treat the null of an optional field as part of the union + { UNION_MEMBER_INDEX => rs_index - 1 } + else + { UNION_MEMBER_INDEX => rs_index } + end return read_data(writers_schema, readers_schema.schemas[rs_index], decoder, union_info) if rs_index raise Avro::IO::SchemaMatchException.new(writers_schema, readers_schema) end