Skip to content

Commit 7d824af

Browse files
committed
feat: add Response Objects for function return types
* Adds media type responses for the different return types of functions (RETURNS TABLE, RETURNS <type>, INOUT/OUT arguments). * The `application/json` media type returns `array` only when the function is `SET OF`.
1 parent c47b3f6 commit 7d824af

File tree

3 files changed

+305
-0
lines changed

3 files changed

+305
-0
lines changed

sql/components.sql

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ create or replace function oas_build_response_objects(schemas text[])
695695
returns jsonb language sql stable as
696696
$$
697697
select oas_build_response_objects_from_tables(schemas) ||
698+
oas_build_response_objects_from_function_return_types(schemas) ||
698699
oas_build_response_objects_common();
699700
$$;
700701

@@ -790,6 +791,62 @@ from (
790791
) as x
791792
$$;
792793

794+
create or replace function oas_build_response_objects_from_function_return_types(schemas text[])
795+
returns jsonb language sql stable as
796+
$$
797+
select jsonb_object_agg(x.not_empty, x.not_empty_response)
798+
from (
799+
select 'rpc.' || function_full_name as not_empty,
800+
oas_response_object(
801+
description := 'Media types for RPC ' || function_full_name,
802+
content := jsonb_build_object(
803+
'application/json',
804+
case when return_type_is_set then
805+
oas_media_type_object(
806+
schema := oas_schema_object(
807+
type := 'array',
808+
items := return_type_reference_schema
809+
)
810+
)
811+
else
812+
oas_media_type_object(
813+
schema := return_type_reference_schema
814+
)
815+
end,
816+
'application/vnd.pgrst.object+json',
817+
oas_media_type_object(
818+
schema := return_type_reference_schema
819+
),
820+
'application/vnd.pgrst.object+json;nulls=stripped',
821+
oas_media_type_object(
822+
schema := return_type_reference_schema
823+
),
824+
'text/csv',
825+
oas_media_type_object(
826+
schema := oas_schema_object(
827+
type := 'string',
828+
format := 'csv'
829+
)
830+
)
831+
)
832+
) as not_empty_response
833+
from (
834+
select *,
835+
-- Build the reference either to the table/composite return type or the non-composite return type
836+
case when return_type_is_composite then
837+
oas_build_reference_to_schemas(return_type_composite_full_name)
838+
else
839+
oas_build_reference_to_schemas('rpc.' || function_full_name)
840+
end as return_type_reference_schema
841+
from (
842+
select function_full_name, return_type_is_set, return_type_is_composite, return_type_composite_full_name
843+
from postgrest_get_all_functions(schemas)
844+
group by function_full_name, return_type_is_set, return_type_is_composite, return_type_composite_full_name
845+
) _
846+
) _
847+
) as x
848+
$$;
849+
793850
create or replace function oas_build_response_objects_common()
794851
returns jsonb language sql stable as
795852
$$

test/expected/responses.out

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,194 @@ select postgrest_openapi_spec('{test}')->'components'->'responses' ? 'mayBeEmpty
290290
f
291291
(1 row)
292292

