From 30789e760881201709b66c00ca851201522d2647 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Wed, 12 Sep 2018 11:12:11 -0400 Subject: [PATCH] feat(schema) make records non-nullable by default Make records in Entities non-nullable by default, so that they return their full structure on API queries. The idea of returning explicit nulls in API queries was to allow users to see what are the available fields. In nested structures, for that to happen we need to make records non-nullable, even if their individual fields are. A field of the `record` type in an Entity can still be made nullable if desired, with an explicit `nullable = true` property. This is done at the Entity level because the MetaSchema uses nullable records extensively. --- kong/db/schema/entity.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/kong/db/schema/entity.lua b/kong/db/schema/entity.lua index ed98d62fa72b..4cce3ab31328 100644 --- a/kong/db/schema/entity.lua +++ b/kong/db/schema/entity.lua @@ -17,6 +17,18 @@ local base_types = { integer = true, } +-- Make records in Entities non-nullable by default, +-- so that they return their full structure on API queries. +local function make_records_non_nullable(field) + if field.nullable == nil then + field.nullable = false + end + for _, f in Schema.each_field(field) do + if f.type == "record" then + make_records_non_nullable(f) + end + end +end function Entity.new(definition) @@ -47,6 +59,9 @@ function Entity.new(definition) return nil, entity_errors.AGGREGATE_ON_BASE_TYPES_ONLY:format(name) end + elseif field.type == "record" then + make_records_non_nullable(field) + end ::continue::