Skip to content

Commit b199ac2

Browse files
authored
merge dev to main (v2.17.0) (#2191)
2 parents b73b3de + f835eb5 commit b199ac2

File tree

39 files changed

+1171
-365
lines changed

39 files changed

+1171
-365
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "zenstack-monorepo",
3-
"version": "2.16.1",
3+
"version": "2.17.0",
44
"description": "",
55
"scripts": {
66
"build": "pnpm -r --filter=\"!./packages/ide/*\" build",

packages/ide/jetbrains/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ plugins {
99
}
1010

1111
group = "dev.zenstack"
12-
version = "2.16.1"
12+
version = "2.17.0"
1313

1414
repositories {
1515
mavenCentral()

packages/ide/jetbrains/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jetbrains",
3-
"version": "2.16.1",
3+
"version": "2.17.0",
44
"displayName": "ZenStack JetBrains IDE Plugin",
55
"description": "ZenStack JetBrains IDE plugin",
66
"homepage": "https://zenstack.dev",

packages/language/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/language",
3-
"version": "2.16.1",
3+
"version": "2.17.0",
44
"displayName": "ZenStack modeling language compiler",
55
"description": "ZenStack modeling language compiler",
66
"homepage": "https://zenstack.dev",

packages/misc/redwood/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/redwood",
33
"displayName": "ZenStack RedwoodJS Integration",
4-
"version": "2.16.1",
4+
"version": "2.17.0",
55
"description": "CLI and runtime for integrating ZenStack with RedwoodJS projects.",
66
"repository": {
77
"type": "git",

packages/plugins/openapi/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/openapi",
33
"displayName": "ZenStack Plugin and Runtime for OpenAPI",
4-
"version": "2.16.1",
4+
"version": "2.17.0",
55
"description": "ZenStack plugin and runtime supporting OpenAPI",
66
"main": "index.js",
77
"repository": {

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/plugins/openapi/tests/openapi-restful.test.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ model Bar {
129129
}
130130
});
131131

132-
it('options', async () => {
132+
it('common options', async () => {
133133
const { model, dmmf, modelFile } = await loadZModelAndDmmf(`
134134
plugin openapi {
135135
provider = '${normalizePath(path.resolve(__dirname, '../dist'))}'
@@ -396,6 +396,39 @@ model User {
396396
expect.arrayContaining(['role', 'company'])
397397
);
398398
});
399+
400+
it('works with mapped model name', async () => {
401+
const { model, dmmf, modelFile } = await loadZModelAndDmmf(`
402+
plugin openapi {
403+
provider = '${normalizePath(path.resolve(__dirname, '../dist'))}'
404+
title = 'My Awesome API'
405+
prefix = '/api'
406+
modelNameMapping = {
407+
User: 'myUser'
408+
}
409+
}
410+
411+
model User {
412+
id String @id
413+
posts Post[]
414+
}
415+
416+
model Post {
417+
id String @id
418+
author User @relation(fields: [authorId], references: [id])
419+
authorId String
420+
}
421+
`);
422+
423+
const { name: output } = tmp.fileSync({ postfix: '.yaml' });
424+
const options = buildOptions(model, modelFile, output);
425+
await generate(model, options, dmmf);
426+
console.log('OpenAPI specification generated:', output);
427+
const api = await OpenAPIParser.validate(output);
428+
expect(api.paths?.['/api/myUser']).toBeTruthy();
429+
expect(api.paths?.['/api/user']).toBeFalsy();
430+
expect(api.paths?.['/api/post']).toBeTruthy();
431+
});
399432
});
400433

401434
function buildOptions(model: Model, modelFile: string, output: string, specVersion = '3.0.0') {

packages/plugins/swr/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/swr",
33
"displayName": "ZenStack plugin for generating SWR hooks",
4-
"version": "2.16.1",
4+
"version": "2.17.0",
55
"description": "ZenStack plugin for generating SWR hooks",
66
"main": "index.js",
77
"repository": {

packages/plugins/tanstack-query/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/tanstack-query",
33
"displayName": "ZenStack plugin for generating tanstack-query hooks",
4-
"version": "2.16.1",
4+
"version": "2.17.0",
55
"description": "ZenStack plugin for generating tanstack-query hooks",
66
"main": "index.js",
77
"exports": {

0 commit comments

Comments
 (0)