This is an automated email from the ASF dual-hosted git repository.

tjwp pushed a commit to branch branch-1.11
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/branch-1.11 by this push:
     new 88f8be154 AVRO-3773: [ruby] fix validator for decimal default (#2275)
88f8be154 is described below

commit 88f8be154ec9d2beee9537d1fbea696c6b82c404
Author: Rich <[email protected]>
AuthorDate: Wed Jun 14 15:57:21 2023 -0400

    AVRO-3773: [ruby] fix validator for decimal default (#2275)
---
 lang/ruby/lib/avro/schema.rb  | 12 ++++++++++--
 lang/ruby/test/test_schema.rb | 31 +++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/lang/ruby/lib/avro/schema.rb b/lang/ruby/lib/avro/schema.rb
index 4485d5e9e..444911de5 100644
--- a/lang/ruby/lib/avro/schema.rb
+++ b/lang/ruby/lib/avro/schema.rb
@@ -601,8 +601,16 @@ module Avro
                            else
                              type
                            end
-
-        Avro::SchemaValidator.validate!(type_for_default, default)
+        case type_for_default.logical_type
+        when DECIMAL_LOGICAL_TYPE
+          # https://avro.apache.org/docs/1.11.1/specification/#schema-record
+          # Default values for bytes and fixed fields are JSON strings, where 
Unicode code points 0-255 are mapped to unsigned 8-bit byte values 0-255
+          options = SchemaValidator::DEFAULT_VALIDATION_OPTIONS.dup
+          options[:encoded] = true
+          Avro::SchemaValidator.validate!(type_for_default, default, options)
+        else
+          Avro::SchemaValidator.validate!(type_for_default, default)
+        end
       rescue Avro::SchemaValidator::ValidationError => e
         raise Avro::SchemaParseError, "Error validating default for #{name}: 
#{e.message}"
       end
diff --git a/lang/ruby/test/test_schema.rb b/lang/ruby/test/test_schema.rb
index 802653010..258cd198c 100644
--- a/lang/ruby/test/test_schema.rb
+++ b/lang/ruby/test/test_schema.rb
@@ -612,6 +612,37 @@ class TestSchema < Test::Unit::TestCase
     assert_equal schema_hash, schema.to_avro
   end
 
+  def test_bytes_decimal_in_record
+    assert_nothing_raised do
+      hash_to_schema(
+        type: 'record',
+        name: 'account',
+        fields: [
+          { name: 'balance', type: 'bytes', logicalType: 'decimal', precision: 
9, scale: 2 }
+        ]
+      )
+    end
+  end
+
+  def test_bytes_decimal_with_default_in_record
+    assert_nothing_raised do
+      hash_to_schema(
+        type: 'record',
+        name: 'account',
+        fields: [
+          {
+            name: 'balance',
+            type: [
+              { type: 'bytes', logicalType: 'decimal', precision: 9, scale: 2 
},
+              'null'
+            ],
+            default: '\u00ff'
+          }
+        ]
+      )
+    end
+  end
+  
   def test_bytes_decimal_to_include_precision_scale
     schema = Avro::Schema.parse <<-SCHEMA
       {

Reply via email to