Skip to content

Commit

Permalink
WIP: Add functions to /get path
Browse files Browse the repository at this point in the history
  • Loading branch information
laurenceisla committed Sep 12, 2024
1 parent dbd8f95 commit ab06b07
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 8 deletions.
73 changes: 68 additions & 5 deletions sql/components.sql
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ create or replace function oas_build_component_parameters(schemas text[])
returns jsonb language sql stable as
$$
select oas_build_component_parameters_query_params_from_tables(schemas) ||
oas_build_component_parameters_query_params_from_function_args(schemas) ||
oas_build_component_parameters_query_params_common() ||
oas_build_component_parameters_headers_common();
$$;
Expand All @@ -245,9 +246,64 @@ from (
) as param_schema
from (
select table_full_name, column_name
from postgrest_get_all_tables_and_composite_types()
where table_schema = any(schemas)
and (is_table or is_view)
from postgrest_get_all_tables_and_composite_types()
where
(table_schema = any(schemas) and (is_table or is_view))
-- All the tables and composite types returned by functions
or exists (
select 1
from postgrest_get_all_functions(schemas)
where return_type_composite_relid = table_oid
)
) _
) x;
$$;

create or replace function oas_build_component_parameters_query_params_from_function_args(schemas text[])
returns jsonb language sql stable as
$$
select jsonb_object_agg(x.param_name, x.param_schema)
from (
select format('funcParams.%1$s.%2$s', function_name, argument_name) as param_name,
oas_parameter_object(
name := argument_name,
"in" := 'query',
schema :=
case when argument_is_in then
oas_schema_object(
type := argument_oastype,
format := argument_type_name::text
)
when argument_is_variadic then
oas_schema_object(
type := 'array',
items := oas_schema_object(
type := argument_oastype,
format := argument_type_name::text
)
)
when argument_is_inout then
-- INOUT arguments can be used as argument and row filter at the same time, e.g. "?arg=1&arg=eq.1"
-- It would be more accurate to use "anyOf", but the OpenAPI UIs by different providers do not support it for query parameters
oas_schema_object(
type := 'array',
maxItems := 2,
items := oas_schema_object(
type := 'string'
)
)
else
-- TABLE and OUT arguments can be used as row filters
oas_schema_object(
type := 'string'
)
end
) as param_schema
from (
select function_name, argument_name, argument_is_in, argument_is_inout, argument_type_name, argument_is_variadic,
case when postgrest_pgtype_to_oastype(argument_type_name) = any ('{array,object}') then 'string' else postgrest_pgtype_to_oastype(argument_type_name) end as argument_oastype
from postgrest_get_all_functions(schemas)
where argument_input_qty > 0 and argument_name <> ''
) _
) x;
$$;
Expand Down Expand Up @@ -616,6 +672,7 @@ from (
)
)
) as not_empty_response,
-- TODO: this should be null for tables/composite types returned by functions and not included in exposed schemas
'mayBeEmpty.' || table_full_name as may_be_empty,
case when insertable then
oas_response_object(
Expand Down Expand Up @@ -668,8 +725,14 @@ from (
)
end as may_be_empty_response
from postgrest_get_all_tables_and_composite_types()
where table_schema = any(schemas)
and (is_table or is_view)
where
(table_schema = any(schemas) and (is_table or is_view))
-- All the tables and composite types returned by functions
or exists (
select 1
from postgrest_get_all_functions(schemas) f
where return_type_composite_relid = table_oid
)
group by table_schema, table_full_name, insertable, is_table, is_view
) as x
$$;
Expand Down
78 changes: 77 additions & 1 deletion sql/paths.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ create or replace function oas_build_paths(schemas text[])
returns jsonb language sql stable as
$$
select oas_build_path_item_root() ||
oas_build_path_items_from_tables(schemas);
oas_build_path_items_from_tables(schemas) ||
oas_build_path_items_from_functions(schemas);
$$;

create or replace function oas_build_path_items_from_tables(schemas text[])
Expand Down Expand Up @@ -134,6 +135,81 @@ from (
) x;
$$;

create or replace function oas_build_path_items_from_functions(schemas text[])
returns jsonb language sql stable as
$$
select jsonb_object_agg(x.path, x.oas_path_item)
from (
select '/rpc/' || function_name as path,
oas_path_item_object(
get :=oas_operation_object(
description := function_description,
tags := array['(rpc)' || function_name],
parameters :=
jsonb_agg(
oas_build_reference_to_parameters(format('funcParams.%1$s.%2$s', function_name, argument_name))
) ||
return_composite_param_ref ||
case when return_type_is_table or return_type_is_out or return_type_composite_relid <> 0 then
jsonb_build_array(
oas_build_reference_to_parameters('select'),
oas_build_reference_to_parameters('order'),
oas_build_reference_to_parameters('limit'),
oas_build_reference_to_parameters('offset'),
oas_build_reference_to_parameters('or'),
oas_build_reference_to_parameters('and'),
oas_build_reference_to_parameters('not.or'),
oas_build_reference_to_parameters('not.and'),
oas_build_reference_to_parameters('range'),
oas_build_reference_to_parameters('preferGet')
)
else
jsonb_build_array(
oas_build_reference_to_parameters('preferGet')
)
end,
responses :=
case when return_type_is_table or return_type_is_out then
jsonb_build_object(
'200',
oas_build_reference_to_responses('notEmpty.' || return_type_name, 'OK'),
'206',
oas_build_reference_to_responses('notEmpty.' || return_type_name, 'Partial Content')
)
when return_type_composite_relid <> 0 then
jsonb_build_object(
'200',
oas_build_reference_to_responses('notEmpty.' || return_type_name, 'OK')
)
else
jsonb_build_object(
'200',
oas_build_reference_to_responses(return_type_name, 'OK')
)
end ||
jsonb_build_object(
'default',
oas_build_reference_to_responses('defaultError', 'Error')
)
)
) as oas_path_item
from (
select function_name, function_description, return_type_name, return_type_is_table, return_type_is_out, return_type_composite_relid, argument_name,
comp.return_composite_param_ref
from postgrest_get_all_functions(schemas) f
left join lateral (
select jsonb_agg(oas_build_reference_to_parameters(format('rowFilter.%1$s.%2$s', table_name, column_name))) as return_composite_param_ref
from (
select c.table_name, c.column_name
from postgrest_get_all_tables_and_composite_types() c
where f.return_type_composite_relid = c.table_oid
) _
) comp on true
) _
group by function_name, function_description, return_type_name, return_type_is_table, return_type_is_out, return_type_composite_relid, return_composite_param_ref
) x;
$$;

create or replace function oas_build_path_item_root()
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

0 comments on commit ab06b07

Please sign in to comment.