Dear Kong-ers
I’m currently developing a plugin where a configuration parameters (let’s call it param
) is an array of strings.
I have different ways to set the configuration parameters for this plugin:
- Using a single string containing the list of comma separated values. Within a curl command, it would be something like
--data “config.param=value1,value2,value3,value4”
- Or using the
[]
notation for defining item per item, as what I have seen in an example in the request-transformer plugin documentation (that is heavily using arrays of strings):--data “config.param[1]=value1” --data “config.param[2]=value2” --data “config.param[3]=value3 --data “config.param[4]=value4”
The issue is that:
- When using the first method, the order to the strings within the array is kept: the order of the items presents, at the end, in the
config.param
Lua table accessible in thehandler
function of the plugin is the same as in the curl command sent to configure the plugin. - But when using the second method, the order is NOT respected: the items in the
config.param
Lua table are scrambled
and of course, the order to the items is (very) important for me
I dug into Kong’s code up to the Lapis routes attachment (within kong/api/init.lua
), dumping the content of the config
object at the earlier step as possible, to see that the order was already wrong at that level:
dump(): {
api_name_or_id = "myApi",
name = "myPlugin",
["config.param"] = {
["4"] = "value4",
["3"] = "value3"
["1"] = "value1",
["2"] = "value2",
}
}
Then, obviously, after transformation into the kong/apis/init.lua/parse_params
function, the order is still wrong:
dump(): {
api_name_or_id = "myApi",
config = {
param = {
"value4",
"value3",
"value1",
"value2"
}
},
name = "myPlugin"
}
and this is what I get in the handler
.
So where does the scrambling happens ? Well, it looks it is inside the Lapis framework… more precisely here: https://github.com/leafo/lapis/blob/master/lapis/application.lua#L449
Tracing the json string before this line, the order within the string is still correct; tracing the Lua table after this call, the order is incorrect !
String before calling cjson
call:
dump(): "{\"name\": \"myPlugin\", \"config.param[1]\": \"value1\", \"config.param[2]\": \"value2\", \"config.param[3]\": \"value3\", \"config.param[4]\": \"value4\"}"
I know that my issue is then not directly inside Kong, but I would get the advice from Kong and Lua experts:
- Is the usage of “[]” in the curl/httpie commands for plugin configuration a recognized way for inputting arrays of string, or is it just a “trick” to be forbidden? Is there alternate ways to input arrays of strings, other than the two I presented above, and that I can test?
- Is it normal that
cjson
does not respect the order of the original string using the [] notation ? Is there any way to enfore the order withincjson
?
Thanks for your valuable help.