1
1
// Inspired by: https://github.com/omar-dulaimi/prisma-trpc-generator
2
2
3
- import { PluginError , analyzePolicies , requireOption , resolvePath } from '@zenstackhq/sdk' ;
4
- import { DataModel , isDataModel } from '@zenstackhq/sdk/ast' ;
3
+ import { PluginError , PluginOptions , analyzePolicies , requireOption , resolvePath } from '@zenstackhq/sdk' ;
4
+ import { DataModel , Model , isDataModel } from '@zenstackhq/sdk/ast' ;
5
5
import {
6
6
AggregateOperationSupport ,
7
7
addMissingInputObjectTypesForAggregate ,
@@ -23,6 +23,8 @@ import { name } from '.';
23
23
import { OpenAPIGeneratorBase } from './generator-base' ;
24
24
import { getModelResourceMeta } from './meta' ;
25
25
26
+ const ANY_OBJECT = '_AnyObject' ;
27
+
26
28
/**
27
29
* Generates OpenAPI specification.
28
30
*/
@@ -32,6 +34,16 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
32
34
private usedComponents : Set < string > = new Set < string > ( ) ;
33
35
private aggregateOperationSupport : AggregateOperationSupport ;
34
36
private warnings : string [ ] = [ ] ;
37
+ private omitInputDetails : boolean ;
38
+
39
+ constructor ( protected model : Model , protected options : PluginOptions , protected dmmf : DMMF . Document ) {
40
+ super ( model , options , dmmf ) ;
41
+
42
+ this . omitInputDetails = this . getOption < boolean > ( 'omitInputDetails' , false ) ;
43
+ if ( this . omitInputDetails !== undefined && typeof this . omitInputDetails !== 'boolean' ) {
44
+ throw new PluginError ( name , `Invalid option value for "omitInputDetails", boolean expected` ) ;
45
+ }
46
+ }
35
47
36
48
generate ( ) {
37
49
let output = requireOption < string > ( this . options , 'output' , name ) ;
@@ -151,9 +163,9 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
151
163
type : 'object' ,
152
164
required : [ 'data' ] ,
153
165
properties : {
154
- select : this . ref ( `${ modelName } Select` ) ,
155
- include : hasRelation ? this . ref ( `${ modelName } Include` ) : undefined ,
156
- data : this . ref ( `${ modelName } CreateInput` ) ,
166
+ select : this . omittableRef ( `${ modelName } Select` ) ,
167
+ include : hasRelation ? this . omittableRef ( `${ modelName } Include` ) : undefined ,
168
+ data : this . omittableRef ( `${ modelName } CreateInput` ) ,
157
169
meta : this . ref ( '_Meta' ) ,
158
170
} ,
159
171
} ,
@@ -177,8 +189,8 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
177
189
required : [ 'data' ] ,
178
190
properties : {
179
191
data : this . oneOf (
180
- this . ref ( `${ modelName } CreateManyInput` ) ,
181
- this . array ( this . ref ( `${ modelName } CreateManyInput` ) )
192
+ this . omittableRef ( `${ modelName } CreateManyInput` ) ,
193
+ this . array ( this . omittableRef ( `${ modelName } CreateManyInput` ) )
182
194
) ,
183
195
skipDuplicates : {
184
196
type : 'boolean' ,
@@ -207,9 +219,9 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
207
219
type : 'object' ,
208
220
required : [ 'where' ] ,
209
221
properties : {
210
- select : this . ref ( `${ modelName } Select` ) ,
211
- include : hasRelation ? this . ref ( `${ modelName } Include` ) : undefined ,
212
- where : this . ref ( `${ modelName } WhereUniqueInput` ) ,
222
+ select : this . omittableRef ( `${ modelName } Select` ) ,
223
+ include : hasRelation ? this . omittableRef ( `${ modelName } Include` ) : undefined ,
224
+ where : this . omittableRef ( `${ modelName } WhereUniqueInput` ) ,
213
225
meta : this . ref ( '_Meta' ) ,
214
226
} ,
215
227
} ,
@@ -230,9 +242,9 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
230
242
{
231
243
type : 'object' ,
232
244
properties : {
233
- select : this . ref ( `${ modelName } Select` ) ,
234
- include : hasRelation ? this . ref ( `${ modelName } Include` ) : undefined ,
235
- where : this . ref ( `${ modelName } WhereInput` ) ,
245
+ select : this . omittableRef ( `${ modelName } Select` ) ,
246
+ include : hasRelation ? this . omittableRef ( `${ modelName } Include` ) : undefined ,
247
+ where : this . omittableRef ( `${ modelName } WhereInput` ) ,
236
248
meta : this . ref ( '_Meta' ) ,
237
249
} ,
238
250
} ,
@@ -253,9 +265,9 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
253
265
{
254
266
type : 'object' ,
255
267
properties : {
256
- select : this . ref ( `${ modelName } Select` ) ,
257
- include : hasRelation ? this . ref ( `${ modelName } Include` ) : undefined ,
258
- where : this . ref ( `${ modelName } WhereInput` ) ,
268
+ select : this . omittableRef ( `${ modelName } Select` ) ,
269
+ include : hasRelation ? this . omittableRef ( `${ modelName } Include` ) : undefined ,
270
+ where : this . omittableRef ( `${ modelName } WhereInput` ) ,
259
271
meta : this . ref ( '_Meta' ) ,
260
272
} ,
261
273
} ,
@@ -277,10 +289,10 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
277
289
type : 'object' ,
278
290
required : [ 'where' , 'data' ] ,
279
291
properties : {
280
- select : this . ref ( `${ modelName } Select` ) ,
281
- include : hasRelation ? this . ref ( `${ modelName } Include` ) : undefined ,
282
- where : this . ref ( `${ modelName } WhereUniqueInput` ) ,
283
- data : this . ref ( `${ modelName } UpdateInput` ) ,
292
+ select : this . omittableRef ( `${ modelName } Select` ) ,
293
+ include : hasRelation ? this . omittableRef ( `${ modelName } Include` ) : undefined ,
294
+ where : this . omittableRef ( `${ modelName } WhereUniqueInput` ) ,
295
+ data : this . omittableRef ( `${ modelName } UpdateInput` ) ,
284
296
meta : this . ref ( '_Meta' ) ,
285
297
} ,
286
298
} ,
@@ -302,8 +314,8 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
302
314
type : 'object' ,
303
315
required : [ 'data' ] ,
304
316
properties : {
305
- where : this . ref ( `${ modelName } WhereInput` ) ,
306
- data : this . ref ( `${ modelName } UpdateManyMutationInput` ) ,
317
+ where : this . omittableRef ( `${ modelName } WhereInput` ) ,
318
+ data : this . omittableRef ( `${ modelName } UpdateManyMutationInput` ) ,
307
319
meta : this . ref ( '_Meta' ) ,
308
320
} ,
309
321
} ,
@@ -325,11 +337,11 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
325
337
type : 'object' ,
326
338
required : [ 'create' , 'update' , 'where' ] ,
327
339
properties : {
328
- select : this . ref ( `${ modelName } Select` ) ,
329
- include : hasRelation ? this . ref ( `${ modelName } Include` ) : undefined ,
330
- where : this . ref ( `${ modelName } WhereUniqueInput` ) ,
331
- create : this . ref ( `${ modelName } CreateInput` ) ,
332
- update : this . ref ( `${ modelName } UpdateInput` ) ,
340
+ select : this . omittableRef ( `${ modelName } Select` ) ,
341
+ include : hasRelation ? this . omittableRef ( `${ modelName } Include` ) : undefined ,
342
+ where : this . omittableRef ( `${ modelName } WhereUniqueInput` ) ,
343
+ create : this . omittableRef ( `${ modelName } CreateInput` ) ,
344
+ update : this . omittableRef ( `${ modelName } UpdateInput` ) ,
333
345
meta : this . ref ( '_Meta' ) ,
334
346
} ,
335
347
} ,
@@ -351,9 +363,9 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
351
363
type : 'object' ,
352
364
required : [ 'where' ] ,
353
365
properties : {
354
- select : this . ref ( `${ modelName } Select` ) ,
355
- include : hasRelation ? this . ref ( `${ modelName } Include` ) : undefined ,
356
- where : this . ref ( `${ modelName } WhereUniqueInput` ) ,
366
+ select : this . omittableRef ( `${ modelName } Select` ) ,
367
+ include : hasRelation ? this . omittableRef ( `${ modelName } Include` ) : undefined ,
368
+ where : this . omittableRef ( `${ modelName } WhereUniqueInput` ) ,
357
369
meta : this . ref ( '_Meta' ) ,
358
370
} ,
359
371
} ,
@@ -374,7 +386,7 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
374
386
{
375
387
type : 'object' ,
376
388
properties : {
377
- where : this . ref ( `${ modelName } WhereInput` ) ,
389
+ where : this . omittableRef ( `${ modelName } WhereInput` ) ,
378
390
meta : this . ref ( '_Meta' ) ,
379
391
} ,
380
392
} ,
@@ -395,8 +407,8 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
395
407
{
396
408
type : 'object' ,
397
409
properties : {
398
- select : this . ref ( `${ modelName } Select` ) ,
399
- where : this . ref ( `${ modelName } WhereInput` ) ,
410
+ select : this . omittableRef ( `${ modelName } Select` ) ,
411
+ where : this . omittableRef ( `${ modelName } WhereInput` ) ,
400
412
meta : this . ref ( '_Meta' ) ,
401
413
} ,
402
414
} ,
@@ -425,9 +437,9 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
425
437
{
426
438
type : 'object' ,
427
439
properties : {
428
- where : this . ref ( `${ modelName } WhereInput` ) ,
429
- orderBy : this . ref ( orderByWithRelationInput ) ,
430
- cursor : this . ref ( `${ modelName } WhereUniqueInput` ) ,
440
+ where : this . omittableRef ( `${ modelName } WhereInput` ) ,
441
+ orderBy : this . omittableRef ( orderByWithRelationInput ) ,
442
+ cursor : this . omittableRef ( `${ modelName } WhereUniqueInput` ) ,
431
443
take : { type : 'integer' } ,
432
444
skip : { type : 'integer' } ,
433
445
...this . aggregateFields ( model ) ,
@@ -451,10 +463,10 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
451
463
{
452
464
type : 'object' ,
453
465
properties : {
454
- where : this . ref ( `${ modelName } WhereInput` ) ,
455
- orderBy : this . ref ( orderByWithRelationInput ) ,
456
- by : this . ref ( `${ modelName } ScalarFieldEnum` ) ,
457
- having : this . ref ( `${ modelName } ScalarWhereWithAggregatesInput` ) ,
466
+ where : this . omittableRef ( `${ modelName } WhereInput` ) ,
467
+ orderBy : this . omittableRef ( orderByWithRelationInput ) ,
468
+ by : this . omittableRef ( `${ modelName } ScalarFieldEnum` ) ,
469
+ having : this . omittableRef ( `${ modelName } ScalarWhereWithAggregatesInput` ) ,
458
470
take : { type : 'integer' } ,
459
471
skip : { type : 'integer' } ,
460
472
...this . aggregateFields ( model ) ,
@@ -587,19 +599,19 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
587
599
const modelName = upperCaseFirst ( model . name ) ;
588
600
if ( supportedOps ) {
589
601
if ( supportedOps . count ) {
590
- result . _count = this . oneOf ( { type : 'boolean' } , this . ref ( `${ modelName } CountAggregateInput` ) ) ;
602
+ result . _count = this . oneOf ( { type : 'boolean' } , this . omittableRef ( `${ modelName } CountAggregateInput` ) ) ;
591
603
}
592
604
if ( supportedOps . min ) {
593
- result . _min = this . ref ( `${ modelName } MinAggregateInput` ) ;
605
+ result . _min = this . omittableRef ( `${ modelName } MinAggregateInput` ) ;
594
606
}
595
607
if ( supportedOps . max ) {
596
- result . _max = this . ref ( `${ modelName } MaxAggregateInput` ) ;
608
+ result . _max = this . omittableRef ( `${ modelName } MaxAggregateInput` ) ;
597
609
}
598
610
if ( supportedOps . sum ) {
599
- result . _sum = this . ref ( `${ modelName } SumAggregateInput` ) ;
611
+ result . _sum = this . omittableRef ( `${ modelName } SumAggregateInput` ) ;
600
612
}
601
613
if ( supportedOps . avg ) {
602
- result . _avg = this . ref ( `${ modelName } AvgAggregateInput` ) ;
614
+ result . _avg = this . omittableRef ( `${ modelName } AvgAggregateInput` ) ;
603
615
}
604
616
}
605
617
return result ;
@@ -617,6 +629,14 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
617
629
schemas,
618
630
} ;
619
631
632
+ if ( this . omitInputDetails ) {
633
+ // generate a catch-all object type
634
+ schemas [ ANY_OBJECT ] = {
635
+ type : 'object' ,
636
+ additionalProperties : true ,
637
+ } ;
638
+ }
639
+
620
640
// user-defined and built-in enums
621
641
for ( const _enum of [ ...( this . dmmf . schema . enumTypes . model ?? [ ] ) , ...this . dmmf . schema . enumTypes . prisma ] ) {
622
642
schemas [ upperCaseFirst ( _enum . name ) ] = this . generateEnumComponent ( _enum ) ;
@@ -824,6 +844,14 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
824
844
return { $ref : `#/components/schemas/${ upperCaseFirst ( type ) } ` , description } ;
825
845
}
826
846
847
+ private omittableRef ( type : string , rooted = true , description ?: string ) : OAPI . ReferenceObject {
848
+ if ( this . omitInputDetails ) {
849
+ return this . ref ( ANY_OBJECT ) ;
850
+ } else {
851
+ return this . ref ( type , rooted , description ) ;
852
+ }
853
+ }
854
+
827
855
private response ( schema : OAPI . SchemaObject ) : OAPI . SchemaObject {
828
856
return {
829
857
type : 'object' ,
0 commit comments