-
Notifications
You must be signed in to change notification settings - Fork 4
/
components.sql
945 lines (923 loc) · 28.6 KB
/
components.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
-- Functions to build the Components Object of the OAS document
create or replace function oas_build_components(schemas text[])
returns jsonb language sql stable as
$$
select oas_components_object(
schemas := oas_build_component_schemas(schemas),
parameters := oas_build_component_parameters(schemas),
requestBodies := oas_build_request_bodies(schemas),
responses := oas_build_response_objects(schemas),
securitySchemes := oas_build_component_security_schemes()
)
$$;
-- Schemas
create or replace function oas_build_component_schemas(schemas text[])
returns jsonb language sql stable as
$$
select oas_build_component_schemas_from_tables_and_composite_types(schemas) ||
oas_build_component_schemas_from_functions_return_types(schemas) ||
oas_build_component_schemas_headers()
$$;
create or replace function oas_build_component_schemas_from_tables_and_composite_types(schemas text[])
returns jsonb language sql stable as
$$
with recursive all_rels as (
select *
from postgrest_get_all_tables_and_composite_types()
),
all_funcs as (
select *
from postgrest_get_all_functions(schemas)
),
recursive_rels_in_schema as (
select *
from all_rels
where
-- All the tables or views in the exposed schemas
(table_schema = any(schemas) and (is_table or is_view))
-- All the composite types or tables that are present in function arguments
or exists (
select 1
from all_funcs
where return_type_composite_relid = table_oid
or argument_composite_relid = table_oid
)
union
-- Tables may have columns with composite or table types, so we recursively
-- look for these composite types or tables outside of the exposed schemas
-- in order to build the types correctly for the OpenAPI output.
select e.*
from all_rels e, recursive_rels_in_schema r
where e.table_oid = r.column_composite_relid
),
all_tables_and_composite_types as (
select
table_schema,
table_full_name,
table_description,
is_composite,
array_agg(column_name order by column_position) filter (where not column_is_nullable) AS required_cols,
jsonb_object_agg(
column_name,
case when column_item_data_type is null and column_is_composite then
oas_build_reference_to_schemas(column_composite_full_name)
else
oas_schema_object(
description := column_description,
type := postgrest_pgtype_to_oastype(column_data_type),
format := column_data_type::text,
maxlength := column_character_maximum_length,
-- "default" := to_jsonb(info.column_default),
enum := to_jsonb(column_enums),
items :=
case
when column_item_data_type is null then
null
when column_is_composite then
oas_build_reference_to_schemas(column_composite_full_name)
else
oas_schema_object(
type := postgrest_pgtype_to_oastype(column_item_data_type),
format := column_item_data_type::text
)
end
)
end order by column_position
) as columns
from recursive_rels_in_schema
group by table_schema, table_full_name, table_description, is_composite
)
select jsonb_object_agg(x.component_name, x.oas_schema)
from (
select table_full_name as component_name,
oas_schema_object(
description := table_description,
properties := coalesce(columns, '{}'),
required := required_cols,
type := 'object'
) as oas_schema
from all_tables_and_composite_types
) x;
$$;
create or replace function oas_build_component_schemas_from_functions_return_types(schemas text[])
returns jsonb language sql stable as
$$
with all_functions_returning_table_out_simple_types as (
-- Build Component Schemas for functions that RETURNS simple types or RETURNS TABLE or with INOUT/OUT arguments
select *, not (return_type_is_out or return_type_is_table) as return_type_is_simple
from postgrest_get_all_functions(schemas)
where return_type_is_out or return_type_is_table or not return_type_is_composite
),
aggregated_function_returns as (
select
function_schema,
function_full_name,
function_description,
return_type_is_simple,
-- Build objects for functions with RETURNS TABLE or with INOUT/OUT arguments
case when not return_type_is_simple then
jsonb_object_agg(
argument_name,
case when argument_item_type_name is null and argument_is_composite then
oas_build_reference_to_schemas(argument_composite_full_name)
else
oas_schema_object(
type := postgrest_pgtype_to_oastype(argument_type_name),
format := argument_type_name::text,
items :=
case
when argument_item_type_name is null then
null
when argument_is_composite then
oas_build_reference_to_schemas(argument_composite_full_name)
else
oas_schema_object(
type := postgrest_pgtype_to_oastype(argument_item_type_name),
format := argument_item_type_name::text
)
end
)
end
order by argument_position
) filter ( where argument_is_table or argument_is_out or argument_is_inout)
-- For functions with RETURNS <simple type> build the Schema according to the type
else
oas_schema_object(
type := postgrest_pgtype_to_oastype(return_type_name),
format := return_type_name::text,
items :=
case when return_type_item_name is not null then
oas_schema_object(
type := postgrest_pgtype_to_oastype(return_type_item_name),
format := return_type_item_name::text
)
end
)
end as arguments
from all_functions_returning_table_out_simple_types
group by function_schema, function_name, function_full_name, function_description, return_type_is_simple, return_type_name, return_type_item_name
)
select jsonb_object_agg(x.component_name, x.oas_schema)
from (
select
'rpc.' || function_full_name as component_name,
case when not return_type_is_simple then
oas_schema_object(
description := function_description,
properties := coalesce(arguments, '{}'),
type := 'object'
)
else
arguments
end as oas_schema
from
aggregated_function_returns
) x;
$$;
create or replace function oas_build_component_schemas_headers()
returns jsonb language sql stable as
$$
select jsonb_build_object(
'header.preferParams',
oas_schema_object(
type := 'object',
properties := jsonb_build_object(
'params',
oas_schema_object(
description := 'Send JSON as a single parameter',
type := 'string',
"enum" := '["single-object"]',
deprecated := true
)
)
),
'header.preferReturn',
oas_schema_object(
type:= 'object',
properties := jsonb_build_object(
'return',
oas_schema_object(
description := 'Return information of the affected resource',
type := 'string',
"enum" := '["minimal", "headers-only", "representation"]',
"default" := 'minimal'
)
)
),
'header.preferCount',
oas_schema_object(
type:= 'object',
properties := jsonb_build_object(
'count',
oas_schema_object(
description := 'Get the total size of the table',
type := 'string',
"enum" := '["exact", "planned", "estimated"]'
)
)
),
'header.preferResolution',
oas_schema_object(
type:= 'object',
properties := jsonb_build_object(
'resolution',
oas_schema_object(
description := 'Handle duplicates in an upsert',
type := 'string',
"enum" := '["merge-duplicates", "ignore-duplicates"]'
)
)
),
'header.preferTx',
oas_schema_object(
type:= 'object',
properties := jsonb_build_object(
'tx',
oas_schema_object(
description := 'Specify how to end a transaction',
type := 'string',
"enum" := '["commit", "rollback"]'
)
)
),
'header.preferMissing',
oas_schema_object(
type:= 'object',
properties := jsonb_build_object(
'missing',
oas_schema_object(
description := 'Handle null values in bulk inserts',
type := 'string',
"enum" := '["default", "null"]',
"default" := 'null'
)
)
),
'header.preferHandling',
oas_schema_object(
type:= 'object',
properties := jsonb_build_object(
'handling',
oas_schema_object(
description := 'How to handle invalid preferences',
type := 'string',
"enum" := '["strict", "lenient"]',
"default" := 'lenient'
)
)
),
'header.preferTimezone',
oas_schema_object(
type:= 'object',
properties := jsonb_build_object(
'timezone',
oas_schema_object(
description := 'Specify the time zone',
type := 'string'
)
)
),
'header.preferMaxAffected',
oas_schema_object(
type:= 'object',
properties := jsonb_build_object(
'max-affected',
oas_schema_object(
description := 'Specify the amount of resources affected',
type := 'integer'
)
)
)
)
$$;
-- Parameters
create or replace function oas_build_component_parameters(schemas text[])
returns jsonb language sql stable as
$$
select oas_build_component_parameters_query_params_from_tables(schemas) ||
oas_build_component_parameters_query_params_from_function_args(schemas) ||
oas_build_component_parameters_query_params_common() ||
oas_build_component_parameters_headers_common();
$$;
create or replace function oas_build_component_parameters_query_params_from_tables(schemas text[])
returns jsonb language sql stable as
$$
select jsonb_object_agg(x.param_name, x.param_schema)
from (
select format('rowFilter.%1$s.%2$s', table_full_name, column_name) as param_name,
oas_parameter_object(
name := column_name,
"in" := 'query',
schema := oas_schema_object(
type := 'string'
)
) as param_schema
from (
select table_full_name, column_name
from postgrest_get_all_tables_and_composite_types()
where table_schema = any(schemas)
and (is_table or is_view)
) _
) x;
$$;
create or replace function oas_build_component_parameters_query_params_from_function_args(schemas text[])
returns jsonb language sql stable as
$$
select jsonb_object_agg(x.param_name, x.param_schema)
from (
select format('rpcParam.%1$s.%2$s', function_full_name, argument_name) as param_name,
oas_parameter_object(
name := argument_name,
"in" := 'query',
required := argument_is_required,
schema :=
case when argument_is_variadic then
oas_schema_object(
type := 'array',
items := oas_schema_object(
type := argument_oastype,
format := argument_type_name::text
)
)
else
oas_schema_object(
type := argument_oastype,
format := argument_type_name::text
)
end
) as param_schema
from (
select function_full_name, argument_name, argument_is_required, argument_type_name, argument_is_variadic,
-- In query parameters, "array" and "object" types have a different format, so we use a generic "string"
case when postgrest_pgtype_to_oastype(argument_type_name) = any ('{array,object}') then 'string' else postgrest_pgtype_to_oastype(argument_type_name) end as argument_oastype
from postgrest_get_all_functions(schemas)
where argument_input_qty > 0
and argument_name <> ''
and (argument_is_in or argument_is_inout or argument_is_variadic)
) _
) x;
$$;
create or replace function oas_build_component_parameters_query_params_common()
returns jsonb language sql stable as
$$
select jsonb_object_agg(name, param_object) from unnest(
array['select','order', 'limit', 'offset', 'on_conflict', 'columns', 'or', 'and', 'not.or', 'not.and'],
array[
oas_parameter_object(
name := 'select',
"in" := 'query',
description := 'Vertical filtering of columns',
explode := false,
schema := oas_schema_object(
type := 'array',
items := oas_schema_object(type := 'string')
)
),
oas_parameter_object(
name := 'order',
"in" := 'query',
description := 'Ordering by column',
explode := false,
schema := oas_schema_object(
type := 'array',
items := oas_schema_object(type := 'string')
)
),
oas_parameter_object(
name := 'limit',
"in" := 'query',
description := 'Limit the number of rows returned',
explode := false,
schema := oas_schema_object(
type := 'integer'
)
),
oas_parameter_object(
name := 'offset',
"in" := 'query',
description := 'Skip a certain number of rows',
explode := false,
schema := oas_schema_object(
type := 'integer'
)
),
oas_parameter_object(
name := 'on_conflict',
"in" := 'query',
description := 'Columns that resolve the upsert conflict',
explode := false,
schema := oas_schema_object(
type := 'string'
)
),
oas_parameter_object(
name := 'columns',
"in" := 'query',
description := 'Specify which keys from the payload will be inserted',
explode := false,
schema := oas_schema_object(
type := 'string'
)
),
oas_parameter_object(
name := 'or',
"in" := 'query',
description := 'Logical operator to combine filters using OR',
explode := false,
schema := oas_schema_object(
type := 'string'
)
),
oas_parameter_object(
name := 'and',
"in" := 'query',
description := 'Logical operator to combine filters using AND (the default for query params)',
explode := false,
schema := oas_schema_object(
type := 'string'
)
),
oas_parameter_object(
name := 'not.or',
"in" := 'query',
description := 'Negate the logical operator to combine filters using OR',
explode := false,
schema := oas_schema_object(
type := 'string'
)
),
oas_parameter_object(
name := 'not.and',
"in" := 'query',
description := 'Negate the logical operator to combine filters using AND',
explode := false,
schema := oas_schema_object(
type := 'string'
)
)
]
) as _(name, param_object);
$$;
create or replace function oas_build_component_parameters_headers_common ()
returns jsonb language sql stable as
$$
select jsonb_build_object(
'preferGet',
oas_parameter_object(
name := 'Prefer',
"in" := 'header',
explode := true,
description := 'Specify a required or optional behavior for the request',
"schema" := oas_schema_object(
allOf := jsonb_build_array(
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(
'nothing',
oas_example_object(
summary := 'No preferences'
),
'all',
oas_example_object(
summary := 'All default preferences',
value := '{
"handling":"lenient",
"timezone": "",
"count": ""
}'
)
)
),
'preferPost',
oas_parameter_object(
name := 'Prefer',
"in" := 'header',
explode := true,
description := 'Specify a required or optional behavior for the request',
"schema" := oas_schema_object(
allOf := jsonb_build_array(
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(
'nothing',
oas_example_object(
summary := 'No preferences'
),
'all',
oas_example_object(
summary := 'All default preferences',
value := '{
"handling": "lenient",
"timezone": "",
"return": "minimal",
"count": "",
"resolution": "",
"missing": "null",
"tx": "commit"
}'
)
)
),
'preferPatch',
oas_parameter_object(
name := 'Prefer',
"in" := 'header',
explode := true,
description := 'Specify a required or optional behavior for the request',
"schema" := oas_schema_object(
allOf := jsonb_build_array(
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(
'nothing',
oas_example_object(
summary := 'No preferences'
),
'all',
oas_example_object(
summary := 'All default preferences',
value := '{
"handling": "lenient",
"timezone": "",
"return": "minimal",
"count": "",
"max-affected": "",
"tx": "commit"
}'
)
)
),
'preferPut',
oas_parameter_object(
name := 'Prefer',
"in" := 'header',
explode := true,
description := 'Specify a required or optional behavior for the request',
"schema" := oas_schema_object(
allOf := jsonb_build_array(
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(
'nothing',
oas_example_object(
summary := 'No preferences'
),
'all',
oas_example_object(
summary := 'All default preferences',
value := '{
"handling": "lenient",
"timezone": "",
"return": "minimal",
"count": "",
"tx": "commit"
}'
)
)
),
'preferDelete',
oas_parameter_object(
name := 'Prefer',
"in" := 'header',
explode := true,
description := 'Specify a required or optional behavior for the request',
"schema" := oas_schema_object(
allOf := jsonb_build_array(
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(
'nothing',
oas_example_object(
summary := 'No preferences'
),
'all',
oas_example_object(
summary := 'All default preferences',
value := '{
"handling": "lenient",
"timezone": "",
"return": "minimal",
"count": "",
"max-affected": "",
"tx": "commit"
}'
)
)
),
'preferPostRpc',
oas_parameter_object(
name := 'Prefer',
"in" := 'header',
explode := true,
description := 'Specify a required or optional behavior for the request',
"schema" := oas_schema_object(
allOf := jsonb_build_array(
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(
'nothing',
oas_example_object(
summary := 'No preferences'
),
'all',
oas_example_object(
summary := 'All default preferences',
value := '{
"handling": "lenient",
"timezone": "",
"count": "",
"tx": "commit",
"params": ""
}'
)
)
),
'range',
oas_parameter_object(
name := 'Range',
"in" := 'header',
description := 'For limits and pagination',
example := '"0-4"',
"schema" := oas_schema_object(
type := 'string'
)
)
);
$$;
-- Responses
create or replace function oas_build_response_objects(schemas text[])
returns jsonb language sql stable as
$$
select oas_build_response_objects_from_tables(schemas) ||
oas_build_response_objects_from_function_return_types(schemas) ||
oas_build_response_objects_common();
$$;
create or replace function oas_build_response_objects_from_tables(schemas text[])
returns jsonb language sql stable as
$$
select jsonb_object_agg(x.not_empty, x.not_empty_response) ||
jsonb_object_agg(x.may_be_empty, x.may_be_empty_response)
from (
select 'notEmpty.' || table_full_name as not_empty,
oas_response_object(
description := 'Media types when response body is not empty for ' || table_full_name,
content := jsonb_build_object(
'application/json',
oas_media_type_object(
schema := oas_schema_object(
type := 'array',
items := oas_build_reference_to_schemas(table_full_name)
)
),
'application/vnd.pgrst.object+json',
oas_media_type_object(
schema := oas_build_reference_to_schemas(table_full_name)
),
'application/vnd.pgrst.object+json;nulls=stripped',
oas_media_type_object(
schema := oas_build_reference_to_schemas(table_full_name)
),
'text/csv',
oas_media_type_object(
schema := oas_schema_object(
type := 'string',
format := 'csv'
)
)
)
) as not_empty_response,
'mayBeEmpty.' || table_full_name as may_be_empty,
case when insertable then
oas_response_object(
description := 'Media types when response body could be empty or not for ' || table_full_name,
content := jsonb_build_object(
'application/json',
oas_media_type_object(
schema := oas_schema_object(
oneof := jsonb_build_array(
oas_schema_object(
type := 'array',
items := oas_build_reference_to_schemas(table_full_name)
),
oas_schema_object(
type := 'string'
)
)
)
),
'application/vnd.pgrst.object+json',
oas_media_type_object(
schema := oas_schema_object(
oneOf := jsonb_build_array(
oas_build_reference_to_schemas(table_full_name),
oas_schema_object(
type := 'string'
)
)
)
),
'application/vnd.pgrst.object+json;nulls=stripped',
oas_media_type_object(
schema := oas_schema_object(
oneOf := jsonb_build_array(
oas_build_reference_to_schemas(table_full_name),
oas_schema_object(
type := 'string'
)
)
)
),
'text/csv',
oas_media_type_object(
schema := oas_schema_object(
type := 'string',
format := 'csv'
)
)
)
)
end as may_be_empty_response
from postgrest_get_all_tables_and_composite_types()
where table_schema = any(schemas)
and (is_table or is_view)
group by table_schema, table_full_name, insertable, is_table, is_view
) as x
$$;
create or replace function oas_build_response_objects_from_function_return_types(schemas text[])
returns jsonb language sql stable as
$$
select jsonb_object_agg(x.not_empty, x.not_empty_response)
from (
select 'rpc.' || function_full_name as not_empty,
oas_response_object(
description := 'Media types for RPC ' || function_full_name,
content := jsonb_build_object(
'application/json',
case when return_type_is_set then
oas_media_type_object(
schema := oas_schema_object(
type := 'array',
items := return_type_reference_schema
)
)
else
oas_media_type_object(
schema := return_type_reference_schema
)
end,
'application/vnd.pgrst.object+json',
oas_media_type_object(
schema := return_type_reference_schema
),
'application/vnd.pgrst.object+json;nulls=stripped',
oas_media_type_object(
schema := return_type_reference_schema
),
'text/csv',
oas_media_type_object(
schema := oas_schema_object(
type := 'string',
format := 'csv'
)
)
)
) as not_empty_response
from (
select *,
-- Build the reference either to the table/composite return type or the non-composite return type
case when return_type_is_composite then
oas_build_reference_to_schemas(return_type_composite_full_name)
else
oas_build_reference_to_schemas('rpc.' || function_full_name)
end as return_type_reference_schema
from (
select function_full_name, return_type_is_set, return_type_is_composite, return_type_composite_full_name
from postgrest_get_all_functions(schemas)
group by function_full_name, return_type_is_set, return_type_is_composite, return_type_composite_full_name
) _
) _
) as x
$$;
create or replace function oas_build_response_objects_common()
returns jsonb language sql stable as
$$
select jsonb_build_object(
'defaultError',
oas_response_object(
description := 'Default error reponse',
content := jsonb_build_object(
'application/json',
oas_media_type_object(
schema := oas_schema_object(
type := 'object',
properties := jsonb_build_object(
'code', oas_schema_object(type := 'string'),
'details', oas_schema_object(type := 'string'),
'hint', oas_schema_object(type := 'string'),
'message', oas_schema_object(type := 'string')
)
)
)
)
),
'empty',
oas_response_object(
description := 'No media types when response body is empty'
-- Does not specify a "content": https://swagger.io/docs/specification/describing-responses/#empty
)
);
$$;
-- Request bodies
create or replace function oas_build_request_bodies(schemas text[])
returns jsonb language sql stable as
$$
select oas_build_request_bodies_from_tables(schemas);
$$;
create or replace function oas_build_request_bodies_from_tables(schemas text[])
returns jsonb language sql stable as
$$
select jsonb_object_agg(x.table_full_name, x.oas_req_body)
from (
select
table_full_name,
oas_request_body_object(
description := table_full_name,
content := jsonb_build_object(
'application/json',
oas_media_type_object(
"schema" := oas_schema_object(
oneOf := jsonb_build_array(
oas_build_reference_to_schemas(table_full_name),
oas_schema_object(
type := 'array',
items := oas_build_reference_to_schemas(table_full_name)
)
)
)
),
'application/x-www-form-urlencoded',
oas_media_type_object(
"schema" := oas_build_reference_to_schemas(table_full_name)
),
'text/csv',
oas_media_type_object(
"schema" := oas_schema_object(
type := 'string',
format := 'csv'
)
)
)
) as oas_req_body
from postgrest_get_all_tables_and_composite_types()
where table_schema = any(schemas)
and (is_table or is_view)
and insertable
group by table_schema, table_full_name, insertable
) as x;
$$;
-- Security Schemes
create or replace function oas_build_component_security_schemes ()
returns jsonb language sql stable as
$$
select jsonb_build_object(
'JWT',
oas_security_scheme_object(
type := 'http',
description := 'Adds the JSON Web Token to the `Authorization: Bearer <JWT>` header.',
scheme := 'bearer',
bearerFormat := 'JWT'
)
);
$$;