fix(api): allow empty Lua table for nested dicts #22268

Problem:
The Lua-API bridge allows Dict params to be empty Lua (list) tables at
the function-signature level. But not for _nested_ Dicts, because they
are not modeled:
fae7540732/src/nvim/api/keysets.lua (L184)
Some API functions like nvim_cmd check for kObjectTypeDictionary and
don't handle the case of empty Lua tables (treated as "Array").

Solution:
Introduce VALIDATE_T_DICT and use it in places where
kObjectTypeDictionary was being checked directly.

fixes #21005
This commit is contained in:
Justin M. Keyes
2023-02-16 10:07:18 -05:00
committed by GitHub
parent bcae4af374
commit 09b3432eaf
5 changed files with 175 additions and 84 deletions

View File

@@ -3826,14 +3826,71 @@ describe('API', function()
meths.cmd({ cmd = "set", args = { "cursorline" } }, {})
eq(true, meths.get_option_value("cursorline", {}))
end)
it('validation', function()
eq("Invalid 'cmd': expected non-empty String",
pcall_err(meths.cmd, { cmd = ""}, {}))
eq("Invalid 'cmd': expected non-empty String",
pcall_err(meths.cmd, { cmd = {}}, {}))
eq("Invalid 'args': expected Array, got Boolean",
pcall_err(meths.cmd, { cmd = "set", args = true }, {}))
eq("Invalid 'magic': expected Dict, got Array",
pcall_err(meths.cmd, { cmd = "set", args = {}, magic = {} }, {}))
eq("Invalid command arg: expected non-whitespace",
pcall_err(meths.cmd, { cmd = "set", args = {' '}, }, {}))
eq("Invalid command arg: expected valid type, got Array",
pcall_err(meths.cmd, { cmd = "set", args = {{}}, }, {}))
eq("Wrong number of arguments",
pcall_err(meths.cmd, { cmd = "aboveleft", args = {}, }, {}))
eq("Command cannot accept bang: print",
pcall_err(meths.cmd, { cmd = "print", args = {}, bang = true }, {}))
eq("Command cannot accept range: set",
pcall_err(meths.cmd, { cmd = "set", args = {}, range = {1} }, {}))
eq("Invalid 'range': expected Array, got Boolean",
pcall_err(meths.cmd, { cmd = "print", args = {}, range = true }, {}))
eq("Invalid 'range': expected <=2 elements",
pcall_err(meths.cmd, { cmd = "print", args = {}, range = {1,2,3,4} }, {}))
eq("Invalid range element: expected non-negative Integer",
pcall_err(meths.cmd, { cmd = "print", args = {}, range = {-1} }, {}))
eq("Command cannot accept count: set",
pcall_err(meths.cmd, { cmd = "set", args = {}, count = 1 }, {}))
eq("Invalid 'count': expected non-negative Integer",
pcall_err(meths.cmd, { cmd = "print", args = {}, count = true }, {}))
eq("Invalid 'count': expected non-negative Integer",
pcall_err(meths.cmd, { cmd = "print", args = {}, count = -1 }, {}))
eq("Command cannot accept register: set",
pcall_err(meths.cmd, { cmd = "set", args = {}, reg = 'x' }, {}))
eq('Cannot use register "=',
pcall_err(meths.cmd, { cmd = "put", args = {}, reg = '=' }, {}))
eq("Invalid 'reg': expected single character, got xx",
pcall_err(meths.cmd, { cmd = "put", args = {}, reg = 'xx' }, {}))
-- Lua call allows empty {} for dict item.
eq('', exec_lua([[return vim.cmd{ cmd = "set", args = {}, magic = {} }]]))
eq('', exec_lua([[return vim.cmd{ cmd = "set", args = {}, mods = {} }]]))
-- Lua call does not allow non-empty list-like {} for dict item.
eq("Invalid 'magic': expected Dict, got Array",
pcall_err(exec_lua, [[return vim.cmd{ cmd = "set", args = {}, magic = { 'a' } }]]))
eq("Invalid key: 'bogus'",
pcall_err(exec_lua, [[return vim.cmd{ cmd = "set", args = {}, magic = { bogus = true } }]]))
eq("Invalid key: 'bogus'",
pcall_err(exec_lua, [[return vim.cmd{ cmd = "set", args = {}, mods = { bogus = true } }]]))
end)
it('captures output', function()
eq("foo", meths.cmd({ cmd = "echo", args = { '"foo"' } }, { output = true }))
end)
it('sets correct script context', function()
meths.cmd({ cmd = "set", args = { "cursorline" } }, {})
local str = meths.exec([[verbose set cursorline?]], true)
neq(nil, str:find("cursorline\n\tLast set from API client %(channel id %d+%)"))
end)
it('works with range', function()
insert [[
line1