Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/plugins/openapi/src/rest-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ import {
isForeignKeyField,
isIdField,
isRelationshipField,
PluginError,
PluginOptions,
requireOption,
resolvePath,
} from '@zenstackhq/sdk';
import { DataModel, DataModelField, DataModelFieldType, Enum, isDataModel, isEnum } from '@zenstackhq/sdk/ast';
import { DataModel, DataModelField, DataModelFieldType, Enum, isDataModel, isEnum, Model } from '@zenstackhq/sdk/ast';
import type { DMMF } from '@zenstackhq/sdk/prisma';
import fs from 'fs';
import { lowerCaseFirst } from 'lower-case-first';
import type { OpenAPIV3_1 as OAPI } from 'openapi-types';
import path from 'path';
import pluralize from 'pluralize';
import invariant from 'tiny-invariant';
import { P, match } from 'ts-pattern';
import { match, P } from 'ts-pattern';
import YAML from 'yaml';
import { name } from '.';
import { OpenAPIGeneratorBase } from './generator-base';
Expand All @@ -32,6 +34,14 @@ type Policies = ReturnType<typeof analyzePolicies>;
export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {
private warnings: string[] = [];

constructor(protected model: Model, protected options: PluginOptions, protected dmmf: DMMF.Document) {
super(model, options, dmmf);

if (this.options.omitInputDetails !== undefined) {
throw new PluginError(name, '"omitInputDetails" option is not supported for "rest" flavor');
}
}

generate() {
let output = requireOption<string>(this.options, 'output', name);
output = resolvePath(output, this.options);
Expand Down
118 changes: 73 additions & 45 deletions packages/plugins/openapi/src/rpc-generator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Inspired by: https://github.com/omar-dulaimi/prisma-trpc-generator

import { PluginError, analyzePolicies, requireOption, resolvePath } from '@zenstackhq/sdk';
import { DataModel, isDataModel } from '@zenstackhq/sdk/ast';
import { PluginError, PluginOptions, analyzePolicies, requireOption, resolvePath } from '@zenstackhq/sdk';
import { DataModel, Model, isDataModel } from '@zenstackhq/sdk/ast';
import {
AggregateOperationSupport,
addMissingInputObjectTypesForAggregate,
Expand All @@ -23,6 +23,8 @@ import { name } from '.';
import { OpenAPIGeneratorBase } from './generator-base';
import { getModelResourceMeta } from './meta';

const ANY_OBJECT = '_AnyObject';

/**
* Generates OpenAPI specification.
*/
Expand All @@ -32,6 +34,16 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
private usedComponents: Set<string> = new Set<string>();
private aggregateOperationSupport: AggregateOperationSupport;
private warnings: string[] = [];
private omitInputDetails: boolean;

constructor(protected model: Model, protected options: PluginOptions, protected dmmf: DMMF.Document) {
super(model, options, dmmf);

this.omitInputDetails = this.getOption<boolean>('omitInputDetails', false);
if (this.omitInputDetails !== undefined && typeof this.omitInputDetails !== 'boolean') {
throw new PluginError(name, `Invalid option value for "omitInputDetails", boolean expected`);
}
}

generate() {
let output = requireOption<string>(this.options, 'output', name);
Expand Down Expand Up @@ -151,9 +163,9 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
type: 'object',
required: ['data'],
properties: {
select: this.ref(`${modelName}Select`),
include: hasRelation ? this.ref(`${modelName}Include`) : undefined,
data: this.ref(`${modelName}CreateInput`),
select: this.omittableRef(`${modelName}Select`),
include: hasRelation ? this.omittableRef(`${modelName}Include`) : undefined,
data: this.omittableRef(`${modelName}CreateInput`),
meta: this.ref('_Meta'),
},
},
Expand All @@ -177,8 +189,8 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
required: ['data'],
properties: {
data: this.oneOf(
this.ref(`${modelName}CreateManyInput`),
this.array(this.ref(`${modelName}CreateManyInput`))
this.omittableRef(`${modelName}CreateManyInput`),
this.array(this.omittableRef(`${modelName}CreateManyInput`))
),
skipDuplicates: {
type: 'boolean',
Expand Down Expand Up @@ -207,9 +219,9 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
type: 'object',
required: ['where'],
properties: {
select: this.ref(`${modelName}Select`),
include: hasRelation ? this.ref(`${modelName}Include`) : undefined,
where: this.ref(`${modelName}WhereUniqueInput`),
select: this.omittableRef(`${modelName}Select`),
include: hasRelation ? this.omittableRef(`${modelName}Include`) : undefined,
where: this.omittableRef(`${modelName}WhereUniqueInput`),
meta: this.ref('_Meta'),
},
},
Expand All @@ -230,9 +242,9 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
{
type: 'object',
properties: {
select: this.ref(`${modelName}Select`),
include: hasRelation ? this.ref(`${modelName}Include`) : undefined,
where: this.ref(`${modelName}WhereInput`),
select: this.omittableRef(`${modelName}Select`),
include: hasRelation ? this.omittableRef(`${modelName}Include`) : undefined,
where: this.omittableRef(`${modelName}WhereInput`),
meta: this.ref('_Meta'),
},
},
Expand All @@ -253,9 +265,9 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
{
type: 'object',
properties: {
select: this.ref(`${modelName}Select`),
include: hasRelation ? this.ref(`${modelName}Include`) : undefined,
where: this.ref(`${modelName}WhereInput`),
select: this.omittableRef(`${modelName}Select`),
include: hasRelation ? this.omittableRef(`${modelName}Include`) : undefined,
where: this.omittableRef(`${modelName}WhereInput`),
meta: this.ref('_Meta'),
},
},
Expand All @@ -277,10 +289,10 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
type: 'object',
required: ['where', 'data'],
properties: {
select: this.ref(`${modelName}Select`),
include: hasRelation ? this.ref(`${modelName}Include`) : undefined,
where: this.ref(`${modelName}WhereUniqueInput`),
data: this.ref(`${modelName}UpdateInput`),
select: this.omittableRef(`${modelName}Select`),
include: hasRelation ? this.omittableRef(`${modelName}Include`) : undefined,
where: this.omittableRef(`${modelName}WhereUniqueInput`),
data: this.omittableRef(`${modelName}UpdateInput`),
meta: this.ref('_Meta'),
},
},
Expand All @@ -302,8 +314,8 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
type: 'object',
required: ['data'],
properties: {
where: this.ref(`${modelName}WhereInput`),
data: this.ref(`${modelName}UpdateManyMutationInput`),
where: this.omittableRef(`${modelName}WhereInput`),
data: this.omittableRef(`${modelName}UpdateManyMutationInput`),
meta: this.ref('_Meta'),
},
},
Expand All @@ -325,11 +337,11 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
type: 'object',
required: ['create', 'update', 'where'],
properties: {
select: this.ref(`${modelName}Select`),
include: hasRelation ? this.ref(`${modelName}Include`) : undefined,
where: this.ref(`${modelName}WhereUniqueInput`),
create: this.ref(`${modelName}CreateInput`),
update: this.ref(`${modelName}UpdateInput`),
select: this.omittableRef(`${modelName}Select`),
include: hasRelation ? this.omittableRef(`${modelName}Include`) : undefined,
where: this.omittableRef(`${modelName}WhereUniqueInput`),
create: this.omittableRef(`${modelName}CreateInput`),
update: this.omittableRef(`${modelName}UpdateInput`),
meta: this.ref('_Meta'),
},
},
Expand All @@ -351,9 +363,9 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
type: 'object',
required: ['where'],
properties: {
select: this.ref(`${modelName}Select`),
include: hasRelation ? this.ref(`${modelName}Include`) : undefined,
where: this.ref(`${modelName}WhereUniqueInput`),
select: this.omittableRef(`${modelName}Select`),
include: hasRelation ? this.omittableRef(`${modelName}Include`) : undefined,
where: this.omittableRef(`${modelName}WhereUniqueInput`),
meta: this.ref('_Meta'),
},
},
Expand All @@ -374,7 +386,7 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
{
type: 'object',
properties: {
where: this.ref(`${modelName}WhereInput`),
where: this.omittableRef(`${modelName}WhereInput`),
meta: this.ref('_Meta'),
},
},
Expand All @@ -395,8 +407,8 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
{
type: 'object',
properties: {
select: this.ref(`${modelName}Select`),
where: this.ref(`${modelName}WhereInput`),
select: this.omittableRef(`${modelName}Select`),
where: this.omittableRef(`${modelName}WhereInput`),
meta: this.ref('_Meta'),
},
},
Expand Down Expand Up @@ -425,9 +437,9 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
{
type: 'object',
properties: {
where: this.ref(`${modelName}WhereInput`),
orderBy: this.ref(orderByWithRelationInput),
cursor: this.ref(`${modelName}WhereUniqueInput`),
where: this.omittableRef(`${modelName}WhereInput`),
orderBy: this.omittableRef(orderByWithRelationInput),
cursor: this.omittableRef(`${modelName}WhereUniqueInput`),
take: { type: 'integer' },
skip: { type: 'integer' },
...this.aggregateFields(model),
Expand All @@ -451,10 +463,10 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
{
type: 'object',
properties: {
where: this.ref(`${modelName}WhereInput`),
orderBy: this.ref(orderByWithRelationInput),
by: this.ref(`${modelName}ScalarFieldEnum`),
having: this.ref(`${modelName}ScalarWhereWithAggregatesInput`),
where: this.omittableRef(`${modelName}WhereInput`),
orderBy: this.omittableRef(orderByWithRelationInput),
by: this.omittableRef(`${modelName}ScalarFieldEnum`),
having: this.omittableRef(`${modelName}ScalarWhereWithAggregatesInput`),
take: { type: 'integer' },
skip: { type: 'integer' },
...this.aggregateFields(model),
Expand Down Expand Up @@ -587,19 +599,19 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
const modelName = upperCaseFirst(model.name);
if (supportedOps) {
if (supportedOps.count) {
result._count = this.oneOf({ type: 'boolean' }, this.ref(`${modelName}CountAggregateInput`));
result._count = this.oneOf({ type: 'boolean' }, this.omittableRef(`${modelName}CountAggregateInput`));
}
if (supportedOps.min) {
result._min = this.ref(`${modelName}MinAggregateInput`);
result._min = this.omittableRef(`${modelName}MinAggregateInput`);
}
if (supportedOps.max) {
result._max = this.ref(`${modelName}MaxAggregateInput`);
result._max = this.omittableRef(`${modelName}MaxAggregateInput`);
}
if (supportedOps.sum) {
result._sum = this.ref(`${modelName}SumAggregateInput`);
result._sum = this.omittableRef(`${modelName}SumAggregateInput`);
}
if (supportedOps.avg) {
result._avg = this.ref(`${modelName}AvgAggregateInput`);
result._avg = this.omittableRef(`${modelName}AvgAggregateInput`);
}
}
return result;
Expand All @@ -617,6 +629,14 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
schemas,
};

if (this.omitInputDetails) {
// generate a catch-all object type
schemas[ANY_OBJECT] = {
type: 'object',
additionalProperties: true,
};
}

// user-defined and built-in enums
for (const _enum of [...(this.dmmf.schema.enumTypes.model ?? []), ...this.dmmf.schema.enumTypes.prisma]) {
schemas[upperCaseFirst(_enum.name)] = this.generateEnumComponent(_enum);
Expand Down Expand Up @@ -824,6 +844,14 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
return { $ref: `#/components/schemas/${upperCaseFirst(type)}`, description };
}

private omittableRef(type: string, rooted = true, description?: string): OAPI.ReferenceObject {
if (this.omitInputDetails) {
return this.ref(ANY_OBJECT);
} else {
return this.ref(type, rooted, description);
}
}

private response(schema: OAPI.SchemaObject): OAPI.SchemaObject {
return {
type: 'object',
Expand Down
Loading
Loading