Skip to content

Commit

Permalink
fix(plugins/request-validator): fixed an issue where requests get rej…
Browse files Browse the repository at this point in the history
…ected when defining an object parameter with form style and exploded. (#10052)

FTI-6179
  • Loading branch information
vm-001 authored Sep 23, 2024
1 parent da61065 commit b356210
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: "**Request-validator**: Fixed an issue where requests get rejected when defining an object parameter with form style and exploded."
type: bugfix
scope: Plugin
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,16 @@ end

-- validates the 'required' property of a schema
local function validate_required(location, parameter)
if location == "query" and parameter.style == "deepObject" then
return true
if location == "query" then
if parameter.style == "deepObject" then
return true
end
if parameter.style == "form" and parameter.explode == true then
assert(validator_param_cache[parameter])
if (parameter.decoded_schema or EMPTY).type == "object" then
return true
end
end
end

local value = template_environment[location][parameter.name]
Expand Down Expand Up @@ -253,13 +261,18 @@ local validate_data do
return validate_style_deepobject(location, parameter)
end

local validator = validator_param_cache[parameter]
if location == "query" and parameter.style == "form" and parameter.explode == true and
(parameter.decoded_schema or EMPTY).type == "object" then
parameter.value = template_environment["query"]
end

-- if param is not required and value is nil or serialization
-- information not being set, return valid
if not parameter.value or parameter.style == ngx_null then
return true
end

local validator = validator_param_cache[parameter]
if type(parameter.value) ~= "table" or tables_allowed[parameter.decoded_schema.type] then
-- if the value is a table, then we can only validate it for non-primitives
local result, err = deserialize(parameter.style, parameter.decoded_schema.type,
Expand Down
47 changes: 47 additions & 0 deletions plugins-ee/request-validator/spec/03-access_draft4_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,53 @@ for _, strategy in strategies() do
assert.same({ "a", "e"}, body.data)
end)
end

it("location: query, style: form, explode: true schema_type: object", function()
local param_schema = {
{
name = "id",
["in"] = "query",
required = true,
schema = [[
{
"type": "object",
"required": ["integer", "string"],
"properties": {
"integer": {
"type": "integer"
},
"string": {
"type": "string"
}
}
}]],
style = "form",
explode = true,
}
}

add_plugin(admin_client, { parameter_schema = param_schema, verbose_response = true }, 201)

local res = assert(proxy_client:send {
method = "GET",
path = "/anything?integer=1&string=str",
headers = {
["Host"] = "path.test",
}
})
assert.res_status(200, res)

local res = assert(proxy_client:send {
method = "GET",
path = "/anything?integer=1",
headers = {
["Host"] = "path.test",
}
})
assert.res_status(400, res)
local body = assert.response(res).has.jsonbody()
assert.same("query 'id' validation failed, [error] property string is required", body.message)
end)
end)
end)
end

0 comments on commit b356210

Please sign in to comment.