Skip to content

Commit 350e2ec

Browse files
committed
Fix silently cast huge cdata numbers to null
1 parent 58b64a6 commit 350e2ec

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

graphql/types.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ local function isLong(value)
289289
if ffi.istype('int64_t', value) then
290290
return true
291291
elseif ffi.istype('uint64_t', value) then
292-
return value < 2^63
292+
return true
293293
end
294294
end
295295

@@ -299,9 +299,7 @@ end
299299
local function coerceLong(value)
300300
if value ~= nil then
301301
value = tonumber64(value)
302-
if not isLong(value) then return end
303302
end
304-
305303
return value
306304
end
307305

test/integration/graphql_test.lua

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function g.test_variables()
9696
local function callback(_, args)
9797
local result = ''
9898
for _, tuple in ipairs(getmetatable(args).__index) do
99-
result = result .. tuple.value
99+
result = result .. tostring(tuple.value)
100100
end
101101
return result
102102
end
@@ -184,10 +184,10 @@ function g.test_variables()
184184
)
185185

186186
t.assert_error_msg_equals(
187-
'Could not coerce value "18446744073709551614" to type "Long"',
187+
'Could not coerce value "18446744073709551617" to type "Long"',
188188
function()
189189
check_request([[
190-
query { test(arg: "", arg4: 18446744073709551614) }
190+
query { test(arg: "", arg4: 18446744073709551617) }
191191
]], query_schema)
192192
end
193193
)
@@ -2147,3 +2147,37 @@ function g.test_non_finite_float()
21472147
query_schema, nil, nil, {variables = variables})
21482148
end
21492149
end
2150+
2151+
function g.test_huge_cdata_number()
2152+
local query = [[
2153+
query ($x: Long!) { test(arg: $x) }
2154+
]]
2155+
2156+
local function callback(_, args)
2157+
return args[1].value
2158+
end
2159+
2160+
local query_schema = {
2161+
['test'] = {
2162+
kind = types.long.nonNull,
2163+
arguments = {
2164+
arg = types.long.nonNull,
2165+
},
2166+
resolve = callback,
2167+
}
2168+
}
2169+
2170+
local test_values = {
2171+
{4503599627370495ULL, "{\"test\":4503599627370495}"},
2172+
{9223372036854775807ULL, "{\"test\":9223372036854775807}"},
2173+
{-1LL, "{\"test\":-1}"},
2174+
{-1ULL, "{\"test\":18446744073709551615}"}}
2175+
2176+
for _, v in ipairs(test_values) do
2177+
local variables = {x = v[1]}
2178+
local res = check_request(query, query_schema, nil, nil, {variables = variables})
2179+
t.assert_type(res, 'table')
2180+
t.assert_equals(res.test, v[1])
2181+
t.assert_equals(json.encode(res), v[2])
2182+
end
2183+
end

0 commit comments

Comments
 (0)