Skip to content

Commit 28fca89

Browse files
committed
address review: detect object branches via collect_types
CodeRabbit pointed out that the local branch_has_object helper would miss object branches expressed via composition (e.g. anyOf:[{allOf:[{type:object,...}]}, ...]). Replaced the ad-hoc check with collect_types(b)['object'], which already recursively walks anyOf/oneOf/allOf and handles union type arrays. Added a regression test using an allOf-wrapped object branch.
1 parent 8d3c87b commit 28fca89

2 files changed

Lines changed: 24 additions & 8 deletions

File tree

lib/resty/openapi_validator/params.lua

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,7 @@ local function deserialize_param(raw_value, param, query_args)
263263
and (schema.anyOf or schema.oneOf) then
264264
local branches = schema.anyOf or schema.oneOf
265265
local function branch_has_object(b)
266-
local bt = b and b.type
267-
if bt == "object" then return true end
268-
if type(bt) == "table" then
269-
for _, t in ipairs(bt) do
270-
if t == "object" then return true end
271-
end
272-
end
273-
return false
266+
return collect_types(b)["object"] == true
274267
end
275268
for _, branch in ipairs(branches) do
276269
if branch_has_object(branch) then

t/unit/test_params.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,4 +292,27 @@ T.describe("params: deepObject anyOf nullable object branch", function()
292292
T.ok(not errs or #errs == 0, "no errors")
293293
end)
294294

295+
-- Object branch expressed via composition (allOf) must also be detected, so
296+
-- parse_deep_object is invoked and the value reaches the union validator
297+
-- (instead of being dropped or coerced as a scalar).
298+
T.describe("params: deepObject anyOf composed (allOf) object branch", function()
299+
local route = make_route({
300+
{ name = "created", ["in"] = "query", required = false,
301+
style = "deepObject", explode = true,
302+
schema = { anyOf = {
303+
{ allOf = {
304+
{ type = "object", properties = {
305+
gt = { type = "string" },
306+
} },
307+
} },
308+
{ type = "integer" },
309+
} } },
310+
}, "query")
311+
312+
local ok, errs = params_mod.validate(route,
313+
{}, { ["created[gt]"] = "abc" }, {})
314+
T.ok(ok, "composed object branch parsed")
315+
T.ok(not errs or #errs == 0, "no errors")
316+
end)
317+
295318
T.done()

0 commit comments

Comments
 (0)