Skip to content

Commit 126b886

Browse files
KhatskevichTotktonada
authored andcommitted
Fix nil-union validate
Closes #113
1 parent 083b4a4 commit 126b886

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

avro_schema/frontend.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ copy_data = function(stack, schema, data, visited)
743743
-- Due to this technique, a error message is often misleading,
744744
-- e.x. "attempt to perform arithmetic on a string value". Unless
745745
-- a message starts with '@', we replace it (see copy_data_eh).
746-
if schema.nullable and (data == null or data == nil) then
746+
if schema.nullable and (data == nil) then
747747
return null
748748
end
749749
if schematype == 'null' then
@@ -832,6 +832,8 @@ copy_data = function(stack, schema, data, visited)
832832
else
833833
stack.push(schema, data)
834834
local frame_no = stack.len
835+
-- Replace nil -> NULL to allow it to be a key in a table.
836+
data = data ~= nil and data or null
835837
if visited[data] then
836838
error('@Infinite loop detected in the data', 0)
837839
end

test/api_tests/var.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local msgpack = require('msgpack')
55

66
local test = tap.test('api-tests')
77

8-
test:plan(53)
8+
test:plan(55)
99

1010
test:is_deeply({schema.create()}, {false, 'Unknown Avro type: nil'},
1111
'error unknown type')
@@ -387,5 +387,13 @@ test:is_deeply(compiled.get_types(),
387387
"union_type","union_value", "array*","map","fixed*"},
388388
"compiled.get_types")
389389

390+
-- gh-113: nil and box.NULL are treated differently in validate of the union
391+
local ok, handle = schema.create({'int', 'null'})
392+
local ok, data = schema.validate(handle, nil)
393+
assert(ok, data)
394+
test:is(data == nil and type(data) == 'cdata', true, 'null returned')
395+
local ok, data = schema.validate(handle, msgpack.NULL)
396+
test:is(data == nil and type(data) == 'cdata', true, 'null returned')
397+
390398
test:check()
391399
os.exit(test.planned == test.total and test.failed == 0 and 0 or -1)

0 commit comments

Comments
 (0)