From 9ce1b9d2a656632efa660aa5a572322688e946f8 Mon Sep 17 00:00:00 2001 From: kurt Date: Wed, 13 Nov 2024 12:27:14 +0800 Subject: [PATCH] fix(schema): schema validation to handle potential `nil` field case (#13861) Adjusted schema validation logic to account for `nil` field scenario, ensuring proper handling of fields to prevent errors. Fix:[FTI-6336](https://konghq.atlassian.net/browse/FTI-6336) --------- Signed-off-by: tzssangglass Co-authored-by: Keery Nie --- .../fix-schema-validation-with-nil-field.yml | 3 ++ kong/db/schema/init.lua | 2 +- .../01-db/01-schema/06-routes_spec.lua | 31 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 changelog/unreleased/kong/fix-schema-validation-with-nil-field.yml diff --git a/changelog/unreleased/kong/fix-schema-validation-with-nil-field.yml b/changelog/unreleased/kong/fix-schema-validation-with-nil-field.yml new file mode 100644 index 000000000000..07225001491b --- /dev/null +++ b/changelog/unreleased/kong/fix-schema-validation-with-nil-field.yml @@ -0,0 +1,3 @@ +message: "Fixed a 500 error triggered by unhandled nil fields during schema validation." +type: bugfix +scope: Core diff --git a/kong/db/schema/init.lua b/kong/db/schema/init.lua index 2af360b3b8bd..b6b1532416a8 100644 --- a/kong/db/schema/init.lua +++ b/kong/db/schema/init.lua @@ -1273,7 +1273,7 @@ local function run_entity_check(self, name, input, arg, full_check, errors) -- Don't run if any of the values is a reference in a referenceable field local field = get_schema_field(self, fname) - if field.type == "string" and field.referenceable and is_reference(value) then + if field and field.type == "string" and field.referenceable and is_reference(value) then return end end diff --git a/spec/01-unit/01-db/01-schema/06-routes_spec.lua b/spec/01-unit/01-db/01-schema/06-routes_spec.lua index 50a1bd73329a..3bfd0f1fd92e 100644 --- a/spec/01-unit/01-db/01-schema/06-routes_spec.lua +++ b/spec/01-unit/01-db/01-schema/06-routes_spec.lua @@ -1800,3 +1800,34 @@ describe("routes schema (flavor = expressions)", function() end end) end) + + +describe("routes schema (flavor = traditional_compatible)", function() + local a_valid_uuid = "cbb297c0-a956-486d-ad1d-f9b42df9465a" + local another_uuid = "64a8670b-900f-44e7-a900-6ec7ef5aa4d3" + + reload_flavor("traditional_compatible") + setup_global_env() + + it("validates a route with only expression field", function() + local route = { + id = a_valid_uuid, + name = "my_route", + protocols = { "http" }, + hosts = { "example.com" }, + expression = [[(http.method == "GET")]], + priority = 100, + service = { id = another_uuid }, + } + route = Routes:process_auto_fields(route, "insert") + assert.truthy(route.created_at) + assert.truthy(route.updated_at) + assert.same(route.created_at, route.updated_at) + local ok, errs = Routes:validate(route) + assert.falsy(ok) + assert.same({ + ["expression"] = 'unknown field', + ["priority"] = 'unknown field' + }, errs) + end) +end)