-
-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathgenerator-base.ts
More file actions
158 lines (139 loc) · 6.05 KB
/
generator-base.ts
File metadata and controls
158 lines (139 loc) · 6.05 KB
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
import { getZodErrorMessage } from '@zenstackhq/runtime/local-helpers';
import { PluginError, getDataModels, hasAttribute, type PluginOptions, type PluginResult } from '@zenstackhq/sdk';
import { Model } from '@zenstackhq/sdk/ast';
import type { DMMF } from '@zenstackhq/sdk/prisma';
import type { OpenAPIV3_1 as OAPI } from 'openapi-types';
import semver from 'semver';
import { name } from '.';
import { SecuritySchemesSchema } from './schema';
export abstract class OpenAPIGeneratorBase {
protected readonly DEFAULT_SPEC_VERSION = '3.1.0';
constructor(protected model: Model, protected options: PluginOptions, protected dmmf: DMMF.Document) {}
abstract generate(): PluginResult;
protected get includedModels() {
const includeOpenApiIgnored = this.getOption<boolean>('includeOpenApiIgnored', false);
const models = getDataModels(this.model);
return includeOpenApiIgnored ? models : models.filter((d) => !hasAttribute(d, '@@openapi.ignore'));
}
protected wrapArray(
schema: OAPI.ReferenceObject | OAPI.SchemaObject,
isArray: boolean
): OAPI.ReferenceObject | OAPI.SchemaObject {
if (isArray) {
return { type: 'array', items: schema };
} else {
return schema;
}
}
protected wrapNullable(
schema: OAPI.ReferenceObject | OAPI.SchemaObject,
isNullable: boolean
): OAPI.ReferenceObject | OAPI.SchemaObject {
if (!isNullable) {
return schema;
}
const specVersion = this.getOption('specVersion', this.DEFAULT_SPEC_VERSION);
// https://stackoverflow.com/questions/48111459/how-to-define-a-property-that-can-be-string-or-null-in-openapi-swagger
// https://stackoverflow.com/questions/40920441/how-to-specify-a-property-can-be-null-or-a-reference-with-swagger
if (semver.gte(specVersion, '3.1.0')) {
// OAPI 3.1.0 and above has native 'null' type
if ((schema as OAPI.BaseSchemaObject).oneOf) {
// merge into existing 'oneOf'
return { oneOf: [...(schema as OAPI.BaseSchemaObject).oneOf!, { type: 'null' }] };
} else {
// wrap into a 'oneOf'
return { oneOf: [{ type: 'null' }, schema] };
}
} else {
if ((schema as OAPI.ReferenceObject).$ref) {
// nullable $ref needs to be represented as: { allOf: [{ $ref: ... }], nullable: true }
return {
allOf: [schema],
nullable: true,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any;
} else {
// nullable scalar: { type: ..., nullable: true }
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return { ...schema, nullable: true } as any;
}
}
}
protected array(itemType: OAPI.SchemaObject | OAPI.ReferenceObject) {
return { type: 'array', items: itemType } as const;
}
protected oneOf(...schemas: (OAPI.SchemaObject | OAPI.ReferenceObject)[]) {
return { oneOf: schemas };
}
protected allOf(...schemas: (OAPI.SchemaObject | OAPI.ReferenceObject)[]) {
return { allOf: schemas };
}
protected getOption<T = string>(name: string): T | undefined;
protected getOption<T = string, D extends T = T>(name: string, defaultValue: D): T;
protected getOption<T = string>(name: string, defaultValue?: T): T | undefined {
const value = this.options[name];
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
return value === undefined ? defaultValue : value;
}
protected generateSecuritySchemes() {
const securitySchemes = this.getOption<Record<string, string>[]>('securitySchemes');
if (securitySchemes) {
const parsed = SecuritySchemesSchema.safeParse(securitySchemes);
if (!parsed.success) {
throw new PluginError(name, `"securitySchemes" option is invalid: ${getZodErrorMessage(parsed.error)}`);
}
return parsed.data;
}
return undefined;
}
protected pruneComponents(paths: OAPI.PathsObject, components: OAPI.ComponentsObject) {
const schemas = components.schemas;
if (schemas) {
const roots = new Set<string>();
for (const path of Object.values(paths)) {
this.collectUsedComponents(path, roots);
}
// build a transitive closure for all reachable schemas from roots
const allUsed = new Set<string>(roots);
let todo = [...allUsed];
while (todo.length > 0) {
const curr = new Set<string>(allUsed);
Object.entries(schemas)
.filter(([key]) => todo.includes(key))
.forEach(([, value]) => {
this.collectUsedComponents(value, allUsed);
});
todo = [...allUsed].filter((e) => !curr.has(e));
}
// prune unused schemas
Object.keys(schemas).forEach((key) => {
if (!allUsed.has(key)) {
delete schemas[key];
}
});
}
}
private collectUsedComponents(value: unknown, allUsed: Set<string>) {
if (!value) {
return;
}
if (Array.isArray(value)) {
value.forEach((item) => {
this.collectUsedComponents(item, allUsed);
});
} else if (typeof value === 'object') {
Object.entries(value).forEach(([subKey, subValue]) => {
if (subKey === '$ref') {
const ref = subValue as string;
const name = ref.split('/').pop();
if (name && !allUsed.has(name)) {
allUsed.add(name);
}
} else {
this.collectUsedComponents(subValue, allUsed);
}
});
}
}
}