293+
-- Functions
294+
-- Returning composite types
295+
-- defines an application/json response
296+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.get_attribute'->'content'->'application/json');
297+
jsonb_pretty
298+
------------------------------------------------------------
299+
{ +
300+
"schema": { +
301+
"$ref": "#/components/schemas/types.attribute_ret"+
302+
} +
303+
}
304+
(1 row)
305+
306+
-- defines an application/vnd.pgrst.object+json response
307+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.get_attribute'->'content'->'application/vnd.pgrst.object+json');
308+
jsonb_pretty
309+
------------------------------------------------------------
310+
{ +
311+
"schema": { +
312+
"$ref": "#/components/schemas/types.attribute_ret"+
313+
} +
314+
}
315+
(1 row)
316+
317+
-- defines an application/vnd.pgrst.object+json;nulls=stripped response
318+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.get_attribute'->'content'->'application/vnd.pgrst.object+json;nulls=stripped');
319+
jsonb_pretty
320+
------------------------------------------------------------
321+
{ +
322+
"schema": { +
323+
"$ref": "#/components/schemas/types.attribute_ret"+
324+
} +
325+
}
326+
(1 row)
327+
328+
-- defines a text/csv response
329+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.get_attribute'->'content'->'text/csv');
330+
jsonb_pretty
331+
---------------------------
332+
{ +
333+
"schema": { +
334+
"type": "string",+
335+
"format": "csv" +
336+
} +
337+
}
338+
(1 row)
339+
340+
-- Returning SET OF composite types
341+
-- defines an application/json response
342+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.get_products_by_size'->'content'->'application/json');
343+
jsonb_pretty
344+
-----------------------------------------------------
345+
{ +
346+
"schema": { +
347+
"type": "array", +
348+
"items": { +
349+
"$ref": "#/components/schemas/products"+
350+
} +
351+
} +
352+
}
353+
(1 row)
354+
355+
-- defines an application/vnd.pgrst.object+json response
356+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.get_products_by_size'->'content'->'application/vnd.pgrst.object+json');
357+
jsonb_pretty
358+
-------------------------------------------------
359+
{ +
360+
"schema": { +
361+
"$ref": "#/components/schemas/products"+
362+
} +
363+
}
364+
(1 row)
365+
366+
-- defines an application/vnd.pgrst.object+json;nulls=stripped response
367+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.get_products_by_size'->'content'->'application/vnd.pgrst.object+json;nulls=stripped');
368+
jsonb_pretty
369+
-------------------------------------------------
370+
{ +
371+
"schema": { +
372+
"$ref": "#/components/schemas/products"+
373+
} +
374+
}
375+
(1 row)
376+
377+
-- defines a text/csv response
378+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.get_products_by_size'->'content'->'text/csv');
379+
jsonb_pretty
380+
---------------------------
381+
{ +
382+
"schema": { +
383+
"type": "string",+
384+
"format": "csv" +
385+
} +
386+
}
387+
(1 row)
388+
389+
-- Returning non-composite types
390+
-- defines an application/json response
391+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.returns_inout_out'->'content'->'application/json');
392+
jsonb_pretty
393+
--------------------------------------------------------------
394+
{ +
395+
"schema": { +
396+
"$ref": "#/components/schemas/rpc.returns_inout_out"+
397+
} +
398+
}
399+
(1 row)
400+
401+
-- defines an application/vnd.pgrst.object+json response
402+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.returns_inout_out'->'content'->'application/vnd.pgrst.object+json');
403+
jsonb_pretty
404+
--------------------------------------------------------------
405+
{ +
406+
"schema": { +
407+
"$ref": "#/components/schemas/rpc.returns_inout_out"+
408+
} +
409+
}
410+
(1 row)
411+
412+
-- defines an application/vnd.pgrst.object+json;nulls=stripped response
413+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.returns_inout_out'->'content'->'application/vnd.pgrst.object+json;nulls=stripped');
414+
jsonb_pretty
415+
--------------------------------------------------------------
416+
{ +
417+
"schema": { +
418+
"$ref": "#/components/schemas/rpc.returns_inout_out"+
419+
} +
420+
}
421+
(1 row)
422+
423+
-- defines a text/csv response
424+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.returns_inout_out'->'content'->'text/csv');
425+
jsonb_pretty
426+
---------------------------
427+
{ +
428+
"schema": { +
429+
"type": "string",+
430+
"format": "csv" +
431+
} +
432+
}
433+
(1 row)
434+
435+
-- Returning SET OF non-composite types
436+
-- defines an application/json response
437+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.returns_table'->'content'->'application/json');
438+
jsonb_pretty
439+
--------------------------------------------------------------
440+
{ +
441+
"schema": { +
442+
"type": "array", +
443+
"items": { +
444+
"$ref": "#/components/schemas/rpc.returns_table"+
445+
} +
446+
} +
447+
}
448+
(1 row)
449+
450+
-- defines an application/vnd.pgrst.object+json response
451+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.returns_table'->'content'->'application/vnd.pgrst.object+json');
452+
jsonb_pretty
453+
----------------------------------------------------------
454+
{ +
455+
"schema": { +
456+
"$ref": "#/components/schemas/rpc.returns_table"+
457+
} +
458+
}
459+
(1 row)
460+
461+
-- defines an application/vnd.pgrst.object+json;nulls=stripped response
462+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.returns_table'->'content'->'application/vnd.pgrst.object+json;nulls=stripped');
463+
jsonb_pretty
464+
----------------------------------------------------------
465+
{ +
466+
"schema": { +
467+
"$ref": "#/components/schemas/rpc.returns_table"+
468+
} +
469+
}
470+
(1 row)
471+
472+
-- defines a text/csv response
473+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.returns_table'->'content'->'text/csv');
474+
jsonb_pretty
475+
---------------------------
476+
{ +
477+
"schema": { +
478+
"type": "string",+
479+
"format": "csv" +
480+
} +
481+
}
482+
(1 row)
483+

