From 5559fb10c33932300d9f863cb3f57c48c504acdc Mon Sep 17 00:00:00 2001 From: "IceHe.xyz" Date: Tue, 5 Jul 2022 12:33:47 +0800 Subject: [PATCH] fix(core): do not titlize tags of .well-known APIs (#1412) --- packages/core/src/routes/swagger.test.ts | 11 ++++++++--- packages/core/src/routes/swagger.ts | 12 +++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/core/src/routes/swagger.test.ts b/packages/core/src/routes/swagger.test.ts index 11af0517e..7c7d357ca 100644 --- a/packages/core/src/routes/swagger.test.ts +++ b/packages/core/src/routes/swagger.test.ts @@ -68,15 +68,20 @@ describe('GET /swagger.json', () => { }); it('should generate the tags', async () => { - const response = await mockSwaggerRequest.get('/swagger.json'); + const testTagRouter = new Router(); + testTagRouter.get('/mock', () => ({})); + testTagRouter.put('/.well-known', () => ({})); + const swaggerRequest = createSwaggerRequest([testTagRouter]); + + const response = await swaggerRequest.get('/swagger.json'); expect(response.body.paths).toMatchObject( expect.objectContaining({ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ '/api/mock': expect.objectContaining({ get: expect.objectContaining({ tags: ['Mock'] }), }), - '/api/test': expect.objectContaining({ - put: expect.objectContaining({ tags: ['Test'] }), + '/api/.well-known': expect.objectContaining({ + put: expect.objectContaining({ tags: ['.well-known'] }), }), /* eslint-enable @typescript-eslint/no-unsafe-assignment */ }) diff --git a/packages/core/src/routes/swagger.ts b/packages/core/src/routes/swagger.ts index 06dec1cc6..815b01af6 100644 --- a/packages/core/src/routes/swagger.ts +++ b/packages/core/src/routes/swagger.ts @@ -62,6 +62,16 @@ const buildParameters = ( })); }; +function buildTag(path: string) { + const root = path.split('/')[1]; + + if (root?.startsWith('.')) { + return root; + } + + return toTitle(root ?? 'General'); +} + const buildOperation = (stack: IMiddleware[], path: string): OpenAPIV3.OperationObject => { const guard = stack.find((function_): function_ is WithGuardConfig => isGuardMiddleware(function_) @@ -85,7 +95,7 @@ const buildOperation = (stack: IMiddleware[], path: string): OpenAPIV3.Operation ]; return { - tags: [toTitle(path.split('/')[1] ?? 'General')], + tags: [buildTag(path)], parameters: [...pathParameters, ...queryParameters], requestBody, responses: {