Skip to content

Commit e0bc07c

Browse files
authored
Add DELETE requests for tables and views
1 parent c0c1352 commit e0bc07c

File tree

4 files changed

+248
-4
lines changed

4 files changed

+248
-4
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ SQL functions to build the OpenAPI output of a PostgREST instance.
1212
- [x] Security scheme (security definitions in OAS 2.0)
1313
- [ ] Parameters
1414
- [ ] Paths object
15-
- [ ] Tables and Views
15+
- [x] Tables and Views
1616
- [x] GET
1717
- [x] POST
1818
- [x] PATCH
19-
- [ ] DELETE
19+
- [x] DELETE
2020
- [ ] Functions
2121
- [ ] GET
2222
- [ ] POST

sql/paths.sql

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,42 @@ from (
8989
oas_build_reference_to_responses('defaultError', 'Error')
9090
)
9191
)
92+
end,
93+
delete :=
94+
case when deletable then
95+
oas_operation_object(
96+
description := table_description,
97+
tags := array[table_name],
98+
parameters := jsonb_agg(
99+
oas_build_reference_to_parameters(format('rowFilter.%1$s.%2$s', table_name, column_name))
100+
) ||
101+
jsonb_build_array(
102+
oas_build_reference_to_parameters('select'),
103+
oas_build_reference_to_parameters('order'),
104+
oas_build_reference_to_parameters('limit'),
105+
oas_build_reference_to_parameters('or'),
106+
oas_build_reference_to_parameters('and'),
107+
oas_build_reference_to_parameters('not.or'),
108+
oas_build_reference_to_parameters('not.and'),
109+
oas_build_reference_to_parameters('preferDelete')
110+
),
111+
responses := jsonb_build_object(
112+
'200',
113+
oas_build_reference_to_responses('notEmpty.' || table_name, 'OK'),
114+
'204',
115+
oas_build_reference_to_responses('empty', 'No Content'),
116+
'default',
117+
oas_build_reference_to_responses('defaultError', 'Error')
118+
)
119+
)
92120
end
93121
) as oas_path_item
94122
from (
95-
select table_schema, table_name, table_description, insertable, updatable, unnest(all_cols) as column_name
123+
select table_schema, table_name, table_description, insertable, updatable, deletable, unnest(all_cols) as column_name
96124
from postgrest_get_all_tables(schemas)
97125
) _
98126
where table_schema = any(schemas)
99-
group by table_schema, table_name, table_description, insertable, updatable
127+
group by table_schema, table_name, table_description, insertable, updatable, deletable
100128
) x;
101129
$$;
102130

test/expected/paths.out

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'pat
191191
}
192192
(1 row)
193193

194+
-- uses a reference for request body
195+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'patch'->'requestBody'->'$ref');
196+
jsonb_pretty
197+
---------------------------------------
198+
"#/components/requestBodies/products"
199+
(1 row)
200+
194201
-- uses references for columns as query parameters
195202
select value
196203
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'patch'->'parameters')
@@ -222,6 +229,76 @@ where value->>'$ref' not like '#/components/parameters/rowFilter.products.%';
222229
{"$ref": "#/components/parameters/preferPatch"}
223230
(9 rows)
224231

