From be76591d3d578a5cdcfb0ee1c578e539c5d2edea Mon Sep 17 00:00:00 2001 From: Cristiano Carvalho Date: Mon, 14 Sep 2026 10:58:18 -0300 Subject: [PATCH] fix(gh): don't send empty array as review input body vim.json.encode({}) serializes an empty Lua table as [], since Lua can't distinguish an empty array from an empty object. gh api then sends [] as the request body for actions like starting a PR review when the input table ends up empty (e.g. gh_start_review when item.headRefOid hasn't loaded yet), which GitHub rejects with a 422 since the endpoint requires a JSON object. Force empty input tables to encode as {} instead, using vim.empty_dict() as the marker. --- lua/snacks/gh/api.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lua/snacks/gh/api.lua b/lua/snacks/gh/api.lua index 83bd24b1b..b52d4b3ab 100644 --- a/lua/snacks/gh/api.lua +++ b/lua/snacks/gh/api.lua @@ -235,7 +235,13 @@ function M.request(cb, opts) cb(proc, data and data:find("%S") and proc:json() or nil) end, { args = args, - input = opts.input and vim.json.encode(opts.input) or nil, + -- `vim.json.encode({})` serializes an empty Lua table as `[]`, since Lua + -- can't distinguish an empty array from an empty object. `gh api` then + -- sends `[]` as the request body, which GitHub rejects for endpoints that + -- require a JSON object (e.g. creating a PR review with no fields set). + -- Force empty input tables to encode as `{}` instead. + input = opts.input and vim.json.encode(vim.tbl_isempty(opts.input) and vim.empty_dict() or opts.input) + or nil, on_error = opts.on_error, }) end