Skip to content

Commit

Permalink
Add DELETE requests for tables and views
Browse files Browse the repository at this point in the history
  • Loading branch information
laurenceisla authored Jan 31, 2024
1 parent c0c1352 commit e0bc07c
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ SQL functions to build the OpenAPI output of a PostgREST instance.
- [x] Security scheme (security definitions in OAS 2.0)
- [ ] Parameters
- [ ] Paths object
- [ ] Tables and Views
- [x] Tables and Views
- [x] GET
- [x] POST
- [x] PATCH
- [ ] DELETE
- [x] DELETE
- [ ] Functions
- [ ] GET
- [ ] POST
Expand Down
32 changes: 30 additions & 2 deletions sql/paths.sql
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,42 @@ from (
oas_build_reference_to_responses('defaultError', 'Error')
)
)
end,
delete :=
case when deletable then
oas_operation_object(
description := table_description,
tags := array[table_name],
parameters := jsonb_agg(
oas_build_reference_to_parameters(format('rowFilter.%1$s.%2$s', table_name, column_name))
) ||
jsonb_build_array(
oas_build_reference_to_parameters('select'),
oas_build_reference_to_parameters('order'),
oas_build_reference_to_parameters('limit'),
oas_build_reference_to_parameters('or'),
oas_build_reference_to_parameters('and'),
oas_build_reference_to_parameters('not.or'),
oas_build_reference_to_parameters('not.and'),
oas_build_reference_to_parameters('preferDelete')
),
responses := jsonb_build_object(
'200',
oas_build_reference_to_responses('notEmpty.' || table_name, 'OK'),
'204',
oas_build_reference_to_responses('empty', 'No Content'),
'default',
oas_build_reference_to_responses('defaultError', 'Error')
)
)
end
) as oas_path_item
from (
select table_schema, table_name, table_description, insertable, updatable, unnest(all_cols) as column_name
select table_schema, table_name, table_description, insertable, updatable, deletable, unnest(all_cols) as column_name
from postgrest_get_all_tables(schemas)
) _
where table_schema = any(schemas)
group by table_schema, table_name, table_description, insertable, updatable
group by table_schema, table_name, table_description, insertable, updatable, deletable
) x;
$$;

Expand Down
159 changes: 159 additions & 0 deletions test/expected/paths.out
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'pat
}
(1 row)

-- uses a reference for request body
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'patch'->'requestBody'->'$ref');
jsonb_pretty
---------------------------------------
"#/components/requestBodies/products"
(1 row)

-- uses references for columns as query parameters
select value
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'patch'->'parameters')
Expand Down Expand Up @@ -222,6 +229,76 @@ where value->>'$ref' not like '#/components/parameters/rowFilter.products.%';
{"$ref": "#/components/parameters/preferPatch"}
(9 rows)

-- DELETE operation object
-- shows the table name as tag
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'tags');
jsonb_pretty
----------------
[ +
"products"+
]
(1 row)

-- uses a reference for the 200 HTTP code response
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'responses'->'200');
jsonb_pretty
---------------------------------------------------------
{ +
"$ref": "#/components/responses/notEmpty.products",+
"description": "OK" +
}
(1 row)

-- uses a reference for the 204 HTTP code response
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'responses'->'204');
jsonb_pretty
---------------------------------------------
{ +
"$ref": "#/components/responses/empty",+
"description": "No Content" +
}
(1 row)

-- uses a reference for error responses
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'responses'->'default');
jsonb_pretty
----------------------------------------------------
{ +
"$ref": "#/components/responses/defaultError",+
"description": "Error" +
}
(1 row)

-- uses references for columns as query parameters
select value
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'parameters')
where value->>'$ref' like '#/components/parameters/rowFilter.products.%';
value
--------------------------------------------------------------------
{"$ref": "#/components/parameters/rowFilter.products.id"}
{"$ref": "#/components/parameters/rowFilter.products.code"}
{"$ref": "#/components/parameters/rowFilter.products.name"}
{"$ref": "#/components/parameters/rowFilter.products.description"}
{"$ref": "#/components/parameters/rowFilter.products.attr"}
{"$ref": "#/components/parameters/rowFilter.products.size"}
(6 rows)

-- uses references for common parameters
select value
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'parameters')
where value->>'$ref' not like '#/components/parameters/rowFilter.products.%';
value
--------------------------------------------------
{"$ref": "#/components/parameters/select"}
{"$ref": "#/components/parameters/order"}
{"$ref": "#/components/parameters/limit"}
{"$ref": "#/components/parameters/or"}
{"$ref": "#/components/parameters/and"}
{"$ref": "#/components/parameters/not.or"}
{"$ref": "#/components/parameters/not.and"}
{"$ref": "#/components/parameters/preferDelete"}
(8 rows)

-- Views
-- GET operation object
-- shows the table name as tag
Expand Down Expand Up @@ -396,6 +473,13 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->
}
(1 row)

