Skip to content

Commit

Permalink
Fix Reference Object funcion definition
Browse files Browse the repository at this point in the history
Co-authored-by: Steve Chavez <[email protected]>
  • Loading branch information
laurenceisla and steve-chavez authored Jan 19, 2024
1 parent 9d8dfec commit 4956232
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 39 deletions.
64 changes: 32 additions & 32 deletions sql/components.sql
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ select jsonb_build_object(
description := 'Specify a required or optional behavior for the request',
"schema" := oas_schema_object(
allOf := jsonb_build_array(
oas_reference_object('header.preferHandling'),
oas_reference_object('header.preferTimezone'),
oas_reference_object('header.preferCount')
oas_build_reference_to_schemas('header.preferHandling'),
oas_build_reference_to_schemas('header.preferTimezone'),
oas_build_reference_to_schemas('header.preferCount')
)
),
examples := jsonb_build_object(
Expand All @@ -321,13 +321,13 @@ select jsonb_build_object(
description := 'Specify a required or optional behavior for the request',
"schema" := oas_schema_object(
allOf := jsonb_build_array(
oas_reference_object('header.preferHandling'),
oas_reference_object('header.preferTimezone'),
oas_reference_object('header.preferReturn'),
oas_reference_object('header.preferCount'),
oas_reference_object('header.preferResolution'),
oas_reference_object('header.preferMissing'),
oas_reference_object('header.preferTx')
oas_build_reference_to_schemas('header.preferHandling'),
oas_build_reference_to_schemas('header.preferTimezone'),
oas_build_reference_to_schemas('header.preferReturn'),
oas_build_reference_to_schemas('header.preferCount'),
oas_build_reference_to_schemas('header.preferResolution'),
oas_build_reference_to_schemas('header.preferMissing'),
oas_build_reference_to_schemas('header.preferTx')
)
),
examples := jsonb_build_object(
Expand Down Expand Up @@ -358,12 +358,12 @@ select jsonb_build_object(
description := 'Specify a required or optional behavior for the request',
"schema" := oas_schema_object(
allOf := jsonb_build_array(
oas_reference_object('header.preferHandling'),
oas_reference_object('header.preferTimezone'),
oas_reference_object('header.preferReturn'),
oas_reference_object('header.preferCount'),
oas_reference_object('header.preferTx'),
oas_reference_object('header.preferMaxAffected')
oas_build_reference_to_schemas('header.preferHandling'),
oas_build_reference_to_schemas('header.preferTimezone'),
oas_build_reference_to_schemas('header.preferReturn'),
oas_build_reference_to_schemas('header.preferCount'),
oas_build_reference_to_schemas('header.preferTx'),
oas_build_reference_to_schemas('header.preferMaxAffected')
)
),
examples := jsonb_build_object(
Expand Down Expand Up @@ -393,11 +393,11 @@ select jsonb_build_object(
description := 'Specify a required or optional behavior for the request',
"schema" := oas_schema_object(
allOf := jsonb_build_array(
oas_reference_object('header.preferHandling'),
oas_reference_object('header.preferTimezone'),
oas_reference_object('header.preferReturn'),
oas_reference_object('header.preferCount'),
oas_reference_object('header.preferTx')
oas_build_reference_to_schemas('header.preferHandling'),
oas_build_reference_to_schemas('header.preferTimezone'),
oas_build_reference_to_schemas('header.preferReturn'),
oas_build_reference_to_schemas('header.preferCount'),
oas_build_reference_to_schemas('header.preferTx')
)
),
examples := jsonb_build_object(
Expand Down Expand Up @@ -426,12 +426,12 @@ select jsonb_build_object(
description := 'Specify a required or optional behavior for the request',
"schema" := oas_schema_object(
allOf := jsonb_build_array(
oas_reference_object('header.preferHandling'),
oas_reference_object('header.preferTimezone'),
oas_reference_object('header.preferReturn'),
oas_reference_object('header.preferCount'),
oas_reference_object('header.preferTx'),
oas_reference_object('header.preferMaxAffected')
oas_build_reference_to_schemas('header.preferHandling'),
oas_build_reference_to_schemas('header.preferTimezone'),
oas_build_reference_to_schemas('header.preferReturn'),
oas_build_reference_to_schemas('header.preferCount'),
oas_build_reference_to_schemas('header.preferTx'),
oas_build_reference_to_schemas('header.preferMaxAffected')
)
),
examples := jsonb_build_object(
Expand Down Expand Up @@ -461,11 +461,11 @@ select jsonb_build_object(
description := 'Specify a required or optional behavior for the request',
"schema" := oas_schema_object(
allOf := jsonb_build_array(
oas_reference_object('header.preferHandling'),
oas_reference_object('header.preferTimezone'),
oas_reference_object('header.preferCount'),
oas_reference_object('header.preferTx'),
oas_reference_object('header.preferParams')
oas_build_reference_to_schemas('header.preferHandling'),
oas_build_reference_to_schemas('header.preferTimezone'),
oas_build_reference_to_schemas('header.preferCount'),
oas_build_reference_to_schemas('header.preferTx'),
oas_build_reference_to_schemas('header.preferParams')
)
),
examples := jsonb_build_object(
Expand Down
11 changes: 8 additions & 3 deletions sql/openapi.sql
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,17 @@ $$
)
$$;

-- TODO: should be a Reference Object and not only for "components/schemas"
create or replace function oas_reference_object(ref text)
create or replace function oas_reference_object(
ref text,
summary text default null,
description text default null
)
returns jsonb language sql as
$$
select json_build_object(
'$ref', '#/components/schemas/' || ref
'$ref', ref,
'summary', summary,
'description', description
);
$$;

Expand Down
8 changes: 4 additions & 4 deletions sql/postgrest.sql
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ WITH
jsonb_object_agg(
info.column_name,
case when info.is_composite then
oas_reference_object(info.data_type)
oas_build_reference_to_schemas(info.data_type)
else
oas_schema_object(
description := info.description,
Expand All @@ -120,7 +120,7 @@ WITH
when not info.is_array then
null
when info.item_is_composite then
oas_reference_object(info.item_data_type)
oas_build_reference_to_schemas(info.item_data_type)
else
oas_schema_object(
type := pgtype_to_oastype(info.item_data_type),
Expand Down Expand Up @@ -396,7 +396,7 @@ WITH
jsonb_object_agg(
info.column_name,
case when info.is_composite then
oas_reference_object(info.data_type)
oas_build_reference_to_schemas(info.data_type)
else
oas_schema_object(
description := info.description,
Expand All @@ -410,7 +410,7 @@ WITH
when not info.is_array then
null
when info.item_is_composite then
oas_reference_object(info.item_data_type)
oas_build_reference_to_schemas(info.item_data_type)
else
oas_schema_object(
type := pgtype_to_oastype(info.item_data_type),
Expand Down
8 changes: 8 additions & 0 deletions sql/utils.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ select case when type like any(array['character', 'character varying', 'text'])
when type like 'jsonb' then 'object'
else 'string' end;
$$;

create or replace function oas_build_reference_to_schemas("schema" text)
returns jsonb language sql immutable as
$$
select oas_reference_object(
'#/components/schemas/' || "schema"
);
$$;

0 comments on commit 4956232

Please sign in to comment.