232+
-- DELETE operation object
233+
-- shows the table name as tag
234+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'tags');
235+
jsonb_pretty
236+
----------------
237+
[ +
238+
"products"+
239+
]
240+
(1 row)
241+
242+
-- uses a reference for the 200 HTTP code response
243+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'responses'->'200');
244+
jsonb_pretty
245+
---------------------------------------------------------
246+
{ +
247+
"$ref": "#/components/responses/notEmpty.products",+
248+
"description": "OK" +
249+
}
250+
(1 row)
251+
252+
-- uses a reference for the 204 HTTP code response
253+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'responses'->'204');
254+
jsonb_pretty
255+
---------------------------------------------
256+
{ +
257+
"$ref": "#/components/responses/empty",+
258+
"description": "No Content" +
259+
}
260+
(1 row)
261+
262+
-- uses a reference for error responses
263+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'responses'->'default');
264+
jsonb_pretty
265+
----------------------------------------------------
266+
{ +
267+
"$ref": "#/components/responses/defaultError",+
268+
"description": "Error" +
269+
}
270+
(1 row)
271+
272+
-- uses references for columns as query parameters
273+
select value
274+
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'parameters')
275+
where value->>'$ref' like '#/components/parameters/rowFilter.products.%';
276+
value
277+
--------------------------------------------------------------------
278+
{"$ref": "#/components/parameters/rowFilter.products.id"}
279+
{"$ref": "#/components/parameters/rowFilter.products.code"}
280+
{"$ref": "#/components/parameters/rowFilter.products.name"}
281+
{"$ref": "#/components/parameters/rowFilter.products.description"}
282+
{"$ref": "#/components/parameters/rowFilter.products.attr"}
283+
{"$ref": "#/components/parameters/rowFilter.products.size"}
284+
(6 rows)
285+
286+
-- uses references for common parameters
287+
select value
288+
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'parameters')
289+
where value->>'$ref' not like '#/components/parameters/rowFilter.products.%';
290+
value
291+
--------------------------------------------------
292+
{"$ref": "#/components/parameters/select"}
293+
{"$ref": "#/components/parameters/order"}
294+
{"$ref": "#/components/parameters/limit"}
295+
{"$ref": "#/components/parameters/or"}
296+
{"$ref": "#/components/parameters/and"}
297+
{"$ref": "#/components/parameters/not.or"}
298+
{"$ref": "#/components/parameters/not.and"}
299+
{"$ref": "#/components/parameters/preferDelete"}
300+
(8 rows)
301+
225302
-- Views
226303
-- GET operation object
227304
-- shows the table name as tag
@@ -396,6 +473,13 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->
396473
}
397474
(1 row)
398475

476+
-- uses a reference for request body
477+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'patch'->'requestBody'->'$ref');
478+
jsonb_pretty
479+
-------------------------------------------
480+
"#/components/requestBodies/big_products"
481+
(1 row)
482+
399483
-- uses references for columns as query parameters
400484
select value
401485
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'patch'->'parameters')
@@ -432,3 +516,78 @@ select postgrest_openapi_spec('{test}')->'paths'->'/non_auto_updatable' ? 'patch
432516
f
433517
(1 row)
434518

519+
-- DELETE operation object
520+
-- shows the table name as tag
521+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'tags');
522+
jsonb_pretty
523+
--------------------
524+
[ +
525+
"big_products"+
526+
]
527+
(1 row)
528+
529+
-- uses a reference for the 200 HTTP code response
530+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'responses'->'200');
531+
jsonb_pretty
532+
-------------------------------------------------------------
533+
{ +
534+
"$ref": "#/components/responses/notEmpty.big_products",+
535+
"description": "OK" +
536+
}
537+
(1 row)
538+
539+
-- uses a reference for the 204 HTTP code response
540+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'responses'->'204');
541+
jsonb_pretty
542+
---------------------------------------------
543+
{ +
544+
"$ref": "#/components/responses/empty",+
545+
"description": "No Content" +
546+
}
547+
(1 row)
548+
549+
-- uses a reference for error responses
550+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'responses'->'default');
551+
jsonb_pretty
552+
----------------------------------------------------
553+
{ +
554+
"$ref": "#/components/responses/defaultError",+
555+
"description": "Error" +
556+
}
557+
(1 row)
558+
559+
-- uses references for columns as query parameters
560+
select value
561+
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'parameters')
562+
where value->>'$ref' like '#/components/parameters/rowFilter.big_products.%';
563+
value
564+
-----------------------------------------------------------------
565+
{"$ref": "#/components/parameters/rowFilter.big_products.id"}
566+
{"$ref": "#/components/parameters/rowFilter.big_products.code"}
567+
{"$ref": "#/components/parameters/rowFilter.big_products.name"}
568+
{"$ref": "#/components/parameters/rowFilter.big_products.size"}
569+
(4 rows)
570+
571+
-- uses references for common parameters
572+
select value
573+
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'parameters')
574+
where value->>'$ref' not like '#/components/parameters/rowFilter.big_products.%';
575+
value
576+
--------------------------------------------------
577+
{"$ref": "#/components/parameters/select"}
578+
{"$ref": "#/components/parameters/order"}
579+
{"$ref": "#/components/parameters/limit"}
580+
{"$ref": "#/components/parameters/or"}
581+
{"$ref": "#/components/parameters/and"}
582+
{"$ref": "#/components/parameters/not.or"}
583+
{"$ref": "#/components/parameters/not.and"}
584+
{"$ref": "#/components/parameters/preferDelete"}
585+
(8 rows)
586+
587+
-- does not show a DELETE operation object for non auto-updatable views
588+
select postgrest_openapi_spec('{test}')->'paths'->'/non_auto_updatable' ? 'delete' as value;
589+
value
590+
-------
591+
f
592+
(1 row)
593+

