nic-6443 commented on code in PR #13386:
URL: https://github.com/apache/apisix/pull/13386#discussion_r3263264768


##########
apisix/core/request_json.lua:
##########
@@ -0,0 +1,129 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local config_local = require("apisix.core.config_local")
+local log = require("apisix.core.log")
+local core_json = require("apisix.core.json")
+local require = require
+local pcall = pcall
+
+
+local DEFAULT_JSON_LIB = "qjson"
+local json_libs = {
+    cjson = true,
+    qjson = true,
+    simdjson = true,
+}
+
+local qjson
+local simdjson_parser
+
+local _M = {}
+
+
+local function configured_json_lib()
+    local local_conf = config_local.local_conf()
+    local name = local_conf and local_conf.apisix
+                 and local_conf.apisix.request_body_json_lib
+                 or DEFAULT_JSON_LIB
+
+    if not json_libs[name] then
+        log.warn("invalid apisix.request_body_json_lib: ", name,
+                 ", fallback to ", DEFAULT_JSON_LIB)
+        return DEFAULT_JSON_LIB

Review Comment:
   Addressed in ff40b98: invalid config and dependency load failures are warned 
once per worker instead of once per decode/encode call.



##########
t/core/request.t:
##########
@@ -542,3 +545,161 @@ same table: true
 decode_count: 1
 after set_body model: claude
 decode_count: 2
+
+
+
+=== TEST 18: request_json selects configured JSON library
+--- config
+    location /t {
+        content_by_lua_block {
+            local config_local = require("apisix.core.config_local")
+            local orig_local_conf = config_local.local_conf
+            local orig_qjson = package.loaded["qjson"]
+            local orig_simdjson = package.loaded["resty.simdjson"]
+            local orig_request_json = 
package.loaded["apisix.core.request_json"]
+
+            package.loaded["qjson"] = {
+                decode = function()
+                    return {lib = "qjson"}
+                end,
+                encode = function(data)
+                    return "qjson:" .. data.lib
+                end,
+            }
+
+            package.loaded["resty.simdjson"] = {
+                new = function()
+                    return {
+                        decode = function()
+                            return {lib = "simdjson"}
+                        end,
+                    }
+                end,
+            }
+
+            local function load_with(lib)
+                config_local.local_conf = function()
+                    return {apisix = {request_body_json_lib = lib}}
+                end
+                package.loaded["apisix.core.request_json"] = nil
+                return require("apisix.core.request_json")
+            end
+
+            local request_json = load_with("qjson")
+            local decoded = request_json.decode("{}")
+            local encoded = request_json.encode({lib = "body"})
+            ngx.say("qjson decode: ", decoded.lib)
+            ngx.say("qjson encode: ", encoded)
+
+            request_json = load_with("simdjson")
+            decoded = request_json.decode("{}")
+            encoded = request_json.encode({lib = "body"})
+            ngx.say("simdjson decode: ", decoded.lib)
+            ngx.say("simdjson encode: ", encoded)
+
+            request_json = load_with("cjson")
+            decoded = request_json.decode('{"lib":"cjson"}')
+            encoded = request_json.encode({lib = "body"})
+            ngx.say("cjson decode: ", decoded.lib)
+            ngx.say("cjson encode: ", encoded)
+
+            config_local.local_conf = orig_local_conf
+            package.loaded["apisix.core.request_json"] = orig_request_json
+            package.loaded["qjson"] = orig_qjson
+            package.loaded["resty.simdjson"] = orig_simdjson
+        }
+    }
+--- response_body
+qjson decode: qjson
+qjson encode: qjson:body
+simdjson decode: simdjson
+simdjson encode: {"lib":"body"}
+cjson decode: cjson
+cjson encode: {"lib":"body"}
+
+
+
+=== TEST 19: ai transport encoders use request_json
+--- config
+    location /t {
+        content_by_lua_block {
+            local request_json = require("apisix.core.request_json")
+            local orig_encode = request_json.encode
+            local orig_http = package.loaded["resty.http"]
+            local orig_aws_config = package.loaded["resty.aws.config"]
+            local orig_aws = package.loaded["resty.aws"]
+            local orig_sign = package.loaded["resty.aws.request.sign"]
+            local orig_transport = 
package.loaded["apisix.plugins.ai-transport.http"]
+            local orig_auth_aws = 
package.loaded["apisix.plugins.ai-transport.auth-aws"]
+            request_json.encode = function()
+                return "encoded-by-request-json"
+            end
+
+            package.loaded["resty.http"] = {
+                new = function()
+                    return {
+                        set_timeout = function() end,
+                        connect = function() return true end,
+                        request = function(_, params)
+                            ngx.say("http body: ", params.body)
+                            return {headers = {}, status = 200}
+                        end,
+                        close = function() end,
+                    }
+                end,
+            }
+
+            package.loaded["apisix.plugins.ai-transport.http"] = nil
+            local transport = require("apisix.plugins.ai-transport.http")
+            local res, err = transport.request({
+                host = "127.0.0.1",
+                port = 80,
+                path = "/",
+                body = {model = "test"},
+            }, 1000)
+            if not res then
+                ngx.say(err)
+            end
+
+            package.loaded["resty.aws.config"] = {}
+            package.loaded["resty.aws"] = function()
+                return {
+                    Credentials = function(_, opts)
+                        return opts
+                    end,
+                }
+            end
+            package.loaded["resty.aws.request.sign"] = function(_, req)
+                ngx.say("aws body: ", req.body)
+                return {headers = {Authorization = "signed"}}
+            end
+
+            package.loaded["apisix.plugins.ai-transport.auth-aws"] = nil
+            local auth_aws = require("apisix.plugins.ai-transport.auth-aws")
+            local sign_err = auth_aws.sign_request({
+                method = "POST",
+                host = "bedrock-runtime.us-east-1.amazonaws.com",
+                port = 443,
+                path = "/model/test/converse",
+                headers = {},
+                body = {model = "test"},
+            }, {
+                access_key_id = "ak",
+                secret_access_key = "sk",
+            }, "us-east-1")
+            if sign_err then
+                ngx.say(sign_err)
+            end
+
+            request_json.encode = orig_encode
+            package.loaded["resty.http"] = orig_http
+            package.loaded["resty.aws.config"] = orig_aws_config
+            package.loaded["resty.aws"] = orig_aws
+            package.loaded["resty.aws.request.sign"] = orig_sign
+            package.loaded["apisix.plugins.ai-transport.http"] = orig_transport
+            package.loaded["apisix.plugins.ai-transport.auth-aws"] = 
orig_auth_aws
+        }
+    }
+--- response_body
+http body: encoded-by-request-json
+aws body: encoded-by-request-json

