Copilot commented on code in PR #13269:
URL: https://github.com/apache/apisix/pull/13269#discussion_r3116231983


##########
apisix/plugins/ai-proxy/merge.lua:
##########
@@ -0,0 +1,75 @@
+--
+-- 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.
+--
+
+--- Deep-merge helper for ai-proxy request body overrides.
+-- Semantics:
+--   * Both sides are plain objects (string-keyed tables) -> recursive merge.
+--   * Otherwise (scalar, array, type mismatch, cjson.empty_array/empty_object)
+--     -> patch value replaces target value wholesale.
+-- This matches RFC 7396 JSON Merge Patch minus null-deletion.
+
+local core = require("apisix.core")
+local pairs = pairs
+local next = next
+local type = type
+local getmetatable = getmetatable
+
+local _M = {}
+
+
+-- Returns true when tbl is a plain object (string keys only) that we should
+-- recurse into. Empty tables, arrays, and cjson sentinels are treated as
+-- "replace wholesale" to avoid ambiguity.
+local function is_plain_object(tbl)
+    if type(tbl) ~= "table" then
+        return false
+    end
+    local mt = getmetatable(tbl)
+    if mt == core.json.array_mt then
+        return false
+    end
+    local k = next(tbl)
+    if k == nil then
+        return true
+    end
+    return type(k) == "string"
+end
+
+
+local function deep_merge(target, patch, force)
+    if not is_plain_object(patch) then
+        return patch
+    end
+    if not is_plain_object(target) then
+        -- target is not an object but patch is; patch wins only if force or
+        -- target is nil (which the caller handles)
+        return patch

Review Comment:
   `deep_merge()` returns `patch` whenever `target` is not a plain object (even 
when `force` is false and `target` is non-nil). This can violate the documented 
“client wins unless force” behavior for type-mismatch cases (eg target scalar 
vs patch object) in the edge case where the root body (or a nested merge via a 
future refactor) is non-object. Consider honoring `force` here (eg only replace 
when `force` is true or `target` is nil) so non-force mode never overwrites an 
existing non-nil value on type mismatch.
   



##########
apisix/plugins/ai-protocols/init.lua:
##########
@@ -66,6 +67,17 @@ end
 
 
 
+--- Get the list of all registered protocol names.
+-- @return table Array of protocol names
+function _M.names()
+    local names = {}
+    for name in pairs(registered) do
+        names[#names + 1] = name
+    end

Review Comment:
   `names()` builds the returned array using `pairs(registered)`, which has 
non-deterministic iteration order in Lua. Since callers may use this for schema 
generation / enum lists, it’s safer to return a stable ordering (eg 
`table.sort(names)`) to avoid flaky outputs and make behavior reproducible 
across runs.
   



##########
apisix/plugins/ai-proxy/merge.lua:
##########
@@ -0,0 +1,75 @@
+--
+-- 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.
+--
+
+--- Deep-merge helper for ai-proxy request body overrides.
+-- Semantics:
+--   * Both sides are plain objects (string-keyed tables) -> recursive merge.
+--   * Otherwise (scalar, array, type mismatch, cjson.empty_array/empty_object)
+--     -> patch value replaces target value wholesale.
+-- This matches RFC 7396 JSON Merge Patch minus null-deletion.
+
+local core = require("apisix.core")
+local pairs = pairs
+local next = next
+local type = type
+local getmetatable = getmetatable
+
+local _M = {}
+
+
+-- Returns true when tbl is a plain object (string keys only) that we should
+-- recurse into. Empty tables, arrays, and cjson sentinels are treated as
+-- "replace wholesale" to avoid ambiguity.
+local function is_plain_object(tbl)
+    if type(tbl) ~= "table" then
+        return false
+    end
+    local mt = getmetatable(tbl)
+    if mt == core.json.array_mt then
+        return false
+    end
+    local k = next(tbl)
+    if k == nil then
+        return true

Review Comment:
   `is_plain_object()` treats empty tables as plain objects (`next(tbl) == nil` 
returns true), but the comment and merge semantics above say empty objects 
should be treated as “replace wholesale”. With the current logic, a patch like 
`{ stream_options = {} }` will *not* overwrite/clear an existing 
`stream_options` table even when `force` is true (because it recurses into an 
empty table and makes no assignments). Consider treating empty tables as 
non-objects for recursion (or special-casing empty patch tables) so that 
`force` mode can replace a field with `{}` and the behavior matches the 
documented semantics.
   



-- 
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