Skip to content

Commit

Permalink
Add common query params to components' parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
laurenceisla committed Dec 22, 2023
1 parent 997735a commit 8ac1f41
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 3 deletions.
4 changes: 2 additions & 2 deletions sql/main.sql
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ select openapi_object(
servers := openapi_server_objects(),
paths := '{}',
components := openapi_components_object(
schemas := postgrest_tables_to_openapi_schema_components(schemas) || postgrest_composite_types_to_openapi_schema_components(schemas)
schemas := postgrest_tables_to_openapi_schema_components(schemas) || postgrest_composite_types_to_openapi_schema_components(schemas),
parameters := postgrest_get_query_params()
)
)
from postgrest_get_schema_description(schemas[1]) sd;
$$;

37 changes: 36 additions & 1 deletion sql/openapi.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
-- Functions to build the the OpenAPI output
-- Functions and types to build the the OpenAPI output

-- TODO: create enum for openapi type e.g. string, number, etc.
create type parameter_object_in as enum ('query', 'header', 'path', 'cookie');
create type parameter_object_style as enum ('simple', 'form');

create or replace function openapi_object(
openapi text,
Expand Down Expand Up @@ -180,3 +184,34 @@ select json_build_object(
'$ref', '#/components/schemas/' || ref
);
$$;

create or replace function openapi_parameter_object(
name text,
"in" parameter_object_in,
description text default null,
required boolean default null,
deprecated boolean default null,
allowEmptyValue boolean default null,
style parameter_object_style default null,
explode boolean default null,
"schema" jsonb default null,
example boolean default null,
examples text default null
)
returns jsonb language sql as
$$
-- TODO: Add missing logic between fields (e.g. example and examples are mutually exclusive)
select jsonb_build_object(
'name', name,
'in', "in",
'description', description,
'required', required,
'deprecated', deprecated,
'allowEmptyValue', allowEmptyValue,
'style', style,
'explode', explode,
'schema', "schema",
'example', example,
'examples', examples
)
$$;
66 changes: 66 additions & 0 deletions sql/postgrest.sql
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,69 @@ returns text language sql as
$$
select '11.0.1 (4197d2f)'
$$;

create or replace function postgrest_get_query_params ()
returns jsonb language sql as
$$
select jsonb_agg(param_object) from unnest(
array['select','order', 'limit', 'offset', 'on_conflict', 'columns'],
array[
openapi_parameter_object(
name := 'select',
"in" := 'query',
description := 'Vertical filtering of columns',
explode := false,
schema := openapi_schema_object(
type := 'array',
items := openapi_schema_object(type := 'string')
)
),
openapi_parameter_object(
name := 'order',
"in" := 'query',
description := 'Ordering by column',
explode := false,
schema := openapi_schema_object(
type := 'array',
items := openapi_schema_object(type := 'string')
)
),
openapi_parameter_object(
name := 'limit',
"in" := 'query',
description := 'Limit the number of rows returned',
explode := false,
schema := openapi_schema_object(
type := 'integer'
)
),
openapi_parameter_object(
name := 'offset',
"in" := 'query',
description := 'Skip a certain number of rows',
explode := false,
schema := openapi_schema_object(
type := 'integer'
)
),
openapi_parameter_object(
name := 'on_conflict',
"in" := 'query',
description := 'Columns that resolve the upsert conflict',
explode := false,
schema := openapi_schema_object(
type := 'string'
)
),
openapi_parameter_object(
name := 'columns',
"in" := 'query',
description := 'Specify the only keys from the payload that will be inserted',
explode := false,
schema := openapi_schema_object(
type := 'string'
)
)
]
) as _(name, param_object);
$$;

0 comments on commit 8ac1f41

Please sign in to comment.