test/sql/paths.sql

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'pat
5959
-- uses a reference for error responses
6060
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'patch'->'responses'->'default');
6161

62+
-- uses a reference for request body
63+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'patch'->'requestBody'->'$ref');
64+
6265
-- uses references for columns as query parameters
6366
select value
6467
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'patch'->'parameters')
@@ -69,6 +72,30 @@ select value
6972
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'patch'->'parameters')
7073
where value->>'$ref' not like '#/components/parameters/rowFilter.products.%';
7174

75+
-- DELETE operation object
76+
77+
-- shows the table name as tag
78+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'tags');
79+
80+
-- uses a reference for the 200 HTTP code response
81+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'responses'->'200');
82+
83+
-- uses a reference for the 204 HTTP code response
84+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'responses'->'204');
85+
86+
-- uses a reference for error responses
87+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'responses'->'default');
88+
89+
-- uses references for columns as query parameters
90+
select value
91+
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'parameters')
92+
where value->>'$ref' like '#/components/parameters/rowFilter.products.%';
93+
94+
-- uses references for common parameters
95+
select value
96+
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/products'->'delete'->'parameters')
97+
where value->>'$ref' not like '#/components/parameters/rowFilter.products.%';
98+
7299
-- Views
73100
-- GET operation object
74101

@@ -133,6 +160,9 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->
133160
-- uses a reference for error responses
134161
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'patch'->'responses'->'default');
135162

163+
-- uses a reference for request body
164+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'patch'->'requestBody'->'$ref');
165+
136166
-- uses references for columns as query parameters
137167
select value
138168
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'patch'->'parameters')
@@ -145,3 +175,30 @@ where value->>'$ref' not like '#/components/parameters/rowFilter.big_products.%'
145175

146176
-- does not show a PATCH operation object for non auto-updatable views
147177
select postgrest_openapi_spec('{test}')->'paths'->'/non_auto_updatable' ? 'patch' as value;
178+
179+
-- DELETE operation object
180+
181+
-- shows the table name as tag
182+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'tags');
183+
184+
-- uses a reference for the 200 HTTP code response
185+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'responses'->'200');
186+
187+
-- uses a reference for the 204 HTTP code response
188+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'responses'->'204');
189+
190+
-- uses a reference for error responses
191+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'responses'->'default');
192+
193+
-- uses references for columns as query parameters
194+
select value
195+
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'parameters')
196+
where value->>'$ref' like '#/components/parameters/rowFilter.big_products.%';
197+
198+
-- uses references for common parameters
199+
select value
200+
from jsonb_array_elements(postgrest_openapi_spec('{test}')->'paths'->'/big_products'->'delete'->'parameters')
201+
where value->>'$ref' not like '#/components/parameters/rowFilter.big_products.%';
202+
203+
-- does not show a DELETE operation object for non auto-updatable views
204+
select postgrest_openapi_spec('{test}')->'paths'->'/non_auto_updatable' ? 'delete' as value;

0 commit comments

Comments
 (0)