Skip to content

Commit

Permalink
fix(dc) exclude cases where "plugins" are used not as plugins entities (
Browse files Browse the repository at this point in the history
  • Loading branch information
fffonion authored Jun 2, 2021
1 parent 17bfffe commit c963667
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
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)

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)

0 comments on commit c963667

Please sign in to comment.