avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject [09/35] avro git commit: AVRO-1848: Ruby: Fix handling of falsey default values.
Date Sat, 05 Nov 2016 20:20:26 GMT
AVRO-1848: Ruby: Fix handling of falsey default values.


Project: http://git-wip-us.apache.org/repos/asf/avro/repo
Commit: http://git-wip-us.apache.org/repos/asf/avro/commit/b5503672
Tree: http://git-wip-us.apache.org/repos/asf/avro/tree/b5503672
Diff: http://git-wip-us.apache.org/repos/asf/avro/diff/b5503672

Branch: refs/heads/branch-1.8
Commit: b55036724d220082f54cdb0060b3926314565da4
Parents: fa46c97
Author: Brian McKelvey <theturtle32@gmail.com>
Authored: Thu May 19 15:43:58 2016 -0700
Committer: Ryan Blue <blue@apache.org>
Committed: Sat Nov 5 13:15:03 2016 -0700

----------------------------------------------------------------------
 CHANGES.txt                   |  3 +++
 lang/ruby/lib/avro/io.rb      |  4 ++++
 lang/ruby/lib/avro/schema.rb  |  6 +++---
 lang/ruby/test/test_io.rb     | 21 +++++++++++++++++++++
 lang/ruby/test/test_schema.rb | 17 +++++++++++++++++
 5 files changed, 48 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/avro/blob/b5503672/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 9e73997..0490e86 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -29,6 +29,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

http://git-wip-us.apache.org/repos/asf/avro/blob/b5503672/lang/ruby/lib/avro/io.rb
----------------------------------------------------------------------
diff --git a/lang/ruby/lib/avro/io.rb b/lang/ruby/lib/avro/io.rb
index 1c10c4a..22beea2 100644
--- a/lang/ruby/lib/avro/io.rb
+++ b/lang/ruby/lib/avro/io.rb
@@ -407,6 +407,10 @@ module Avro
       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

http://git-wip-us.apache.org/repos/asf/avro/blob/b5503672/lang/ruby/lib/avro/schema.rb
----------------------------------------------------------------------
diff --git a/lang/ruby/lib/avro/schema.rb b/lang/ruby/lib/avro/schema.rb
index ba6067a..8e345e3 100644
--- a/lang/ruby/lib/avro/schema.rb
+++ b/lang/ruby/lib/avro/schema.rb
@@ -211,7 +211,7 @@ module Avro
           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
@@ -363,7 +363,7 @@ module Avro
     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
@@ -372,7 +372,7 @@ module Avro
 
       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

http://git-wip-us.apache.org/repos/asf/avro/blob/b5503672/lang/ruby/test/test_io.rb
----------------------------------------------------------------------
diff --git a/lang/ruby/test/test_io.rb b/lang/ruby/test/test_io.rb
index 7dca327..153cb94 100644
--- a/lang/ruby/test/test_io.rb
+++ b/lang/ruby/test/test_io.rb
@@ -342,6 +342,24 @@ EOS
   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)
@@ -381,6 +399,9 @@ EOS
 
     # 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)

http://git-wip-us.apache.org/repos/asf/avro/blob/b5503672/lang/ruby/test/test_schema.rb
----------------------------------------------------------------------
diff --git a/lang/ruby/test/test_schema.rb b/lang/ruby/test/test_schema.rb
index 0668cf2..1643307 100644
--- a/lang/ruby/test/test_schema.rb
+++ b/lang/ruby/test/test_schema.rb
@@ -143,4 +143,21 @@ class TestSchema < Test::Unit::TestCase
 
     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


Mime
View raw message