Skip to content

Commit

Permalink
AVRO-1848: Ruby: Fix handling of falsey default values.
Browse files Browse the repository at this point in the history
  • Loading branch information
theturtle32 authored and rdblue committed Sep 4, 2016
1 parent 17cfe99 commit d7e1231
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ Trunk (not yet released)

AVRO-1888: Java: Fix single-record encoding marker check. (blue)

AVRO-1848: Ruby: Fix handling of falsey default values.
(Brian McKelvey via blue)

Avro 1.8.1 (14 May 2016)

INCOMPATIBLE CHANGES
Expand Down
4 changes: 4 additions & 0 deletions lang/ruby/lib/avro/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@ def read_record(writers_schema, readers_schema, decoder)
end

def read_default_value(field_schema, default_value)
if default_value == :no_default
raise AvroError, "Missing data for #{field_schema} with no default"
end

# Basically a JSON Decoder?
case field_schema.type_sym
when :null
Expand Down
6 changes: 3 additions & 3 deletions lang/ruby/lib/avro/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def self.make_field_objects(field_data, names, namespace=nil)
if field.respond_to?(:[]) # TODO(jmhodges) wtffffff
type = field['type']
name = field['name']
default = field['default']
default = field.key?('default') ? field['default'] : :no_default
order = field['order']
new_field = Field.new(type, name, default, order, names, namespace)
# make sure field name has not been used yet
Expand Down Expand Up @@ -363,7 +363,7 @@ def to_avro(names=Set.new)
class Field < Schema
attr_reader :type, :name, :default, :order

def initialize(type, name, default=nil, order=nil, names=nil, namespace=nil)
def initialize(type, name, default=:no_default, order=nil, names=nil, namespace=nil)
@type = subparse(type, names, namespace)
@name = name
@default = default
Expand All @@ -372,7 +372,7 @@ def initialize(type, name, default=nil, order=nil, names=nil, namespace=nil)

def to_avro(names=Set.new)
{'name' => name, 'type' => type.to_avro(names)}.tap do |avro|
avro['default'] = default if default
avro['default'] = default unless default == :no_default
avro['order'] = order if order
end
end
Expand Down
21 changes: 21 additions & 0 deletions lang/ruby/test/test_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,24 @@ def test_schema_promotion
end
private

def check_no_default(schema_json)
actual_schema = '{"type": "record", "name": "Foo", "fields": []}'
actual = Avro::Schema.parse(actual_schema)

expected_schema = <<EOS
{"type": "record",
"name": "Foo",
"fields": [{"name": "f", "type": #{schema_json}}]}
EOS
expected = Avro::Schema.parse(expected_schema)

reader = Avro::IO::DatumReader.new(actual, expected)
assert_raise Avro::AvroError do
value = reader.read(Avro::IO::BinaryDecoder.new(StringIO.new))
assert_not_equal(value, :no_default) # should never return this
end
end

def check_default(schema_json, default_json, default_value)
actual_schema = '{"type": "record", "name": "Foo", "fields": []}'
actual = Avro::Schema.parse(actual_schema)
Expand Down Expand Up @@ -381,6 +399,9 @@ def check(str)

# test writing of data to file
check_datafile(schema)

# check that AvroError is raised when there is no default
check_no_default(str)
end

def checkser(schm, randomdata)
Expand Down
17 changes: 17 additions & 0 deletions lang/ruby/test/test_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,21 @@ def test_unknown_named_type

assert_equal '"MissingType" is not a schema we know about.', error.message
end

def test_to_avro_handles_falsey_defaults
schema = Avro::Schema.parse <<-SCHEMA
{"type": "record", "name": "Record", "namespace": "my.name.space",
"fields": [
{"name": "is_usable", "type": "boolean", "default": false}
]
}
SCHEMA

assert_equal schema.to_avro, {
'type' => 'record', 'name' => 'Record', 'namespace' => 'my.name.space',
'fields' => [
{'name' => 'is_usable', 'type' => 'boolean', 'default' => false}
]
}
end
end

0 comments on commit d7e1231

Please sign in to comment.