Skip to content

Commit

Permalink
feat: build Component Schemas for non-composite return types
Browse files Browse the repository at this point in the history
* These Component Schemas are needed to build the Response Objects for functions.
* The functions returning composite types are not included since those Component Schemas are already built in another function.
  • Loading branch information
laurenceisla committed Sep 23, 2024
1 parent bb725fd commit 6fc2b3a
Show file tree
Hide file tree
Showing 5 changed files with 522 additions and 6 deletions.
77 changes: 77 additions & 0 deletions sql/components.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ create or replace function oas_build_component_schemas(schemas text[])
returns jsonb language sql stable as
$$
select oas_build_component_schemas_from_tables_and_composite_types(schemas) ||
oas_build_component_schemas_from_functions_return_types(schemas) ||
oas_build_component_schemas_headers()
$$;

Expand Down Expand Up @@ -103,6 +104,82 @@ from (
) x;
$$;

create or replace function oas_build_component_schemas_from_functions_return_types(schemas text[])
returns jsonb language sql stable as
$$
with all_functions_returning_table_out_simple_types as (
-- Build Component Schemas for functions that RETURNS simple types or RETURNS TABLE or with INOUT/OUT arguments
select *, not (return_type_is_out or return_type_is_table) as return_type_is_simple
from postgrest_get_all_functions(schemas)
where return_type_is_out or return_type_is_table or not return_type_is_composite
),
aggregated_function_returns as (
select
function_schema,
function_full_name,
function_description,
return_type_is_simple,
-- Build objects for functions with RETURNS TABLE or with INOUT/OUT arguments
case when not return_type_is_simple then
jsonb_object_agg(
argument_name,
case when argument_item_type_name is null and argument_is_composite then
oas_build_reference_to_schemas(argument_composite_full_name)
else
oas_schema_object(
type := postgrest_pgtype_to_oastype(argument_type_name),
format := argument_type_name::text,
items :=
case
when argument_item_type_name is null then
null
when argument_is_composite then
oas_build_reference_to_schemas(argument_composite_full_name)
else
oas_schema_object(
type := postgrest_pgtype_to_oastype(argument_item_type_name),
format := argument_item_type_name::text
)
end
)
end
order by argument_position
) filter ( where argument_is_table or argument_is_out or argument_is_inout)
-- For functions with RETURNS <simple type> build the Schema according to the type
else
oas_schema_object(
type := postgrest_pgtype_to_oastype(return_type_name),
format := return_type_name::text,
items :=
case when return_type_item_name is not null then
oas_schema_object(
type := postgrest_pgtype_to_oastype(return_type_item_name),
format := return_type_item_name::text
)
end
)
end as arguments
from all_functions_returning_table_out_simple_types
group by function_schema, function_name, function_full_name, function_description, return_type_is_simple, return_type_name, return_type_item_name
)
select jsonb_object_agg(x.component_name, x.oas_schema)
from (
select
'rpc.' || function_full_name as component_name,
case when not return_type_is_simple then
oas_schema_object(
description := function_description,
properties := coalesce(arguments, '{}'),
type := 'object'
)
else
arguments
end as oas_schema
from
aggregated_function_returns
) x;
$$;

create or replace function oas_build_component_schemas_headers()
returns jsonb language sql stable as
$$
Expand Down
3 changes: 1 addition & 2 deletions sql/utils.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ select case when type like any(array['character', 'character varying', 'text'])
when type like any(array['bigint', 'integer', 'smallint']) then 'integer'
when type like 'boolean' then 'boolean'
when type like '%[]' then 'array'
when type like 'json' then 'object'
when type like 'jsonb' then 'object'
when type like any(array['json', 'jsonb', 'record']) then 'object'
else 'string' end;
$$;

Expand Down
Loading

0 comments on commit 6fc2b3a

Please sign in to comment.