-- uses a reference for request body
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'patch'->'requestBody'->'$ref');
jsonb_pretty
-------------------------------------------
"#/components/requestBodies/big_products"
(1 row)

-- uses references for columns as query parameters
select value
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'patch'->'parameters')
Expand Down Expand Up @@ -432,3 +516,78 @@ select postgrest_openapi_spec('{test}')->'paths'->'/non_auto_updatable' ? 'patch
f
(1 row)

-- DELETE operation object
-- shows the table name as tag
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'tags');
jsonb_pretty
--------------------
[ +
"big_products"+
]
(1 row)

-- uses a reference for the 200 HTTP code response
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'responses'->'200');
jsonb_pretty
-------------------------------------------------------------
{ +
"$ref": "#/components/responses/notEmpty.big_products",+
"description": "OK" +
}
(1 row)

-- uses a reference for the 204 HTTP code response
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'responses'->'204');
jsonb_pretty
---------------------------------------------
{ +
"$ref": "#/components/responses/empty",+
"description": "No Content" +
}
(1 row)

-- uses a reference for error responses
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'responses'->'default');
jsonb_pretty
----------------------------------------------------
{ +
"$ref": "#/components/responses/defaultError",+
"description": "Error" +
}
(1 row)

-- uses references for columns as query parameters
select value
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'parameters')
where value->>'$ref' like '#/components/parameters/rowFilter.big_products.%';
value
-----------------------------------------------------------------
{"$ref": "#/components/parameters/rowFilter.big_products.id"}
{"$ref": "#/components/parameters/rowFilter.big_products.code"}
{"$ref": "#/components/parameters/rowFilter.big_products.name"}
{"$ref": "#/components/parameters/rowFilter.big_products.size"}
(4 rows)

-- uses references for common parameters
select value
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'parameters')
where value->>'$ref' not like '#/components/parameters/rowFilter.big_products.%';
value
--------------------------------------------------
{"$ref": "#/components/parameters/select"}
{"$ref": "#/components/parameters/order"}
{"$ref": "#/components/parameters/limit"}
{"$ref": "#/components/parameters/or"}
{"$ref": "#/components/parameters/and"}
{"$ref": "#/components/parameters/not.or"}
{"$ref": "#/components/parameters/not.and"}
{"$ref": "#/components/parameters/preferDelete"}
(8 rows)

-- does not show a DELETE operation object for non auto-updatable views
select postgrest_openapi_spec('{test}')->'paths'->'/non_auto_updatable' ? 'delete' as value;
value
-------
f
(1 row)

57 changes: 57 additions & 0 deletions test/sql/paths.sql
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'pat
-- uses a reference for error responses
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'patch'->'responses'->'default');

-- uses a reference for request body
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'patch'->'requestBody'->'$ref');

-- uses references for columns as query parameters
select value
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'patch'->'parameters')
Expand All @@ -69,6 +72,30 @@ select value
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'patch'->'parameters')
where value->>'$ref' not like '#/components/parameters/rowFilter.products.%';

-- DELETE operation object

-- shows the table name as tag
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'tags');

-- uses a reference for the 200 HTTP code response
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'responses'->'200');

-- uses a reference for the 204 HTTP code response
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'responses'->'204');

-- uses a reference for error responses
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'responses'->'default');

-- uses references for columns as query parameters
select value
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'parameters')
where value->>'$ref' like '#/components/parameters/rowFilter.products.%';

-- uses references for common parameters
select value
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'parameters')
where value->>'$ref' not like '#/components/parameters/rowFilter.products.%';

-- Views
-- GET operation object

Expand Down Expand Up @@ -133,6 +160,9 @@ 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'->'patch'->'responses'->'default');

-- uses a reference for request body
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'patch'->'requestBody'->'$ref');

-- uses references for columns as query parameters
select value
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'patch'->'parameters')
Expand All @@ -145,3 +175,30 @@ where value->>'$ref' not like '#/components/parameters/rowFilter.big_products.%'

-- does not show a PATCH operation object for non auto-updatable views
select postgrest_openapi_spec('{test}')->'paths'->'/non_auto_updatable' ? 'patch' as value;

-- DELETE operation object

-- shows the table name as tag
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'tags');

-- uses a reference for the 200 HTTP code response
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'responses'->'200');

-- uses a reference for the 204 HTTP code response
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'responses'->'204');

-- uses a reference for error responses
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'responses'->'default');

-- uses references for columns as query parameters
select value
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'parameters')
where value->>'$ref' like '#/components/parameters/rowFilter.big_products.%';

-- uses references for common parameters
select value
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'parameters')
where value->>'$ref' not like '#/components/parameters/rowFilter.big_products.%';

-- does not show a DELETE operation object for non auto-updatable views
select postgrest_openapi_spec('{test}')->'paths'->'/non_auto_updatable' ? 'delete' as value;

0 comments on commit e0bc07c

Please sign in to comment.