Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions graphql/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,7 @@ local function isLong(value)
end

if type(value) == 'cdata' then
if ffi.istype('int64_t', value) then
return true
elseif ffi.istype('uint64_t', value) then
return value < 2^63
end
return ffi.istype('int64_t', value) or ffi.istype('uint64_t', value)
end

return false
Expand All @@ -299,9 +295,7 @@ end
local function coerceLong(value)
if value ~= nil then
value = tonumber64(value)
if not isLong(value) then return end
end

return value
end

Expand Down
40 changes: 37 additions & 3 deletions test/integration/graphql_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function g.test_variables()
local function callback(_, args)
local result = ''
for _, tuple in ipairs(getmetatable(args).__index) do
result = result .. tuple.value
result = result .. tostring(tuple.value)
end
return result
end
Expand Down Expand Up @@ -184,10 +184,10 @@ function g.test_variables()
)

t.assert_error_msg_equals(
'Could not coerce value "18446744073709551614" to type "Long"',
'Could not coerce value "18446744073709551617" to type "Long"',
function()
check_request([[
query { test(arg: "", arg4: 18446744073709551614) }
query { test(arg: "", arg4: 18446744073709551617) }
]], query_schema)
end
)
Expand Down Expand Up @@ -2147,3 +2147,37 @@ function g.test_non_finite_float()
query_schema, nil, nil, {variables = variables})
end
end

function g.test_huge_cdata_number()
local query = [[
query ($x: Long!) { test(arg: $x) }
]]

local function callback(_, args)
return args[1].value
end

local query_schema = {
['test'] = {
kind = types.long.nonNull,
arguments = {
arg = types.long.nonNull,
},
resolve = callback,
}
}

local test_values = {
{4503599627370495ULL, "{\"test\":4503599627370495}"},
{9223372036854775807ULL, "{\"test\":9223372036854775807}"},
{-1LL, "{\"test\":-1}"},
{-1ULL, "{\"test\":18446744073709551615}"}}

for _, v in ipairs(test_values) do
local variables = {x = v[1]}
local res = check_request(query, query_schema, nil, nil, {variables = variables})
t.assert_type(res, 'table')
t.assert_equals(res.test, v[1])
t.assert_equals(json.encode(res), v[2])
end
end