-
-
Notifications
You must be signed in to change notification settings - Fork 89
Description
Description
Content types containing characters that are special in Lua patterns (such as +, -, .) fail to match response Content-Type headers when used as keys in the contenttypes config option.
Steps to Reproduce
Add the following to your kulala config:
contenttypes = {
["application/hal+json"] = "application/json",
}Make a request to an endpoint that responds with Content-Type: application/hal+json. The response body will be treated as plain text instead of JSON.
The response buffer :set filetype? will return text.kulala_ui, when it should be json.kulala_ui.
Impact
The response body is not formatted.
This may also affect the built-in default mapping ["application/graphql-response+json"], which contains both + and -, both special characters in Lua patterns, meaning that mapping may also not work as intended. Though I have not tested this.
Root Cause
In
kulala.nvim/lua/kulala/internal_processing/init.lua
Lines 130 to 132 in 6656c9d
| local config_key = vim.iter(CONFIG.get().contenttypes):find(function(k, _) | |
| return content_type:match(k) | |
| end) |
contenttypes key is used directly as a Lua pattern.
Because k is passed to string.match as a pattern rather than a plain string, any key containing Lua pattern special characters (+, -, ., *, ?, [, ], ^, $, %, (, )) will not behave as expected.
For example, the key "application/hal+json" is interpreted as the pattern application/hal+json, where l+ means "one or more l characters".
Workaround
Escape special characters manually in your config keys using %
contenttypes = {
["application/hal%+json"] = "application/json",
}Suggested Fix
Escape keys with vim.pesc() before using them as patterns.
local config_key = vim.iter(CONFIG.get().contenttypes):find(function(k, _)
return content_type:match(vim.pesc(k))
end)I would be happy to raise a PR for this, but I wanted to check I'm not barking up the wrong tree first.
Request
Any request that responds with Content-Type: application/hal+json.
Here is an example from https://httpbin.org/
GET https://httpbin.org/response-headers?Content-Type=application/hal+jsonError
No error happens, but the response body is not formatted.
Health
System:
βΉ {OS} Darwin 25.3.0
βΉ {Neovim} version 0.12.0
βΉ {kulala.nvim} version 5.3.4
Tools:
β {cURL} found: /usr/bin/curl (version: 8.7.1)
β {gRPCurl} not found
β {websocat} not found
β {openssl} found: /opt/homebrew/bin/openssl (version: 3.6.1)
β {NPM} found: /Users/chris.robertson/.asdf/shims/npm (version: unknown)
Formatters:
β {application/json} formatter: jq .
β {application/javascript} formatter: prettier --stdin-filepath file.js
β {application/lua} formatter not found
β {application/graphql} formatter: prettier --stdin-filepath file.graphql
β {text/html} formatter: prettier --stdin-filepath file.html
β {application/graphql-response+json} formatter: jq .
β {application/xml} formatter: xmllint --format -
β {application/hal+json} formatter: jq .
User Config
{
additional_curl_options = { "--insecure" },
contenttypes = {
["application/hal+json"] = "application/json"
},
debug = 4,
default_env = "tunnel",
global_keymaps = true,
urlencode = "always"
}