From 3081ad3ffd2d1ae75f1c02b342019fb046b2bce9 Mon Sep 17 00:00:00 2001 From: Laurence Isla Date: Fri, 16 Feb 2024 20:52:23 -0500 Subject: [PATCH 1/3] Add summary to info object --- sql/main.sql | 5 +++-- sql/postgrest.sql | 43 ++++++++++++++++++++++-------------------- sql/utils.sql | 14 +++++++++++++- test/expected/info.out | 31 +++++++++++++++++++++++++++++- test/fixtures.sql | 2 ++ test/sql/info.sql | 14 +++++++++++++- 6 files changed, 84 insertions(+), 25 deletions(-) diff --git a/sql/main.sql b/sql/main.sql index e30d306..68d6910 100644 --- a/sql/main.sql +++ b/sql/main.sql @@ -24,8 +24,9 @@ $$ select oas_openapi_object( openapi := '3.1.0', info := oas_info_object( - title := coalesce(sd.title, 'PostgREST API'), - description := coalesce(sd.description, 'This is a dynamic API generated by PostgREST'), + title := sd.title, + summary := sd.summary, + description := sd.description, -- The document version version := coalesce(document_version, 'undefined') ), diff --git a/sql/postgrest.sql b/sql/postgrest.sql index c56775d..ac4ef72 100644 --- a/sql/postgrest.sql +++ b/sql/postgrest.sql @@ -113,7 +113,7 @@ WITH else oas_schema_object( description := info.description, - type := pgtype_to_oastype(info.data_type), + type := postgrest_pgtype_to_oastype(info.data_type), format := info.data_type::text, maxlength := info.character_maximum_length, -- "default" := to_jsonb(info.column_default), @@ -126,7 +126,7 @@ WITH oas_build_reference_to_schemas(info.item_data_type) else oas_schema_object( - type := pgtype_to_oastype(info.item_data_type), + type := postgrest_pgtype_to_oastype(info.item_data_type), format := info.item_data_type::text ) end @@ -408,7 +408,7 @@ WITH else oas_schema_object( description := info.description, - type := pgtype_to_oastype(info.data_type), + type := postgrest_pgtype_to_oastype(info.data_type), format := info.data_type::text, maxlength := info.character_maximum_length, -- "default" := to_jsonb(info.column_default), @@ -421,7 +421,7 @@ WITH oas_build_reference_to_schemas(info.item_data_type) else oas_schema_object( - type := pgtype_to_oastype(info.item_data_type), + type := postgrest_pgtype_to_oastype(info.item_data_type), format := info.item_data_type::text ) end @@ -571,7 +571,7 @@ $$ oas_build_reference_to_schemas(info.data_type) else oas_schema_object( - type := pgtype_to_oastype(info.data_type), + type := postgrest_pgtype_to_oastype(info.data_type), format := info.data_type::text, -- TODO: take default values from pg_proc.pronargdefaults -- "default" := to_jsonb(info.arg_default), @@ -584,7 +584,7 @@ $$ oas_build_reference_to_schemas(info.item_data_type) else oas_schema_object( - type := pgtype_to_oastype(info.item_data_type), + type := postgrest_pgtype_to_oastype(info.item_data_type), format := info.item_data_type::text ) end @@ -647,22 +647,25 @@ $$; create or replace function postgrest_get_schema_description(schema text) returns table ( title text, + summary text, description text ) language sql stable as $$ select - substr(sd.schema_desc, 0, break_position) as title, - trim(leading from substr(sd.schema_desc, break_position), ' -') as description -from ( - select - description as schema_desc, - strpos(description, ' -') as break_position - from - pg_namespace n - left join pg_description d on d.objoid = n.oid - where - n.nspname = schema -) sd; + coalesce( + (postgrest_unfold_comment(description))[1], + 'PostgREST API') as title, + (postgrest_unfold_comment( + (postgrest_unfold_comment(description))[2]) + )[1] as summary, + coalesce( + (postgrest_unfold_comment( + (postgrest_unfold_comment(description))[2]) + )[2], + 'This is a dynamic API generated by PostgREST') as description +from + pg_namespace n + left join pg_description d on d.objoid = n.oid +where + n.nspname = schema; $$; diff --git a/sql/utils.sql b/sql/utils.sql index e1efd4a..2be2013 100644 --- a/sql/utils.sql +++ b/sql/utils.sql @@ -1,6 +1,6 @@ -- Functions that help in building the OpenAPI spec inside PostgreSQL -create or replace function pgtype_to_oastype(type text) +create or replace function postgrest_pgtype_to_oastype(type text) returns text language sql immutable as $$ select case when type like any(array['character', 'character varying', 'text']) then 'string' @@ -13,6 +13,18 @@ select case when type like any(array['character', 'character varying', 'text']) else 'string' end; $$; +create or replace function postgrest_unfold_comment(comm text) returns text[] +language sql immutable as +$$ +select array[ + substr(comm, 0, break_position), + trim(leading from substr(comm, break_position), ' + ') -- trims newlines and empty spaces +] +from (select strpos(comm, ' +') as break_position)_; +$$; + create or replace function oas_build_reference_to_schemas("schema" text) returns jsonb language sql immutable as $$ diff --git a/test/expected/info.out b/test/expected/info.out index 9f5b5a4..7e60be5 100644 --- a/test/expected/info.out +++ b/test/expected/info.out @@ -5,11 +5,40 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'info'->'title'); "My API title" (1 row) --- shows the second line of the comment on the schema as description +-- shows the second line of the comment on the schema as summary +select jsonb_pretty(postgrest_openapi_spec('{test}')->'info'->'summary'); + jsonb_pretty +------------------ + "My API summary" +(1 row) + +-- shows the third line of the comment on the schema as description select jsonb_pretty(postgrest_openapi_spec('{test}')->'info'->'description'); jsonb_pretty -------------------------------------------------- "My API description\nthat spans\nmultiple lines" (1 row) +-- shows default title when there is no comment on the schema +select jsonb_pretty(postgrest_openapi_spec('{types}')->'info'->'title'); + jsonb_pretty +----------------- + "PostgREST API" +(1 row) + +-- shows no summary when there is no comment on the schema +select postgrest_openapi_spec('{types}')->'info' ? 'summary'; + ?column? +---------- + f +(1 row) + + +-- shows default description when there is no comment on the schema +select jsonb_pretty(postgrest_openapi_spec('{types}')->'info'->'description'); + jsonb_pretty +------------------------------------------------ + "This is a dynamic API generated by PostgREST" +(1 row) + -- TODO: tests for versions diff --git a/test/fixtures.sql b/test/fixtures.sql index 527d5cb..b99edef 100644 --- a/test/fixtures.sql +++ b/test/fixtures.sql @@ -36,6 +36,8 @@ create schema test; comment on schema test is $$My API title +My API summary + My API description that spans multiple lines$$; diff --git a/test/sql/info.sql b/test/sql/info.sql index d180922..2c0e2db 100644 --- a/test/sql/info.sql +++ b/test/sql/info.sql @@ -1,7 +1,19 @@ -- shows the first line of the comment on the schema as title select jsonb_pretty(postgrest_openapi_spec('{test}')->'info'->'title'); --- shows the second line of the comment on the schema as description +-- shows the second line of the comment on the schema as summary +select jsonb_pretty(postgrest_openapi_spec('{test}')->'info'->'summary'); + +-- shows the third line of the comment on the schema as description select jsonb_pretty(postgrest_openapi_spec('{test}')->'info'->'description'); +-- shows default title when there is no comment on the schema +select jsonb_pretty(postgrest_openapi_spec('{types}')->'info'->'title'); + +-- shows no summary when there is no comment on the schema +select postgrest_openapi_spec('{types}')->'info' ? 'summary'; + +-- shows default description when there is no comment on the schema +select jsonb_pretty(postgrest_openapi_spec('{types}')->'info'->'description'); + -- TODO: tests for versions From 4dbad03e2263674086c5ad2b5b10674ed5af74fb Mon Sep 17 00:00:00 2001 From: Laurence Isla Date: Fri, 16 Feb 2024 20:57:35 -0500 Subject: [PATCH 2/3] Add summary to operation objects (get,patch,etc.) --- sql/paths.sql | 12 ++-- test/expected/paths.out | 118 ++++++++++++++++++++++++++++++++++++++-- test/fixtures.sql | 7 +++ test/sql/paths.sql | 56 +++++++++++++++++-- 4 files changed, 181 insertions(+), 12 deletions(-) diff --git a/sql/paths.sql b/sql/paths.sql index c036c20..70d0bb0 100644 --- a/sql/paths.sql +++ b/sql/paths.sql @@ -15,7 +15,8 @@ from ( select '/' || table_name as path, oas_path_item_object( get :=oas_operation_object( - description := table_description, + summary := (postgrest_unfold_comment(table_description))[1], + description := (postgrest_unfold_comment(table_description))[2], tags := array[table_name], parameters := jsonb_agg( oas_build_reference_to_parameters(format('rowFilter.%1$s.%2$s', table_name, column_name)) @@ -44,7 +45,8 @@ from ( post := case when insertable then oas_operation_object( - description := table_description, + summary := (postgrest_unfold_comment(table_description))[1], + description := (postgrest_unfold_comment(table_description))[2], tags := array[table_name], requestBody := oas_build_reference_to_request_bodies(table_name), parameters := jsonb_build_array( @@ -63,7 +65,8 @@ from ( patch := case when updatable then oas_operation_object( - description := table_description, + summary := (postgrest_unfold_comment(table_description))[1], + description := (postgrest_unfold_comment(table_description))[2], tags := array[table_name], requestBody := oas_build_reference_to_request_bodies(table_name), parameters := jsonb_agg( @@ -93,7 +96,8 @@ from ( delete := case when deletable then oas_operation_object( - description := table_description, + summary := (postgrest_unfold_comment(table_description))[1], + description := (postgrest_unfold_comment(table_description))[2], tags := array[table_name], parameters := jsonb_agg( oas_build_reference_to_parameters(format('rowFilter.%1$s.%2$s', table_name, column_name)) diff --git a/test/expected/paths.out b/test/expected/paths.out index be35a9e..3593680 100644 --- a/test/expected/paths.out +++ b/test/expected/paths.out @@ -40,7 +40,6 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'get ] (1 row) - -- uses a reference for the 200 HTTP code response select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'get'->'responses'->'200'); jsonb_pretty @@ -72,7 +71,7 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'get (1 row) -- uses references for columns as query parameters -select value +select value from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'get'->'parameters') where value->>'$ref' like '#/components/parameters/rowFilter.products.%'; value @@ -85,6 +84,20 @@ where value->>'$ref' like '#/components/parameters/rowFilter.products.%'; {"$ref": "#/components/parameters/rowFilter.products.size"} (6 rows) +-- shows the first line of the comment on the table as summary +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'get'->'summary'); + jsonb_pretty +-------------------- + "Products summary" +(1 row) + +-- shows the second line of the comment on the table as description +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'get'->'description'); + jsonb_pretty +----------------------------------------------------- + "Products description\nthat spans\nmultiple lines." +(1 row) + -- uses references for common parameters select value from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'get'->'parameters') @@ -140,6 +153,20 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'pos "#/components/requestBodies/products" (1 row) +-- shows the first line of the comment on the table as summary +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'post'->'summary'); + jsonb_pretty +-------------------- + "Products summary" +(1 row) + +-- shows the second line of the comment on the table as description +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'post'->'description'); + jsonb_pretty +----------------------------------------------------- + "Products description\nthat spans\nmultiple lines." +(1 row) + -- uses references for common parameters select value from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'post'->'parameters') @@ -198,6 +225,20 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'pat "#/components/requestBodies/products" (1 row) +-- shows the first line of the comment on the table as summary +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'patch'->'summary'); + jsonb_pretty +-------------------- + "Products summary" +(1 row) + +-- shows the second line of the comment on the table as description +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'patch'->'description'); + jsonb_pretty +----------------------------------------------------- + "Products description\nthat spans\nmultiple lines." +(1 row) + -- uses references for columns as query parameters select value from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'patch'->'parameters') @@ -269,6 +310,20 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'del } (1 row) +-- shows the first line of the comment on the table as summary +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'summary'); + jsonb_pretty +-------------------- + "Products summary" +(1 row) + +-- shows the second line of the comment on the table as description +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'description'); + jsonb_pretty +----------------------------------------------------- + "Products description\nthat spans\nmultiple lines." +(1 row) + -- uses references for columns as query parameters select value from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'parameters') @@ -310,7 +365,6 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'-> ] (1 row) - -- uses a reference for the 200 HTTP code response select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'get'->'responses'->'200'); jsonb_pretty @@ -341,8 +395,22 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'-> } (1 row) +-- shows the first line of the comment on the table as summary +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'get'->'summary'); + jsonb_pretty +------------------------ + "Big products summary" +(1 row) + +-- shows the second line of the comment on the table as description +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'get'->'description'); + jsonb_pretty +--------------------------------------------------------- + "Big products description\nthat spans\nmultiple lines." +(1 row) + -- uses references for columns as query parameters -select value +select value from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'get'->'parameters') where value->>'$ref' like '#/components/parameters/rowFilter.big_products.%'; value @@ -415,6 +483,20 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'-> "#/components/requestBodies/big_products" (1 row) +-- shows the first line of the comment on the table as summary +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'post'->'summary'); + jsonb_pretty +------------------------ + "Big products summary" +(1 row) + +-- shows the second line of the comment on the table as description +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'post'->'description'); + jsonb_pretty +--------------------------------------------------------- + "Big products description\nthat spans\nmultiple lines." +(1 row) + -- uses references for common parameters select value from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'post'->'parameters') @@ -480,6 +562,20 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'-> "#/components/requestBodies/big_products" (1 row) +-- shows the first line of the comment on the table as summary +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'patch'->'summary'); + jsonb_pretty +------------------------ + "Big products summary" +(1 row) + +-- shows the second line of the comment on the table as description +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'patch'->'description'); + jsonb_pretty +--------------------------------------------------------- + "Big products description\nthat spans\nmultiple lines." +(1 row) + -- uses references for columns as query parameters select value from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'patch'->'parameters') @@ -556,6 +652,20 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'-> } (1 row) +-- shows the first line of the comment on the table as summary +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'summary'); + jsonb_pretty +------------------------ + "Big products summary" +(1 row) + +-- shows the second line of the comment on the table as description +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'description'); + jsonb_pretty +--------------------------------------------------------- + "Big products description\nthat spans\nmultiple lines." +(1 row) + -- uses references for columns as query parameters select value from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'parameters') diff --git a/test/fixtures.sql b/test/fixtures.sql index b99edef..1dec36b 100644 --- a/test/fixtures.sql +++ b/test/fixtures.sql @@ -89,6 +89,13 @@ create view test.big_products as from test.products where size in ('L', 'XL'); +comment on view test.big_products is +$$Big products summary + +Big products description +that spans +multiple lines.$$; + create view test.non_auto_updatable as select 'this view is not auto updatable' as description; diff --git a/test/sql/paths.sql b/test/sql/paths.sql index 55e8969..0bb3a09 100644 --- a/test/sql/paths.sql +++ b/test/sql/paths.sql @@ -6,7 +6,7 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/'); -- shows the table name as tag select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'get'->'tags'); - + -- uses a reference for the 200 HTTP code response select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'get'->'responses'->'200'); @@ -17,10 +17,16 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'get select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'get'->'responses'->'default'); -- uses references for columns as query parameters -select value +select value from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'get'->'parameters') where value->>'$ref' like '#/components/parameters/rowFilter.products.%'; +-- shows the first line of the comment on the table as summary +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'get'->'summary'); + +-- shows the second line of the comment on the table as description +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'get'->'description'); + -- uses references for common parameters select value from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'get'->'parameters') @@ -40,6 +46,12 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'pos -- uses a reference for request body select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'post'->'requestBody'->'$ref'); +-- shows the first line of the comment on the table as summary +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'post'->'summary'); + +-- shows the second line of the comment on the table as description +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'post'->'description'); + -- uses references for common parameters select value from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'post'->'parameters') @@ -62,6 +74,12 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'pat -- uses a reference for request body select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'patch'->'requestBody'->'$ref'); +-- shows the first line of the comment on the table as summary +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'patch'->'summary'); + +-- shows the second line of the comment on the table as description +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'patch'->'description'); + -- uses references for columns as query parameters select value from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'patch'->'parameters') @@ -86,6 +104,12 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'del -- uses a reference for error responses select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'responses'->'default'); +-- shows the first line of the comment on the table as summary +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'summary'); + +-- shows the second line of the comment on the table as description +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'description'); + -- uses references for columns as query parameters select value from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'parameters') @@ -101,7 +125,7 @@ where value->>'$ref' not like '#/components/parameters/rowFilter.products.%'; -- shows the table name as tag select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'get'->'tags'); - + -- uses a reference for the 200 HTTP code response select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'get'->'responses'->'200'); @@ -111,8 +135,14 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'-> -- uses a reference for error responses select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'get'->'responses'->'default'); +-- shows the first line of the comment on the table as summary +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'get'->'summary'); + +-- shows the second line of the comment on the table as description +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'get'->'description'); + -- uses references for columns as query parameters -select value +select value from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'get'->'parameters') where value->>'$ref' like '#/components/parameters/rowFilter.big_products.%'; @@ -138,6 +168,12 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'-> -- uses a reference for request body select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'post'->'requestBody'->'$ref'); +-- shows the first line of the comment on the table as summary +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'post'->'summary'); + +-- shows the second line of the comment on the table as description +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'post'->'description'); + -- uses references for common parameters select value from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'post'->'parameters') @@ -163,6 +199,12 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'-> -- uses a reference for request body select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'patch'->'requestBody'->'$ref'); +-- shows the first line of the comment on the table as summary +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'patch'->'summary'); + +-- shows the second line of the comment on the table as description +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'patch'->'description'); + -- uses references for columns as query parameters select value from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'patch'->'parameters') @@ -190,6 +232,12 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'-> -- uses a reference for error responses select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'responses'->'default'); +-- shows the first line of the comment on the table as summary +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'summary'); + +-- shows the second line of the comment on the table as description +select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'description'); + -- uses references for columns as query parameters select value from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'parameters') From db7fb28b3d57cd9f0bee579e254d84ca2008a5d4 Mon Sep 17 00:00:00 2001 From: Laurence Isla Date: Wed, 21 Feb 2024 12:29:31 -0500 Subject: [PATCH 3/3] Refactor function to make it more readable --- sql/postgrest.sql | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/sql/postgrest.sql b/sql/postgrest.sql index ac4ef72..82ae7e0 100644 --- a/sql/postgrest.sql +++ b/sql/postgrest.sql @@ -652,20 +652,20 @@ returns table ( ) language sql stable as $$ select + coalesce(title, 'PostgREST API') as title, + (postgrest_unfold_comment(sum_and_desc))[1] as summary, coalesce( - (postgrest_unfold_comment(description))[1], - 'PostgREST API') as title, - (postgrest_unfold_comment( - (postgrest_unfold_comment(description))[2]) - )[1] as summary, - coalesce( - (postgrest_unfold_comment( - (postgrest_unfold_comment(description))[2]) - )[2], - 'This is a dynamic API generated by PostgREST') as description -from - pg_namespace n - left join pg_description d on d.objoid = n.oid -where - n.nspname = schema; + (postgrest_unfold_comment(sum_and_desc))[2], + 'This is a dynamic API generated by PostgREST' + ) as description +from ( + select + (postgrest_unfold_comment(description))[1] as title, + (postgrest_unfold_comment(description))[2] as sum_and_desc + from + pg_namespace n + left join pg_description d on d.objoid = n.oid + where + n.nspname = schema +) _; $$;