Skip to content

Commit 31c9fdd

Browse files
olegrokTotktonada
authored andcommitted
Fix case when "long" type validation allows field to have a "string" type
Since 23be848 (Validate/long: final rewrite) "long" type validation started to handle string representation of numbers. This happens because "tonumber" function implicitly casts some strings to numbers. That allows to pass strings (e.g. "123") instead of "long". This patch fixes such behaviour and introduces corresponding test. Currently we will try to cast to number only fields that have "number" or "cdata" type. Closes #133
1 parent 3b2d264 commit 31c9fdd

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

avro_schema/frontend.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,10 @@ copy_data = function(stack, schema, data, visited)
772772
stack.remove_last()
773773
return data
774774
elseif schematype == 'long' then
775-
local n = tonumber(data)
775+
local n
776+
if type(data) == 'number' or type(data) == 'cdata' then
777+
n = tonumber(data)
778+
end
776779
if not n then
777780
stack.push(schema, data)
778781
error()

test/ddt_suite/validate.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ t {
133133
validate_error = 'Not a long: 9223372036854775808ULL'
134134
}
135135

136+
t {
137+
schema = '"long"',
138+
validate = '"42"',
139+
validate_error = 'Not a long: 42'
140+
}
141+
136142
-- note: IEEE 754 double precision floating-point numbers encode
137143
-- fraction with 52 bits, hence when the value is 2^63,
138144
-- the delta must be at least 2^11 (2048) to make a difference.

0 commit comments

Comments
 (0)