diff --git a/kong/dao/cassandra/apis.lua b/kong/dao/cassandra/apis.lua index 36ef4d5ef757..d0ffb21260d3 100644 --- a/kong/dao/cassandra/apis.lua +++ b/kong/dao/cassandra/apis.lua @@ -4,7 +4,7 @@ local PluginsConfigurations = require "kong.dao.cassandra.plugins_configurations local SCHEMA = { id = { type = constants.DATABASE_TYPES.ID }, - name = { type = "string", required = true, unique = true, queryable = true }, + name = { type = "string", unique = true, queryable = true, default = function(api_t) return api_t.public_dns end }, public_dns = { type = "string", required = true, unique = true, queryable = true, regex = "(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])" }, target_url = { type = "string", required = true }, diff --git a/kong/dao/schemas.lua b/kong/dao/schemas.lua index 13eadd917b8a..4c7b8f199173 100644 --- a/kong/dao/schemas.lua +++ b/kong/dao/schemas.lua @@ -43,7 +43,7 @@ function _M.validate(t, schema, is_update) -- Set default value for the field if given if t[column] == nil and v.default ~= nil then if type(v.default) == "function" then - t[column] = v.default() + t[column] = v.default(t) else t[column] = v.default end diff --git a/spec/integration/admin_api/admin_api_spec.lua b/spec/integration/admin_api/admin_api_spec.lua index 0a98d97058ee..a933eb6fa2fe 100644 --- a/spec/integration/admin_api/admin_api_spec.lua +++ b/spec/integration/admin_api/admin_api_spec.lua @@ -18,7 +18,7 @@ local ENDPOINTS = { update_fields = { public_dns = "newapi.mockbin.com" }, - error_message = '{"public_dns":"public_dns is required","name":"name is required","target_url":"target_url is required"}\n' + error_message = '{"public_dns":"public_dns is required","target_url":"target_url is required"}\n' }, { collection = "consumers", diff --git a/spec/unit/dao/cassandra_spec.lua b/spec/unit/dao/cassandra_spec.lua index 100de63d709d..261783df930c 100644 --- a/spec/unit/dao/cassandra_spec.lua +++ b/spec/unit/dao/cassandra_spec.lua @@ -151,6 +151,16 @@ describe("Cassandra DAO", function() assert.truthy(api.created_at) end) + it("should use the public_dns as the name if none is specified", function() + local api, err = dao_factory.apis:insert { + public_dns = "test.com", + target_url = "http://mockbin.com" + } + assert.falsy(err) + assert.truthy(api.name) + assert.are.same("test.com", api.name) + end) + it("should not insert an invalid api", function() -- Nil local api, err = dao_factory.apis:insert() diff --git a/spec/unit/validations_spec.lua b/spec/unit/validations_spec.lua index 844c88ba73e2..52f130c3d56c 100644 --- a/spec/unit/validations_spec.lua +++ b/spec/unit/validations_spec.lua @@ -2,7 +2,7 @@ local schemas = require "kong.dao.schemas" local constants = require "kong.constants" local validate = schemas.validate -describe("Validation #schema", function() +describe("Validation", function() it("should return the right alias", function() assert.are.same("number", schemas.get_type("number")) @@ -23,7 +23,10 @@ describe("Validation #schema", function() date = { default = 123456, immutable = true }, allowed = { enum = { "hello", "world" }}, boolean_val = { type = "boolean" }, - default = { default = function() return "default" end }, + default = { default = function(t) + assert.truthy(t) + return "default" + end }, custom = { func = function(v, t) if v then if t.default == "test_custom_func" then