lib/avro/schema_compatibility.rb in avro-1.9.2 vs lib/avro/schema_compatibility.rb in avro-1.10.0
- old
+ new
@@ -26,14 +26,12 @@
def self.mutual_read?(writers_schema, readers_schema)
Checker.new.mutual_read?(writers_schema, readers_schema)
end
# Perform a basic check that a datum written with the writers_schema could
- # be read using the readers_schema. This check only includes matching the types,
- # including schema promotion, and matching the full name for named types.
- # Aliases for named types are not supported here, and the ruby implementation
- # of Avro in general does not include support for aliases.
+ # be read using the readers_schema. This check includes matching the types,
+ # including schema promotion, and matching the full name (including aliases) for named types.
def self.match_schemas(writers_schema, readers_schema)
w_type = writers_schema.type_sym
r_type = readers_schema.type_sym
# This conditional is begging for some OO love.
@@ -44,20 +42,20 @@
if w_type == r_type
return true if Schema::PRIMITIVE_TYPES_SYM.include?(r_type)
case r_type
when :record
- return writers_schema.fullname == readers_schema.fullname
+ return readers_schema.match_fullname?(writers_schema.fullname)
when :error
- return writers_schema.fullname == readers_schema.fullname
+ return readers_schema.match_fullname?(writers_schema.fullname)
when :request
return true
when :fixed
- return writers_schema.fullname == readers_schema.fullname &&
+ return readers_schema.match_fullname?(writers_schema.fullname) &&
writers_schema.size == readers_schema.size
when :enum
- return writers_schema.fullname == readers_schema.fullname
+ return readers_schema.match_fullname?(writers_schema.fullname)
when :map
return match_schemas(writers_schema.values, readers_schema.values)
when :array
return match_schemas(writers_schema.items, readers_schema.items)
end
@@ -116,12 +114,12 @@
when :array
full_match_schemas(writers_schema.items, readers_schema.items)
when :union
match_union_schemas(writers_schema, readers_schema)
when :enum
- # reader's symbols must contain all writer's symbols
- (writers_schema.symbols - readers_schema.symbols).empty?
+ # reader's symbols must contain all writer's symbols or reader has default
+ (writers_schema.symbols - readers_schema.symbols).empty? || !readers_schema.default.nil?
else
if writers_schema.type_sym == :union && writers_schema.schemas.size == 1
full_match_schemas(writers_schema.schemas.first, readers_schema)
else
false
@@ -146,10 +144,17 @@
writer_fields_hash = writers_schema.fields_hash
readers_schema.fields.each do |field|
if writer_fields_hash.key?(field.name)
return false unless full_match_schemas(writer_fields_hash[field.name].type, field.type)
else
- return false unless field.default?
+ names = writer_fields_hash.keys & field.alias_names
+ if names.size > 1
+ return false
+ elsif names.size == 1
+ return false unless full_match_schemas(writer_fields_hash[names.first].type, field.type)
+ else
+ return false unless field.default?
+ end
end
end
return true
end