From 48c864d13d1672d765bebec4f0c60a61bf784cb5 Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Sat, 19 Jul 2025 11:55:49 +0800 Subject: [PATCH] chore(openapi): test case for model name mapping --- .../openapi/tests/openapi-restful.test.ts | 35 ++++++++++++++++++- packages/server/tests/api/rest.test.ts | 2 +- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/packages/plugins/openapi/tests/openapi-restful.test.ts b/packages/plugins/openapi/tests/openapi-restful.test.ts index a66f45941..bde8ea53c 100644 --- a/packages/plugins/openapi/tests/openapi-restful.test.ts +++ b/packages/plugins/openapi/tests/openapi-restful.test.ts @@ -129,7 +129,7 @@ model Bar { } }); - it('options', async () => { + it('common options', async () => { const { model, dmmf, modelFile } = await loadZModelAndDmmf(` plugin openapi { provider = '${normalizePath(path.resolve(__dirname, '../dist'))}' @@ -396,6 +396,39 @@ model User { expect.arrayContaining(['role', 'company']) ); }); + + it('works with mapped model name', async () => { + const { model, dmmf, modelFile } = await loadZModelAndDmmf(` +plugin openapi { + provider = '${normalizePath(path.resolve(__dirname, '../dist'))}' + title = 'My Awesome API' + prefix = '/api' + modelNameMapping = { + User: 'myUser' + } +} + +model User { + id String @id + posts Post[] +} + +model Post { + id String @id + author User @relation(fields: [authorId], references: [id]) + authorId String +} + `); + + const { name: output } = tmp.fileSync({ postfix: '.yaml' }); + const options = buildOptions(model, modelFile, output); + await generate(model, options, dmmf); + console.log('OpenAPI specification generated:', output); + const api = await OpenAPIParser.validate(output); + expect(api.paths?.['/api/myUser']).toBeTruthy(); + expect(api.paths?.['/api/user']).toBeFalsy(); + expect(api.paths?.['/api/post']).toBeTruthy(); + }); }); function buildOptions(model: Model, modelFile: string, output: string, specVersion = '3.0.0') { diff --git a/packages/server/tests/api/rest.test.ts b/packages/server/tests/api/rest.test.ts index 2ef74e5d2..c0023873a 100644 --- a/packages/server/tests/api/rest.test.ts +++ b/packages/server/tests/api/rest.test.ts @@ -3038,7 +3038,7 @@ describe('REST server tests', () => { const _handler = makeHandler({ endpoint: 'http://localhost/api', modelNameMapping: { - user: 'myUser', + User: 'myUser', }, }); handler = (args) =>