mirror of
https://github.com/neovim/neovim.git
synced 2024-12-23 20:55:18 -07:00
feat(rpc): allow empty string key in msgpack => Vim conversion
Problem: Sincee057b38e70
#20757 we support empty key in JSON encode/decode, but we don't allow it in RPC object => Vim dict conversion. But empty string is a valid key in Vim dicts and the msgpack spec. Empty string key was disallowed in7c01d5ff92
(2014) but that commit/PR doesn't explicitly discuss it, so presumably it was a "seems reasonable" decision (or Vimscript didn't allow empty keys until later). Solution: Remove the check in `object_to_vim()`. Note that `tv_dict_item_alloc_len` will invoke `memcpy(…, 0)` but that's allowed by the C spec: https://stackoverflow.com/a/3751937/152142
This commit is contained in:
parent
5fa88a7ae1
commit
cca6c4c698
@ -328,15 +328,6 @@ bool object_to_vim(Object obj, typval_T *tv, Error *err)
|
|||||||
for (uint32_t i = 0; i < obj.data.dictionary.size; i++) {
|
for (uint32_t i = 0; i < obj.data.dictionary.size; i++) {
|
||||||
KeyValuePair item = obj.data.dictionary.items[i];
|
KeyValuePair item = obj.data.dictionary.items[i];
|
||||||
String key = item.key;
|
String key = item.key;
|
||||||
|
|
||||||
if (key.size == 0) {
|
|
||||||
api_set_error(err, kErrorTypeValidation,
|
|
||||||
"Empty dictionary keys aren't allowed");
|
|
||||||
// cleanup
|
|
||||||
tv_dict_free(dict);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
dictitem_T *const di = tv_dict_item_alloc(key.data);
|
dictitem_T *const di = tv_dict_item_alloc(key.data);
|
||||||
|
|
||||||
if (!object_to_vim(item.value, &di->di_tv, err)) {
|
if (!object_to_vim(item.value, &di->di_tv, err)) {
|
||||||
|
@ -1319,6 +1319,10 @@ describe('API', function()
|
|||||||
eq("Key not found: lua", pcall_err(meths.del_var, 'lua'))
|
eq("Key not found: lua", pcall_err(meths.del_var, 'lua'))
|
||||||
meths.set_var('lua', 1)
|
meths.set_var('lua', 1)
|
||||||
|
|
||||||
|
-- Empty keys are allowed in Vim dicts (and msgpack).
|
||||||
|
nvim('set_var', 'dict_empty_key', {[''] = 'empty key'})
|
||||||
|
eq({[''] = 'empty key'}, nvim('get_var', 'dict_empty_key'))
|
||||||
|
|
||||||
-- Set locked g: var.
|
-- Set locked g: var.
|
||||||
command('lockvar lua')
|
command('lockvar lua')
|
||||||
eq('Key is locked: lua', pcall_err(meths.del_var, 'lua'))
|
eq('Key is locked: lua', pcall_err(meths.del_var, 'lua'))
|
||||||
@ -1983,7 +1987,7 @@ describe('API', function()
|
|||||||
it('errors when context dictionary is invalid', function()
|
it('errors when context dictionary is invalid', function()
|
||||||
eq('E474: Failed to convert list to msgpack string buffer',
|
eq('E474: Failed to convert list to msgpack string buffer',
|
||||||
pcall_err(nvim, 'load_context', { regs = { {} }, jumps = { {} } }))
|
pcall_err(nvim, 'load_context', { regs = { {} }, jumps = { {} } }))
|
||||||
eq("Empty dictionary keys aren't allowed",
|
eq('E474: Failed to convert list to msgpack string buffer',
|
||||||
pcall_err(nvim, 'load_context', { regs = { { [''] = '' } } }))
|
pcall_err(nvim, 'load_context', { regs = { { [''] = '' } } }))
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
@ -576,8 +576,8 @@ describe('json_encode() function', function()
|
|||||||
eq('{}', eval('json_encode({})'))
|
eq('{}', eval('json_encode({})'))
|
||||||
eq('{"d": []}', funcs.json_encode({d={}}))
|
eq('{"d": []}', funcs.json_encode({d={}}))
|
||||||
eq('{"d": [], "e": []}', funcs.json_encode({d={}, e={}}))
|
eq('{"d": [], "e": []}', funcs.json_encode({d={}, e={}}))
|
||||||
-- Empty keys not allowed (yet?) in object_to_vim() (since 7c01d5ff9286). #25564
|
-- Empty keys are allowed per JSON spec (and Vim dicts, and msgpack).
|
||||||
-- eq('{"": []}', funcs.json_encode({['']={}}))
|
eq('{"": []}', funcs.json_encode({['']={}}))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('cannot dump generic mapping with generic mapping keys and values',
|
it('cannot dump generic mapping with generic mapping keys and values',
|
||||||
|
Loading…
Reference in New Issue
Block a user