Skip to content

Commit 908a842

Browse files
committed
Fix error when float variable value is greater 10^15-1
1 parent 17b642d commit 908a842

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

graphql/types.lua

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,19 @@ types.long = types.scalar({
318318
})
319319

320320
local function isFloat(value)
321-
return type(value) == 'number'
321+
if type(value) == 'number' then
322+
return true
323+
end
324+
325+
if type(value) == 'cdata' then
326+
if ffi.istype('int64_t', value) then
327+
return value >= -2^53 and value < 2^53
328+
elseif ffi.istype('uint64_t', value) then
329+
return value < 2^53
330+
end
331+
end
332+
333+
return false
322334
end
323335

324336
local function coerceFloat(value)

test/integration/graphql_test.lua

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,3 +2051,65 @@ g.test_propagate_defaults_to_callback = function()
20512051
t.assert_equals(errors, nil)
20522052
t.assert_items_equals(json.decode(data.prefix.test_mutation), result)
20532053
end
2054+
2055+
function g.test_float_variables()
2056+
local query = [[
2057+
query ($arg: Float) { test(arg: $arg) }
2058+
]]
2059+
2060+
local floats_positive = {
2061+
'9007199254740991',
2062+
'-9007199254740992',
2063+
'0',
2064+
'9007199254740991.1',
2065+
'-9007199254740992.1',
2066+
'99999999999999',
2067+
'100000000000000',
2068+
'-99999999999999',
2069+
'-100000000000000',
2070+
'1e1',
2071+
'-1e1',
2072+
'inf',
2073+
'-inf',
2074+
}
2075+
2076+
local floats_negative = {
2077+
{'9007199254740992', 'Wrong variable \"arg\" for the Scalar \"Float\"'},
2078+
{'-9007199254740993', 'Wrong variable \"arg\" for the Scalar \"Float\"'},
2079+
}
2080+
2081+
local function callback(_, args)
2082+
return args[1].value
2083+
end
2084+
2085+
local query_schema = {
2086+
['test'] = {
2087+
kind = types.float.nonNull,
2088+
arguments = {
2089+
arg = types.float,
2090+
},
2091+
resolve = callback,
2092+
}
2093+
}
2094+
2095+
-- Positive tests
2096+
for _, value in ipairs(floats_positive) do
2097+
t.assert_items_equals(
2098+
check_request(query, query_schema, nil, nil, {variables = json.decode('{"arg": '.. value .. '}')}),
2099+
{test = tonumber(value)}
2100+
)
2101+
end
2102+
t.assert_nan(
2103+
check_request(query, query_schema, nil, nil, {variables = json.decode('{"arg": nan}')}).test
2104+
)
2105+
2106+
-- Negative tests
2107+
for _, case in ipairs(floats_negative) do
2108+
t.assert_error_msg_equals(
2109+
case[2],
2110+
function()
2111+
check_request(query, query_schema, nil, nil, {variables = json.decode('{"arg": '.. case[1] .. '}')})
2112+
end
2113+
)
2114+
end
2115+
end

0 commit comments

Comments
 (0)