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

change: check metadata_schema with check_schema like the other schema #4381

Merged
merged 1 commit into from
Jun 9, 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
26 changes: 18 additions & 8 deletions apisix/admin/plugin_metadata.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ local require = require
local core = require("apisix.core")
local api_router = require("apisix.api_router")

local injected_mark = "injected metadata_schema"
local _M = {
}

Expand Down Expand Up @@ -50,15 +51,14 @@ local function check_conf(plugin_name, conf)
return nil, {error_msg = "missing configurations"}
end

local schema = plugin_object.metadata_schema or {
type = "object",
properties = {},
}
if not schema.properties then
schema.properties = {
additionalProperties = false,
if not plugin_object.metadata_schema then
plugin_object.metadata_schema = {
type = "object",
['$comment'] = injected_mark,
properties = {},
}
end
local schema = plugin_object.metadata_schema

-- inject interceptors schema to each plugins
if schema.properties.interceptors
Expand All @@ -70,7 +70,17 @@ local function check_conf(plugin_name, conf)

core.log.info("schema: ", core.json.delay_encode(schema))
core.log.info("conf : ", core.json.delay_encode(conf))
local ok, err = core.schema.check(schema, conf)

local ok, err
if schema['$comment'] == injected_mark
-- check_schema is not required. If missing, fallback to check schema directly
or not plugin_object.check_schema
then
ok, err = core.schema.check(schema, conf)
else
ok, err = plugin_object.check_schema(conf, core.schema.TYPE_METADATA)
end

if not ok then
return nil, {error_msg = "invalid configuration: " .. err}
end
Expand Down
2 changes: 2 additions & 0 deletions apisix/core/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ local pcall = pcall

local _M = {
version = 0.3,

TYPE_CONSUMER = 1,
TYPE_METADATA = 2,
}


Expand Down
9 changes: 4 additions & 5 deletions apisix/plugins/batch-requests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,11 @@ local _M = {
}


function _M.check_schema(conf)
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
function _M.check_schema(conf, schema_type)
if schema_type == core.schema.TYPE_METADATA then
return core.schema.check(metadata_schema, conf)
end
return true
return core.schema.check(schema, conf)
end


Expand Down
3 changes: 3 additions & 0 deletions apisix/plugins/example-plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ local _M = {


function _M.check_schema(conf, schema_type)
if schema_type == core.schema.TYPE_METADATA then
return core.schema.check(metadata_schema, conf)
end
return core.schema.check(schema, conf)
end

Expand Down
5 changes: 4 additions & 1 deletion apisix/plugins/http-logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ local _M = {
}


function _M.check_schema(conf)
function _M.check_schema(conf, schema_type)
if schema_type == core.schema.TYPE_METADATA then
return core.schema.check(metadata_schema, conf)
end
return core.schema.check(schema, conf)
end

Expand Down
6 changes: 3 additions & 3 deletions apisix/plugins/skywalking.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ local math = math
local require = require

local plugin_name = "skywalking"
local metadata_schema = {
local attr_schema = {
type = "object",
properties = {
service_name = {
Expand Down Expand Up @@ -66,7 +66,7 @@ local _M = {
priority = -1100, -- last running plugin, but before serverless post func
name = plugin_name,
schema = schema,
metadata_schema = metadata_schema,
attr_schema = attr_schema,
}


Expand Down Expand Up @@ -117,7 +117,7 @@ function _M.init()
"plugin_attr",
plugin_name) or {}
local_plugin_info = core.table.clone(local_plugin_info)
local ok, err = core.schema.check(metadata_schema, local_plugin_info)
local ok, err = core.schema.check(attr_schema, local_plugin_info)
if not ok then
core.log.error("failed to check the plugin_attr[", plugin_name, "]",
": ", err)
Expand Down
4 changes: 4 additions & 0 deletions t/plugin/error-log-logger.t
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,15 @@ plugins:
}]]
)

-- ensure the request is rejected even this plugin doesn't
-- have check_schema method
ngx.status = code
core.log.warn("this is a warning message for test.")
}
}
--- request
GET /tg
--- error_code: 400
--- response_body
--- error_log eval
qr/please set the correct plugin_metadata for error-log-logger/
Expand Down