From 35c79dd46168150fb3a10ae16450b3e8ad3d87e6 Mon Sep 17 00:00:00 2001 From: oowl Date: Tue, 19 Dec 2023 11:13:53 +0800 Subject: [PATCH] fix(dbless): fix error data loss caused by weakly typed of function in declarative_config_flattened function (#12167) FTI-5584 (cherry picked from commit 410d9bd32f6206dfab1c8121f79b1f50d532a5d4) --- ...declarative-config-flattened-data-loss.yml | 3 ++ kong/db/errors.lua | 8 +++- .../04-admin_api/15-off_spec.lua | 37 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 changelog/unreleased/kong/fix-declarative-config-flattened-data-loss.yml diff --git a/changelog/unreleased/kong/fix-declarative-config-flattened-data-loss.yml b/changelog/unreleased/kong/fix-declarative-config-flattened-data-loss.yml new file mode 100644 index 000000000000..05991af010d0 --- /dev/null +++ b/changelog/unreleased/kong/fix-declarative-config-flattened-data-loss.yml @@ -0,0 +1,3 @@ +message: fix error data loss caused by weakly typed of function in declarative_config_flattened function +type: bugfix +scope: Configuration diff --git a/kong/db/errors.lua b/kong/db/errors.lua index e5c01f3473f5..c1b3cefc85a4 100644 --- a/kong/db/errors.lua +++ b/kong/db/errors.lua @@ -1022,7 +1022,13 @@ do for i, err_t_i in drain(section_errors) do local entity = entities[i] - if type(entity) == "table" then + + -- promote error strings to `@entity` type errors + if type(err_t_i) == "string" then + err_t_i = { ["@entity"] = err_t_i } + end + + if type(entity) == "table" and type(err_t_i) == "table" then add_entity_errors(entity_type, entity, err_t_i, flattened) else diff --git a/spec/02-integration/04-admin_api/15-off_spec.lua b/spec/02-integration/04-admin_api/15-off_spec.lua index 61ec17c8fe28..8515870fca77 100644 --- a/spec/02-integration/04-admin_api/15-off_spec.lua +++ b/spec/02-integration/04-admin_api/15-off_spec.lua @@ -2697,6 +2697,43 @@ R6InCcH2Wh8wSeY5AuDXvu2tv9g/PW9wIJmPuKSHMA== }, }, flattened) end) + it("origin error do not loss when enable flatten_errors - (#12167)", function() + local input = { + _format_version = "3.0", + consumers = { + { + id = "a73dc9a7-93df-584d-97c0-7f41a1bbce3d", + username = "test-consumer-1", + tags = { "consumer-1" }, + }, + { + id = "a73dc9a7-93df-584d-97c0-7f41a1bbce32", + username = "test-consumer-1", + tags = { "consumer-2" }, + }, + }, + } + local flattened = post_config(input) + validate({ + { + entity_type = "consumer", + entity_id = "a73dc9a7-93df-584d-97c0-7f41a1bbce32", + entity_name = nil, + entity_tags = { "consumer-2" }, + entity = { + id = "a73dc9a7-93df-584d-97c0-7f41a1bbce32", + username = "test-consumer-1", + tags = { "consumer-2" }, + }, + errors = { + { + type = "entity", + message = "uniqueness violation: 'consumers' entity with username set to 'test-consumer-1' already declared", + } + }, + }, + }, flattened) + end) end)