Skip to content

Commit

Permalink
feat(schema) make records non-nullable by default
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hishamhm authored and thibaultcha committed Sep 12, 2018
1 parent 39ee105 commit 30789e7
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions kong/db/schema/entity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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::
Expand Down

0 comments on commit 30789e7

Please sign in to comment.