Review Comment:
   Addressed in ff40b98: TEST 19 now snapshots and restores 
package.loaded['apisix.core.request_json'] as well.



##########
t/core/request.t:
##########
@@ -542,3 +545,161 @@ same table: true
 decode_count: 1
 after set_body model: claude
 decode_count: 2
+
+
+
+=== TEST 18: request_json selects configured JSON library
+--- config
+    location /t {
+        content_by_lua_block {
+            local config_local = require("apisix.core.config_local")
+            local orig_local_conf = config_local.local_conf
+            local orig_qjson = package.loaded["qjson"]
+            local orig_simdjson = package.loaded["resty.simdjson"]
+            local orig_request_json = 
package.loaded["apisix.core.request_json"]
+
+            package.loaded["qjson"] = {
+                decode = function()
+                    return {lib = "qjson"}
+                end,
+                encode = function(data)
+                    return "qjson:" .. data.lib
+                end,
+            }
+
+            package.loaded["resty.simdjson"] = {
+                new = function()
+                    return {
+                        decode = function()
+                            return {lib = "simdjson"}
+                        end,
+                    }
+                end,
+            }
+
+            local function load_with(lib)
+                config_local.local_conf = function()
+                    return {apisix = {request_body_json_lib = lib}}
+                end
+                package.loaded["apisix.core.request_json"] = nil
+                return require("apisix.core.request_json")
+            end
+
+            local request_json = load_with("qjson")
+            local decoded = request_json.decode("{}")
+            local encoded = request_json.encode({lib = "body"})
+            ngx.say("qjson decode: ", decoded.lib)
+            ngx.say("qjson encode: ", encoded)
+
+            request_json = load_with("simdjson")
+            decoded = request_json.decode("{}")
+            encoded = request_json.encode({lib = "body"})
+            ngx.say("simdjson decode: ", decoded.lib)
+            ngx.say("simdjson encode: ", encoded)
+
+            request_json = load_with("cjson")
+            decoded = request_json.decode('{"lib":"cjson"}')
+            encoded = request_json.encode({lib = "body"})
+            ngx.say("cjson decode: ", decoded.lib)
+            ngx.say("cjson encode: ", encoded)
+
+            config_local.local_conf = orig_local_conf
+            package.loaded["apisix.core.request_json"] = orig_request_json
+            package.loaded["qjson"] = orig_qjson
+            package.loaded["resty.simdjson"] = orig_simdjson

Review Comment:
   I left this as a direct Test::Nginx block to keep the test small. The test 
snapshots/restores all touched package.loaded/package.preload entries on the 
normal path, and a failure inside the block fails the case immediately; adding 
a custom finally wrapper here would make the test much harder to read for 
limited benefit.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to