Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dc) exclude cases where "plugins" are used not as plugins #7412

Merged
merged 3 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions kong/db/schema/others/declarative_config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,12 @@ local function load_plugin_subschemas(fields, plugin_set, indent)
for _, f in ipairs(fields) do
local fname, fdata = next(f)

if fname == "plugins" then
-- Exclude cases where `plugins` are used expect from plugins entities.
-- This assumes other entities doesn't have `name` as its subschema_key.
if fname == "plugins" and fdata.elements and fdata.elements.subschema_key == "name" then
for plugin in pairs(plugin_set) do
local _, err, err_t = plugin_loader.load_subschema(fdata.elements, plugin, errors)
local _, err = plugin_loader.load_subschema(fdata.elements, plugin, errors)
gszr marked this conversation as resolved.
Show resolved Hide resolved

if err_t then
return nil, "schema for plugin '" .. plugin .. "' is invalid: " ..
tostring(errors:schema_violation(err_t))
end
if err then
return nil, err
end
Expand Down Expand Up @@ -735,10 +733,10 @@ function DeclarativeConfig.load(plugin_set, include_foreign)
end

for plugin in pairs(plugin_set) do
local entities, err, err_t = plugin_loader.load_entities(plugin, errors,
local entities, err = plugin_loader.load_entities(plugin, errors,
plugin_loader.load_entity_schema)
if err or err_t then
return nil, err, err_t
if err then
return nil, err
end
for entity, schema in pairs(entities) do
all_schemas[entity] = schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -851,3 +851,61 @@ describe("declarative config: validate", function()
end)
end)
end)

describe("declarative config: validate", function()

local daos = {
["dao-keywords"] = {
name = "dao-keywords",
primary_key = {"field1"},
admin_api_name = "dao-keywords",
admin_api_nested_name = "dao-keywords",
fields = {
{field1 = {type = "string", required = true}},
{field2 = {type = "string", required = true}},
}
}
}

local plugins_set = helpers.test_conf.loaded_plugins
plugins_set["dao-keywords"] = true

lazy_setup(function()
package.loaded["kong.plugins.dao-keywords.schema"] = {
name = "dao-keywords",
fields = {
{ config = {
type = "record",
fields = {},
}, },
}
}

package.loaded["kong.plugins.dao-keywords.daos"] = daos
end)

lazy_teardown(function()
package.loaded["kong.plugins.dao-keywords.schema"] = nil
package.loaded["kong.plugins.dao-keywords.daos"] = nil
end)

it("loads plugins with custom DAO that has keywords as string", function()
daos["dao-keywords"]["fields"][2] = {plugins = {type = "string", required = true}}

assert(declarative_config.load(plugins_set))
end)

it("loads plugins with custom DAO that has keywords as array", function()
daos["dao-keywords"]["fields"][2] = {
plugins = {
type = "array",
required = false,
elements = {
type = "string",
},
},
}

assert(declarative_config.load(plugins_set))
end)
end)