Skip to content

Commit 92fdd9a

Browse files
committed
update openapi plugin
1 parent 7d03fdb commit 92fdd9a

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

packages/plugins/openapi/src/rest-generator.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ type Policies = ReturnType<typeof analyzePolicies>;
4444
*/
4545
export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {
4646
private warnings: string[] = [];
47+
private modelNameMapping: Record<string, string>;
4748

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

5152
if (this.options.omitInputDetails !== undefined) {
5253
throw new PluginError(name, '"omitInputDetails" option is not supported for "rest" flavor');
5354
}
55+
56+
this.modelNameMapping = this.getOption('modelNameMapping', {} as Record<string, string>);
5457
}
5558

5659
generate() {
@@ -126,6 +129,10 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {
126129
return result;
127130
}
128131

132+
private mapModelName(modelName: string): string {
133+
return this.modelNameMapping[modelName] ?? modelName;
134+
}
135+
129136
private generatePathsForModel(model: DMMF.Model, zmodel: DataModel): OAPI.PathItemObject | undefined {
130137
const result: Record<string, OAPI.PathItemObject> = {};
131138

@@ -139,9 +146,11 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {
139146

140147
const resourceMeta = getModelResourceMeta(zmodel);
141148

149+
const modelName = this.mapModelName(model.name);
150+
142151
// GET /resource
143152
// POST /resource
144-
result[`${prefix}/${lowerCaseFirst(model.name)}`] = {
153+
result[`${prefix}/${lowerCaseFirst(modelName)}`] = {
145154
get: this.makeResourceList(zmodel, policies, resourceMeta),
146155
post: this.makeResourceCreate(zmodel, policies, resourceMeta),
147156
};
@@ -150,10 +159,10 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {
150159
// PUT /resource/{id}
151160
// PATCH /resource/{id}
152161
// DELETE /resource/{id}
153-
result[`${prefix}/${lowerCaseFirst(model.name)}/{id}`] = {
162+
result[`${prefix}/${lowerCaseFirst(modelName)}/{id}`] = {
154163
get: this.makeResourceFetch(zmodel, policies, resourceMeta),
155-
put: this.makeResourceUpdate(zmodel, policies, `update-${model.name}-put`, resourceMeta),
156-
patch: this.makeResourceUpdate(zmodel, policies, `update-${model.name}-patch`, resourceMeta),
164+
put: this.makeResourceUpdate(zmodel, policies, `update-${modelName}-put`, resourceMeta),
165+
patch: this.makeResourceUpdate(zmodel, policies, `update-${modelName}-patch`, resourceMeta),
157166
delete: this.makeResourceDelete(zmodel, policies, resourceMeta),
158167
};
159168

@@ -165,14 +174,14 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {
165174
}
166175

167176
// GET /resource/{id}/{relationship}
168-
const relatedDataPath = `${prefix}/${lowerCaseFirst(model.name)}/{id}/${field.name}`;
177+
const relatedDataPath = `${prefix}/${lowerCaseFirst(modelName)}/{id}/${field.name}`;
169178
let container = result[relatedDataPath];
170179
if (!container) {
171180
container = result[relatedDataPath] = {};
172181
}
173182
container.get = this.makeRelatedFetch(zmodel, field, relationDecl, resourceMeta);
174183

175-
const relationshipPath = `${prefix}/${lowerCaseFirst(model.name)}/{id}/relationships/${field.name}`;
184+
const relationshipPath = `${prefix}/${lowerCaseFirst(modelName)}/{id}/relationships/${field.name}`;
176185
container = result[relationshipPath];
177186
if (!container) {
178187
container = result[relationshipPath] = {};

packages/server/src/api/rest/index.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,7 @@ class RequestHandler extends APIHandlerBase {
245245
const segmentCharset = options.urlSegmentCharset ?? 'a-zA-Z0-9-_~ %';
246246

247247
this.modelNameMapping = options.modelNameMapping ?? {};
248-
this.reverseModelNameMapping = Object.fromEntries(
249-
Object.entries(this.modelNameMapping).map(([k, v]) => [v, k])
250-
);
248+
this.reverseModelNameMapping = Object.fromEntries(Object.entries(this.modelNameMapping).map(([k, v]) => [v, k]));
251249
this.urlPatternMap = this.buildUrlPatternMap(segmentCharset);
252250
}
253251

@@ -269,8 +267,8 @@ class RequestHandler extends APIHandlerBase {
269267
};
270268
}
271269

272-
private reverseModelNameMap(type: string): string {
273-
return this.reverseModelNameMapping[type] ?? type;
270+
private mapModelName(modelName: string): string {
271+
return this.modelNameMapping[modelName] ?? modelName;
274272
}
275273

276274
private matchUrlPattern(path: string, routeType: UrlPatterns): Match {
@@ -281,7 +279,7 @@ class RequestHandler extends APIHandlerBase {
281279

282280
const match = pattern.match(path);
283281
if (match) {
284-
match.type = this.modelNameMapping[match.type] ?? match.type;
282+
match.type = this.reverseModelNameMapping[match.type] ?? match.type;
285283
}
286284
return match;
287285
}
@@ -569,7 +567,7 @@ class RequestHandler extends APIHandlerBase {
569567
}
570568

571569
if (entity?.[relationship]) {
572-
const mappedType = this.reverseModelNameMap(type);
570+
const mappedType = this.mapModelName(type);
573571
return {
574572
status: 200,
575573
body: await this.serializeItems(relationInfo.type, entity[relationship], {
@@ -621,7 +619,7 @@ class RequestHandler extends APIHandlerBase {
621619
}
622620

623621
const entity: any = await prisma[type].findUnique(args);
624-
const mappedType = this.reverseModelNameMap(type);
622+
const mappedType = this.mapModelName(type);
625623

626624
if (entity?._count?.[relationship] !== undefined) {
627625
// build up paginator
@@ -720,7 +718,7 @@ class RequestHandler extends APIHandlerBase {
720718
]);
721719
const total = count as number;
722720

723-
const mappedType = this.reverseModelNameMap(type);
721+
const mappedType = this.mapModelName(type);
724722
const url = this.makeNormalizedUrl(`/${mappedType}`, query);
725723
const options: Partial<SerializerOptions> = {
726724
include,
@@ -1050,7 +1048,7 @@ class RequestHandler extends APIHandlerBase {
10501048

10511049
const entity: any = await prisma[type].update(updateArgs);
10521050

1053-
const mappedType = this.reverseModelNameMap(type);
1051+
const mappedType = this.mapModelName(type);
10541052

10551053
const serialized: any = await this.serializeItems(relationInfo.type, entity[relationship], {
10561054
linkers: {
@@ -1201,7 +1199,7 @@ class RequestHandler extends APIHandlerBase {
12011199

12021200
for (const model of Object.keys(modelMeta.models)) {
12031201
const ids = getIdFields(modelMeta, model);
1204-
const mappedModel = this.reverseModelNameMap(model);
1202+
const mappedModel = this.mapModelName(model);
12051203

12061204
if (ids.length < 1) {
12071205
continue;
@@ -1254,7 +1252,7 @@ class RequestHandler extends APIHandlerBase {
12541252
}
12551253
const fieldIds = getIdFields(modelMeta, fieldMeta.type);
12561254
if (fieldIds.length > 0) {
1257-
const mappedModel = this.reverseModelNameMap(model);
1255+
const mappedModel = this.mapModelName(model);
12581256

12591257
const relator = new Relator(
12601258
async (data) => {

0 commit comments

Comments
 (0)