Skip to content

Commit 8d3c87b

Browse files
committed
address review: recognise object branches with type union (e.g. {object,null})
CodeRabbit pointed out that 'branch.type == "object"' misses union type arrays produced by nullable normalisation (type = {"object", "null"}). Added a small helper that treats branch.type as either a string or a list. Added a regression test covering the nullable object branch. (Did not adopt the proposal to per-branch validate and pick the matching object branch: parse_deep_object is schema-agnostic about which keys it collects, so all object branches see the same parsed object, and the downstream jsonschema validator on the full anyOf/oneOf already evaluates every branch. Branch order only affects per-property type coercion via coerce_object_values, and in practice deepObject filters use uniform property types — adding per-branch jsonschema validation here would duplicate work without changing acceptance behaviour.)
1 parent ff1fab3 commit 8d3c87b

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

lib/resty/openapi_validator/params.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,18 @@ local function deserialize_param(raw_value, param, query_args)
262262
if style == "deepObject" and stype ~= "object"
263263
and (schema.anyOf or schema.oneOf) then
264264
local branches = schema.anyOf or schema.oneOf
265+
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
274+
end
265275
for _, branch in ipairs(branches) do
266-
if branch.type == "object" then
276+
if branch_has_object(branch) then
267277
local obj = parse_deep_object(param.name, query_args or {}, branch)
268278
if obj ~= nil then
269279
return obj

t/unit/test_params.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,24 @@ T.describe("params: deepObject anyOf rejects unmatched scalar", function()
272272
T.ok(errs and #errs >= 1, "error reported")
273273
end)
274274

275+
-- Union type arrays from nullable normalization, e.g. type = {"object","null"},
276+
-- must still be recognised as an object branch.
277+
T.describe("params: deepObject anyOf nullable object branch", function()
278+
local route = make_route({
279+
{ name = "created", ["in"] = "query", required = false,
280+
style = "deepObject", explode = true,
281+
schema = { anyOf = {
282+
{ type = { "object", "null" }, properties = {
283+
gt = { type = "integer" },
284+
} },
285+
{ type = "integer" },
286+
} } },
287+
}, "query")
288+
289+
local ok, errs = params_mod.validate(route,
290+
{}, { ["created[gt]"] = "1700000000" }, {})
291+
T.ok(ok, "nullable object branch parsed")
292+
T.ok(not errs or #errs == 0, "no errors")
293+
end)
294+
275295
T.done()

0 commit comments

Comments
 (0)