Skip to content

Commit

Permalink
Add query to get all functions and detect composite types in argument…
Browse files Browse the repository at this point in the history
…s and return values
  • Loading branch information
laurenceisla authored Feb 9, 2024
1 parent e0bc07c commit 24c2346
Show file tree
Hide file tree
Showing 10 changed files with 437 additions and 40 deletions.
30 changes: 15 additions & 15 deletions sql/components.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- Functions to build the Components Object of the OAS document

create or replace function oas_build_components(schemas text[])
returns jsonb language sql as
returns jsonb language sql stable as
$$
select oas_components_object(
schemas := oas_build_component_schemas(schemas),
Expand All @@ -15,15 +15,15 @@ $$;
-- Schemas

create or replace function oas_build_component_schemas(schemas text[])
returns jsonb language sql as
returns jsonb language sql stable as
$$
select oas_build_component_schemas_from_tables(schemas) ||
oas_build_component_schemas_from_composite_types(schemas) ||
oas_build_component_schemas_headers()
$$;

create or replace function oas_build_component_schemas_from_tables(schemas text[])
returns jsonb language sql as
returns jsonb language sql stable as
$$
select jsonb_object_agg(x.table_name, x.oas_schema)
from (
Expand All @@ -40,7 +40,7 @@ from (
$$;

create or replace function oas_build_component_schemas_from_composite_types(schemas text[])
returns jsonb language sql as
returns jsonb language sql stable as
$$
SELECT coalesce(jsonb_object_agg(x.ct_name, x.oas_schema), '{}')
FROM (
Expand All @@ -55,7 +55,7 @@ FROM (
$$;

create or replace function oas_build_component_schemas_headers()
returns jsonb language sql as
returns jsonb language sql stable as
$$
select jsonb_build_object(
'header.preferParams',
Expand Down Expand Up @@ -174,15 +174,15 @@ $$;
-- Parameters

create or replace function oas_build_component_parameters(schemas text[])
returns jsonb language sql as
returns jsonb language sql stable as
$$
select oas_build_component_parameters_query_params_from_tables(schemas) ||
oas_build_component_parameters_query_params_common() ||
oas_build_component_parameters_headers_common();
$$;

create or replace function oas_build_component_parameters_query_params_from_tables(schemas text[])
returns jsonb language sql as
returns jsonb language sql stable as
$$
select jsonb_object_agg(x.param_name, x.param_schema)
from (
Expand All @@ -203,7 +203,7 @@ from (
$$;

create or replace function oas_build_component_parameters_query_params_common()
returns jsonb language sql as
returns jsonb language sql stable as
$$
select jsonb_object_agg(name, param_object) from unnest(
array['select','order', 'limit', 'offset', 'on_conflict', 'columns', 'or', 'and', 'not.or', 'not.and'],
Expand Down Expand Up @@ -305,7 +305,7 @@ select jsonb_object_agg(name, param_object) from unnest(
$$;

create or replace function oas_build_component_parameters_headers_common ()
returns jsonb language sql as
returns jsonb language sql stable as
$$
select jsonb_build_object(
'preferGet',
Expand Down Expand Up @@ -526,14 +526,14 @@ $$;
-- Responses

create or replace function oas_build_response_objects(schemas text[])
returns jsonb language sql as
returns jsonb language sql stable as
$$
select oas_build_response_objects_from_tables(schemas) ||
oas_build_response_objects_common();
$$;

create or replace function oas_build_response_objects_from_tables(schemas text[])
returns jsonb language sql as
returns jsonb language sql stable as
$$
select jsonb_object_agg(x.not_empty, x.not_empty_response) ||
jsonb_object_agg(x.may_be_empty, x.may_be_empty_response)
Expand Down Expand Up @@ -623,7 +623,7 @@ from (
$$;

create or replace function oas_build_response_objects_common()
returns jsonb language sql as
returns jsonb language sql stable as
$$
select jsonb_build_object(
'defaultError',
Expand Down Expand Up @@ -655,13 +655,13 @@ $$;
-- Request bodies

create or replace function oas_build_request_bodies(schemas text[])
returns jsonb language sql as
returns jsonb language sql stable as
$$
select oas_build_request_bodies_from_tables(schemas);
$$;

create or replace function oas_build_request_bodies_from_tables(schemas text[])
returns jsonb language sql as
returns jsonb language sql stable as
$$
select jsonb_object_agg(x.table_name, x.oas_req_body)
from (
Expand Down Expand Up @@ -704,7 +704,7 @@ $$;
-- Security Schemes

create or replace function oas_build_component_security_schemes ()
returns jsonb language sql as
returns jsonb language sql stable as
$$
select jsonb_build_object(
'JWT',
Expand Down
4 changes: 2 additions & 2 deletions sql/main.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ create or replace function callable_root() returns jsonb as $$
proa_version := '0.1'::text, -- TODO: needs to be updated; put into config, and have Makefile update
document_version := 'unset'::text
);
$$ language sql;
$$ language sql volatile;

-- This one returns the OpenAPI JSON; instead of calling it directly, call "callable_root", below
create or replace function postgrest_openapi_spec(
Expand All @@ -19,7 +19,7 @@ create or replace function postgrest_openapi_spec(
proa_version text default null,
document_version text default null
)
returns jsonb language sql as
returns jsonb language sql stable as
$$
select oas_openapi_object(
openapi := '3.1.0',
Expand Down
28 changes: 14 additions & 14 deletions sql/openapi.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ create or replace function oas_openapi_object(
tags jsonb default null,
externalDocs jsonb default null
)
returns jsonb language sql as
returns jsonb language sql stable as
$$
select jsonb_strip_nulls(
jsonb_build_object(
Expand Down Expand Up @@ -47,7 +47,7 @@ create or replace function oas_info_object(
contact jsonb default null,
license jsonb default null
)
returns jsonb language sql as
returns jsonb language sql stable as
$$
select jsonb_build_object(
'title', title,
Expand All @@ -65,7 +65,7 @@ create or replace function oas_x_software_object(
version text,
description text
)
returns jsonb language sql as
returns jsonb language sql stable as
$$
select json_build_object(
'x-name', name,
Expand All @@ -86,7 +86,7 @@ create or replace function oas_components_object(
callbacks jsonb default null,
pathItems jsonb default null
)
returns jsonb language sql as
returns jsonb language sql stable as
$$
select json_build_object(
'schemas', schemas,
Expand Down Expand Up @@ -138,7 +138,7 @@ create or replace function oas_schema_object(
example jsonb default null,
deprecated boolean default null
)
returns jsonb language sql as
returns jsonb language sql stable as
$$
-- TODO: build the JSON object according to the type
select jsonb_build_object(
Expand Down Expand Up @@ -184,7 +184,7 @@ create or replace function oas_reference_object(
summary text default null,
description text default null
)
returns jsonb language sql as
returns jsonb language sql stable as
$$
select json_build_object(
'$ref', ref,
Expand All @@ -206,7 +206,7 @@ create or replace function oas_parameter_object(
example jsonb default null,
examples jsonb default null
)
returns jsonb language sql as
returns jsonb language sql stable as
$$
-- TODO: Add missing logic between fields (e.g. example and examples are mutually exclusive)
select jsonb_build_object(
Expand Down Expand Up @@ -235,7 +235,7 @@ create or replace function oas_security_scheme_object(
flows jsonb default null,
openIdConnectUrl text default null
)
returns jsonb language sql as
returns jsonb language sql stable as
$$
select jsonb_build_object(
'type', type,
Expand All @@ -255,7 +255,7 @@ create or replace function oas_example_object(
value jsonb default null,
externalValue text default null
)
returns jsonb language sql as
returns jsonb language sql stable as
$$
select jsonb_build_object(
'summary', summary,
Expand All @@ -280,7 +280,7 @@ create or replace function oas_path_item_object(
servers jsonb default null,
parameters jsonb default null
)
returns jsonb language sql as
returns jsonb language sql stable as
$$
select json_build_object(
'$ref', ref,
Expand Down Expand Up @@ -313,7 +313,7 @@ create or replace function oas_operation_object(
security jsonb default null,
servers jsonb default null
)
returns jsonb language sql as
returns jsonb language sql stable as
$$
select json_build_object(
'tags', tags,
Expand All @@ -337,7 +337,7 @@ create or replace function oas_response_object(
content jsonb default null,
links jsonb default null
)
returns jsonb language sql as
returns jsonb language sql stable as
$$
select json_build_object(
'description', description,
Expand All @@ -353,7 +353,7 @@ create or replace function oas_media_type_object(
examples jsonb default null,
encoding jsonb default null
)
returns jsonb language sql as
returns jsonb language sql stable as
$$
select json_build_object(
'schema', "schema",
Expand All @@ -368,7 +368,7 @@ create or replace function oas_request_body_object(
description text default null,
required boolean default null
)
returns jsonb language sql as
returns jsonb language sql stable as
$$
select json_build_object(
'content', content,
Expand Down
6 changes: 3 additions & 3 deletions sql/paths.sql
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
-- Functions to build the Paths Object of the OAS document

create or replace function oas_build_paths(schemas text[])
returns jsonb language sql as
returns jsonb language sql stable as
$$
select oas_build_path_item_root() ||
oas_build_path_items_from_tables(schemas);
$$;

create or replace function oas_build_path_items_from_tables(schemas text[])
returns jsonb language sql as
returns jsonb language sql stable as
$$
select jsonb_object_agg(x.path, x.oas_path_item)
from (
Expand Down Expand Up @@ -129,7 +129,7 @@ from (
$$;

create or replace function oas_build_path_item_root()
returns jsonb language sql as
returns jsonb language sql stable as
$$
select
jsonb_build_object(
Expand Down
Loading

0 comments on commit 24c2346

Please sign in to comment.