test/sql/responses.sql

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,60 @@ select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'-
6666

6767
-- does not define an empty or non-empty response body for non auto-updatable views
6868
select postgrest_openapi_spec('{test}')->'components'->'responses' ? 'mayBeEmpty.non_auto_updatable' as value;
69+
70+
-- Functions
71+
-- Returning composite types
72+
73+
-- defines an application/json response
74+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.get_attribute'->'content'->'application/json');
75+
76+
-- defines an application/vnd.pgrst.object+json response
77+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.get_attribute'->'content'->'application/vnd.pgrst.object+json');
78+
79+
-- defines an application/vnd.pgrst.object+json;nulls=stripped response
80+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.get_attribute'->'content'->'application/vnd.pgrst.object+json;nulls=stripped');
81+
82+
-- defines a text/csv response
83+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.get_attribute'->'content'->'text/csv');
84+
85+
-- Returning SET OF composite types
86+
87+
-- defines an application/json response
88+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.get_products_by_size'->'content'->'application/json');
89+
90+
-- defines an application/vnd.pgrst.object+json response
91+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.get_products_by_size'->'content'->'application/vnd.pgrst.object+json');
92+
93+
-- defines an application/vnd.pgrst.object+json;nulls=stripped response
94+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.get_products_by_size'->'content'->'application/vnd.pgrst.object+json;nulls=stripped');
95+
96+
-- defines a text/csv response
97+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.get_products_by_size'->'content'->'text/csv');
98+
99+
-- Returning non-composite types
100+
101+
-- defines an application/json response
102+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.returns_inout_out'->'content'->'application/json');
103+
104+
-- defines an application/vnd.pgrst.object+json response
105+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.returns_inout_out'->'content'->'application/vnd.pgrst.object+json');
106+
107+
-- defines an application/vnd.pgrst.object+json;nulls=stripped response
108+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.returns_inout_out'->'content'->'application/vnd.pgrst.object+json;nulls=stripped');
109+
110+
-- defines a text/csv response
111+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.returns_inout_out'->'content'->'text/csv');
112+
113+
-- Returning SET OF non-composite types
114+
115+
-- defines an application/json response
116+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.returns_table'->'content'->'application/json');
117+
118+
-- defines an application/vnd.pgrst.object+json response
119+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.returns_table'->'content'->'application/vnd.pgrst.object+json');
120+
121+
-- defines an application/vnd.pgrst.object+json;nulls=stripped response
122+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.returns_table'->'content'->'application/vnd.pgrst.object+json;nulls=stripped');
123+
124+
-- defines a text/csv response
125+
select jsonb_pretty(postgrest_openapi_spec('{test}')->'components'->'responses'->'rpc.returns_table'->'content'->'text/csv');

0 commit comments

Comments
 (0)