Skip to content

Commit f835eb5

Browse files
authored
chore(openapi): test case for model name mapping (#2192)
1 parent 4d75919 commit f835eb5

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

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/server/tests/api/rest.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3038,7 +3038,7 @@ describe('REST server tests', () => {
30383038
const _handler = makeHandler({
30393039
endpoint: 'http://localhost/api',
30403040
modelNameMapping: {
3041-
user: 'myUser',
3041+
User: 'myUser',
30423042
},
30433043
});
30443044
handler = (args) =>

0 commit comments

Comments
 (0)