-
Notifications
You must be signed in to change notification settings - Fork 5
/
openapi.sql
378 lines (363 loc) · 9.42 KB
/
openapi.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
-- Functions and types to build the the OpenAPI output
-- TODO: create enum for openapi type e.g. string, number, etc.
create type oas_parameter_in as enum ('query', 'header', 'path', 'cookie');
create type oas_parameter_style as enum ('simple', 'form');
create type oas_security_scheme_in as enum ('query', 'header', 'cookie');
create type oas_security_scheme_type as enum ('apiKey', 'http', 'mutualTLS', 'oauth2', 'openIdConnect');
create or replace function oas_openapi_object(
openapi text,
info jsonb,
xsoftware jsonb,
paths jsonb,
jsonSchemaDialect text default null,
servers jsonb default null,
webhooks jsonb default null,
components jsonb default null,
security jsonb default null,
tags jsonb default null,
externalDocs jsonb default null
)
returns jsonb language sql stable as
$$
select jsonb_strip_nulls(
jsonb_build_object(
'openapi', openapi,
'info', info,
'x-software', xsoftware,
'paths', paths,
'jsonSchemaDialect', jsonSchemaDialect,
'servers', servers,
'webhooks', webhooks,
'components', components,
'security', security,
'tags', tags,
'externalDocs', externalDocs
)
);
$$;
create or replace function oas_info_object(
title text,
version text,
summary text default null,
description text default null,
termsOfService text default null,
contact jsonb default null,
license jsonb default null
)
returns jsonb language sql stable as
$$
select jsonb_build_object(
'title', title,
'version', version,
'summary', summary,
'description', description,
'termsOfService', termsOfService,
'contact', contact,
'license', license
);
$$;
create or replace function oas_x_software_object(
name text,
version text,
description text
)
returns jsonb language sql stable as
$$
select json_build_object(
'x-name', name,
'x-version', version,
'x-description', description
);
$$;
create or replace function oas_components_object(
schemas jsonb default null,
responses jsonb default null,
parameters jsonb default null,
examples jsonb default null,
requestBodies jsonb default null,
headers jsonb default null,
securitySchemes jsonb default null,
links jsonb default null,
callbacks jsonb default null,
pathItems jsonb default null
)
returns jsonb language sql stable as
$$
select json_build_object(
'schemas', schemas,
'responses', responses,
'parameters', parameters,
'examples', examples,
'requestBodies', requestBodies,
'headers', headers,
'securitySchemes', securitySchemes,
'links', links,
'callbacks', callbacks,
'pathItems', pathItems
);
$$;
create or replace function oas_schema_object(
title text default null,
description text default null,
enum jsonb default null,
"default" text default null,
format text default null,
type text default null,
items jsonb default null,
maxItems integer default null,
minItems integer default null,
uniqueItems boolean default null,
pattern text default null,
maxLength integer default null,
minLength integer default null,
maximum numeric default null,
exclusiveMaximum boolean default null,
minimum numeric default null,
exclusiveMinimum boolean default null,
multipleOf numeric default null,
required text[] default null,
properties jsonb default null,
additionalProperties jsonb default null,
maxProperties integer default null,
minProperties integer default null,
allOf jsonb default null,
oneOf jsonb default null,
anyOf jsonb default null,
"not" jsonb default null,
discriminator jsonb default null,
readOnly boolean default null,
writeOnly boolean default null,
xml jsonb default null,
externalDocs jsonb default null,
example jsonb default null,
deprecated boolean default null
)
returns jsonb language sql stable as
$$
-- TODO: build the JSON object according to the type
select jsonb_build_object(
'title', title,
'description', description,
'enum', enum,
'default', "default",
'format', format,
'type', type,
'items', items,
'maxItems', maxItems,
'minItems', minItems,
'uniqueItems', uniqueItems,
'pattern', pattern,
'maxLength', maxLength,
'minLength', minLength,
'maximum', maximum,
'exclusiveMaximum', exclusiveMaximum,
'minimum', minimum,
'exclusiveMinimum', exclusiveMinimum,
'multipleOf', multipleOf,
'required' , required,
'properties', properties,
'additionalProperties', additionalProperties,
'maxProperties', maxProperties,
'minProperties', minProperties,
'allOf', allOf,
'oneOf', oneOf,
'anyOf', anyOf,
'not', "not",
'discriminator', discriminator,
'readOnly', readOnly,
'writeOnly', writeOnly,
'xml', xml,
'externalDocs', externalDocs,
'example', example,
'deprecated', deprecated
)
$$;
create or replace function oas_reference_object(
ref text,
summary text default null,
description text default null
)
returns jsonb language sql stable as
$$
select json_build_object(
'$ref', ref,
'summary', summary,
'description', description
);
$$;
create or replace function oas_parameter_object(
name text,
"in" oas_parameter_in,
description text default null,
required boolean default null,
deprecated boolean default null,
allowEmptyValue boolean default null,
style oas_parameter_style default null,
explode boolean default null,
"schema" jsonb default null,
example jsonb default null,
examples jsonb default null
)
returns jsonb language sql stable as
$$
-- TODO: Add missing logic between fields (e.g. example and examples are mutually exclusive)
select jsonb_build_object(
'name', name,
'in', "in",
'description', description,
'required', required,
'deprecated', deprecated,
'allowEmptyValue', allowEmptyValue,
'style', style,
'explode', explode,
'schema', "schema",
'example', example,
'examples', examples
)
$$;
create or replace function oas_security_scheme_object(
type oas_security_scheme_type,
description text default null,
-- TODO: the following should be required depending on the "type"
name text default null,
"in" oas_security_scheme_in default null,
scheme text default null,
bearerFormat text default null,
flows jsonb default null,
openIdConnectUrl text default null
)
returns jsonb language sql stable as
$$
select jsonb_build_object(
'type', type,
'description', description,
'name', name,
'in', "in",
'scheme', scheme,
'bearerFormat', bearerFormat,
'flows', flows,
'openIdConnectUrl', openIdConnectUrl
);
$$;
create or replace function oas_example_object(
summary text default null,
description text default null,
value jsonb default null,
externalValue text default null
)
returns jsonb language sql stable as
$$
select jsonb_build_object(
'summary', summary,
'description', description,
'value', value,
'externalValue', externalValue
);
$$;
create or replace function oas_path_item_object(
ref text default null,
summary text default null,
description text default null,
get jsonb default null,
put jsonb default null,
post jsonb default null,
delete jsonb default null,
options jsonb default null,
head jsonb default null,
patch jsonb default null,
trace jsonb default null,
servers jsonb default null,
parameters jsonb default null
)
returns jsonb language sql stable as
$$
select json_build_object(
'$ref', ref,
'summary', summary,
'description', description,
'get', get,
'put', put,
'post', post,
'delete', delete,
'options', options,
'head', head,
'patch', patch,
'trace', trace,
'servers', servers,
'parameters', parameters
)
$$;
create or replace function oas_operation_object(
tags text[] default null,
summary text default null,
description text default null,
externalDocs jsonb default null,
operationId text default null,
parameters jsonb default null,
requestBody jsonb default null,
responses jsonb default null,
callbacks jsonb default null,
deprecated boolean default null,
security jsonb default null,
servers jsonb default null
)
returns jsonb language sql stable as
$$
select json_build_object(
'tags', tags,
'summary', summary,
'description', description,
'externalDocs', externalDocs,
'operationId', operationId,
'parameters', parameters,
'requestBody', requestBody,
'responses', responses,
'callbacks', callbacks,
'deprecated', deprecated,
'security', security,
'servers', servers
)
$$;
create or replace function oas_response_object(
description text,
headers jsonb default null,
content jsonb default null,
links jsonb default null
)
returns jsonb language sql stable as
$$
select json_build_object(
'description', description,
'headers', headers,
'content', content,
'links', links
)
$$;
create or replace function oas_media_type_object(
"schema" jsonb default null,
example text default null,
examples jsonb default null,
encoding jsonb default null
)
returns jsonb language sql stable as
$$
select json_build_object(
'schema', "schema",
'example', example,
'examples', examples,
'encoding', encoding
)
$$;
create or replace function oas_request_body_object(
content jsonb,
description text default null,
required boolean default null
)
returns jsonb language sql stable as
$$
select json_build_object(
'content', content,
'description', description,
'required', required
)
$$;