Skip to content

Commit

Permalink
fix(admin_api): allow retrieval via uuid names
Browse files Browse the repository at this point in the history
Previously you could name an entity using a uuid
however it wouldn't be retrieveable from the API by
it's name - ex: GET /routes/<uuid> where uuid was set
as this route's name. This commit fixes that.

KAG-2581
  • Loading branch information
nowNick committed Mar 20, 2024
1 parent 3ca6385 commit c035086
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/kong/fix-admin-api-uuid-entity-names.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
message: |
**Admin API**: Fixed an issue where an entity (like route) wouldn't be
retrievable through admin via it's name if it was set as uuid.
type: bugfix
scope: Admin API
15 changes: 14 additions & 1 deletion kong/api/endpoints.lua
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,20 @@ local function query_entity(context, self, db, schema, method)
end
end

if key.id and not utils.is_valid_uuid(key.id) then
if key.id then
if utils.is_valid_uuid(key.id) then
local res, err, err_t, offset
if is_update then
res, err, err_t, offset = dao[method or context](dao, key, args, opts)
else
res, err, err_t, offset = dao[method or context](dao, key, opts)
end

if res or (err_t and err_t.code ~= Errors.codes.NOT_FOUND) then
return res, err, err_t, offset
end
end

local endpoint_key = schema.endpoint_key
if endpoint_key then
local field = schema.fields[endpoint_key]
Expand Down
47 changes: 46 additions & 1 deletion spec/02-integration/04-admin_api/09-routes_routes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,16 @@ for _, strategy in helpers.each_strategy() do
end)

it("retrieves by name", function()
local route = bp.named_routes:insert(nil, { nulls = true })
local route = bp.named_routes:insert({ name = "test-route-name" }, { nulls = true })
local res = client:get("/routes/" .. route.name)
local body = assert.res_status(200, res)

local json = cjson.decode(body)
assert.same(route, json)
end)

it("retrieves by name - when name is uuid", function()
local route = bp.named_routes:insert({ name = utils.uuid() }, { nulls = true })
local res = client:get("/routes/" .. route.name)
local body = assert.res_status(200, res)

Expand Down Expand Up @@ -1098,6 +1107,42 @@ for _, strategy in helpers.each_strategy() do
end
end)

it_content_types("updates if found by name - when name is uuid", function(content_type)
return function()
if content_type == "multipart/form-data" then
-- the client doesn't play well with this
return
end

local route = bp.routes:insert({
name = utils.uuid(),
paths = { "/my-route" },
})
local res = client:patch("/routes/" .. route.name, {
headers = {
["Content-Type"] = content_type
},
body = {
methods = cjson.null,
hosts = cjson.null,
paths = { "/updated-paths" },
},
})
local body = assert.res_status(200, res)
local json = cjson.decode(body)
assert.same({ "/updated-paths" }, json.paths)
assert.same(cjson.null, json.hosts)
assert.same(cjson.null, json.methods)
assert.equal(route.id, json.id)
assert.equal(route.name, json.name)

local in_db = assert(db.routes:select(route, { nulls = true }))
assert.same(json, in_db)

db.routes:delete(route)
end
end)

it_content_types("updates strip_path if not previously set", function(content_type)
return function()
local route = bp.routes:insert({ paths = { "/my-route" } })
Expand Down
9 changes: 9 additions & 0 deletions spec/02-integration/04-admin_api/10-services_routes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,15 @@ for _, strategy in helpers.each_strategy() do
assert.same(service, json)
end)

it("retrieves by name - even when name is uuid", function()
local service = bp.named_services:insert({name = utils.uuid()}, { nulls = true })
local res = client:get("/services/" .. service.name)
local body = assert.res_status(200, res)

local json = cjson.decode(body)
assert.same(service, json)
end)

it("retrieves by utf-8 name and percent-escaped utf-8 name", function()
local service = bp.services:insert({ name = "" }, { nulls = true })
local res = client:get("/services/" .. service.name)
Expand Down

0 comments on commit c035086

Please sign in to comment.