|
17 | 17 | create or replace function oas_build_component_schemas(schemas text[])
|
18 | 18 | returns jsonb language sql stable as
|
19 | 19 | $$
|
20 |
| - select oas_build_component_schemas_from_tables(schemas) || |
21 |
| - oas_build_component_schemas_from_composite_types(schemas) || |
| 20 | + select oas_build_component_schemas_from_tables_and_composite_types(schemas) || |
22 | 21 | oas_build_component_schemas_headers()
|
23 | 22 | $$;
|
24 | 23 |
|
25 |
| -create or replace function oas_build_component_schemas_from_tables(schemas text[]) |
| 24 | +create or replace function oas_build_component_schemas_from_tables_and_composite_types(schemas text[]) |
26 | 25 | returns jsonb language sql stable as
|
27 | 26 | $$
|
28 |
| -select jsonb_object_agg(x.table_name, x.oas_schema) |
| 27 | +with recursive all_rels as ( |
| 28 | + select * |
| 29 | + from postgrest_get_all_tables_and_composite_types() |
| 30 | +), |
| 31 | +recursive_rels_in_schema as ( |
| 32 | + select * |
| 33 | + from all_rels |
| 34 | + where |
| 35 | + -- All the tables or views in the exposed schemas |
| 36 | + (table_schema = any(schemas) and (is_table or is_view)) |
| 37 | + -- All the composite types or tables that are present in function arguments |
| 38 | + -- TODO: tweak postgrest_get_all_functions() or use another CTE for a more performant filter |
| 39 | + or table_oid in ( |
| 40 | + select unnest(composite_args_ret) |
| 41 | + from postgrest_get_all_functions(schemas) |
| 42 | + ) |
| 43 | + union |
| 44 | + -- Tables may have columns with composite or table types, so we recursively |
| 45 | + -- look for these composite types or tables outside of the exposed schemas |
| 46 | + -- in order to build the types correctly for the OpenAPI output. |
| 47 | + select e.* |
| 48 | + from all_rels e, recursive_rels_in_schema r |
| 49 | + where e.table_oid = r.column_composite_relid |
| 50 | +), |
| 51 | +all_tables_and_composite_types as ( |
| 52 | + select |
| 53 | + table_schema, |
| 54 | + table_name, |
| 55 | + table_description, |
| 56 | + is_composite, |
| 57 | + array_agg(column_name order by column_position) filter (where not column_is_nullable) AS required_cols, |
| 58 | + jsonb_object_agg( |
| 59 | + column_name, |
| 60 | + case when column_item_data_type is null and column_composite_relid <> 0 then |
| 61 | + oas_build_reference_to_schemas(column_data_type) |
| 62 | + else |
| 63 | + oas_schema_object( |
| 64 | + description := column_description, |
| 65 | + type := postgrest_pgtype_to_oastype(column_data_type), |
| 66 | + format := column_data_type::text, |
| 67 | + maxlength := column_character_maximum_length, |
| 68 | + -- "default" := to_jsonb(info.column_default), |
| 69 | + enum := to_jsonb(column_enums), |
| 70 | + items := |
| 71 | + case |
| 72 | + when column_item_data_type is null then |
| 73 | + null |
| 74 | + when column_composite_relid <> 0 then |
| 75 | + oas_build_reference_to_schemas(column_item_data_type) |
| 76 | + else |
| 77 | + oas_schema_object( |
| 78 | + type := postgrest_pgtype_to_oastype(column_item_data_type), |
| 79 | + format := column_item_data_type::text |
| 80 | + ) |
| 81 | + end |
| 82 | + ) |
| 83 | + end order by column_position |
| 84 | + ) as columns |
| 85 | + from recursive_rels_in_schema |
| 86 | + group by table_schema, table_name, table_description, is_composite |
| 87 | +) |
| 88 | +select jsonb_object_agg(x.component_name, x.oas_schema) |
29 | 89 | from (
|
30 |
| - select table_name, |
| 90 | + select case when is_composite then table_schema || '.' else '' end || table_name as component_name, |
31 | 91 | oas_schema_object(
|
32 | 92 | description := table_description,
|
33 |
| - properties := columns, |
| 93 | + properties := coalesce(columns, '{}'), |
34 | 94 | required := required_cols,
|
35 | 95 | type := 'object'
|
36 | 96 | ) as oas_schema
|
37 |
| - from postgrest_get_all_tables(schemas) |
38 |
| - where table_schema = any(schemas) |
39 |
| -) x; |
40 |
| -$$; |
41 |
| - |
42 |
| -create or replace function oas_build_component_schemas_from_composite_types(schemas text[]) |
43 |
| -returns jsonb language sql stable as |
44 |
| -$$ |
45 |
| -SELECT coalesce(jsonb_object_agg(x.ct_name, x.oas_schema), '{}') |
46 |
| -FROM ( |
47 |
| - SELECT comptype_schema || '.' || comptype_name as ct_name, |
48 |
| - oas_schema_object( |
49 |
| - description := comptype_description, |
50 |
| - properties := columns, |
51 |
| - type := 'object' |
52 |
| - ) AS oas_schema |
53 |
| - FROM postgrest_get_all_composite_types(schemas) |
| 97 | + from all_tables_and_composite_types |
54 | 98 | ) x;
|
55 | 99 | $$;
|
56 | 100 |
|
@@ -195,10 +239,11 @@ from (
|
195 | 239 | )
|
196 | 240 | ) as param_schema
|
197 | 241 | from (
|
198 |
| - select table_schema, table_name, unnest(all_cols) as column_name |
199 |
| - from postgrest_get_all_tables(schemas) |
| 242 | + select table_schema, is_table, is_view, table_name, column_name |
| 243 | + from postgrest_get_all_tables_and_composite_types() |
| 244 | + where table_schema = any(schemas) |
| 245 | + and (is_table or is_view) |
200 | 246 | ) _
|
201 |
| - where table_schema = any(schemas) |
202 | 247 | ) x;
|
203 | 248 | $$;
|
204 | 249 |
|
@@ -617,8 +662,10 @@ from (
|
617 | 662 | )
|
618 | 663 | )
|
619 | 664 | end as may_be_empty_response
|
620 |
| - from postgrest_get_all_tables(schemas) |
| 665 | + from postgrest_get_all_tables_and_composite_types() |
621 | 666 | where table_schema = any(schemas)
|
| 667 | + and (is_table or is_view) |
| 668 | + group by table_schema, table_name, insertable, is_table, is_view |
622 | 669 | ) as x
|
623 | 670 | $$;
|
624 | 671 |
|
@@ -695,9 +742,11 @@ from (
|
695 | 742 | )
|
696 | 743 | )
|
697 | 744 | ) as oas_req_body
|
698 |
| - from postgrest_get_all_tables(schemas) |
| 745 | + from postgrest_get_all_tables_and_composite_types() |
699 | 746 | where table_schema = any(schemas)
|
| 747 | + and (is_table or is_view) |
700 | 748 | and insertable
|
| 749 | + group by table_schema, table_name, insertable |
701 | 750 | ) as x;
|
702 | 751 | $$;
|
703 | 752 |
|
|
0 commit comments