diff --git a/kong/db/schema/others/declarative_config.lua b/kong/db/schema/others/declarative_config.lua index e04fe87144b..afbb0995721 100644 --- a/kong/db/schema/others/declarative_config.lua +++ b/kong/db/schema/others/declarative_config.lua @@ -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 @@ -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 diff --git a/spec/01-unit/01-db/01-schema/11-declarative_config/01-validate_spec.lua b/spec/01-unit/01-db/01-schema/11-declarative_config/01-validate_spec.lua index aab2e8467b3..b65e1139646 100644 --- a/spec/01-unit/01-db/01-schema/11-declarative_config/01-validate_spec.lua +++ b/spec/01-unit/01-db/01-schema/11-declarative_config/01-validate_spec.lua @@ -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)