From 5fdbe7d2ce95b8eba0b23318af55a3f454d53eed Mon Sep 17 00:00:00 2001 From: Matatjahu Date: Mon, 17 Jan 2022 16:03:36 +0100 Subject: [PATCH 1/2] fix: add prefix to the all paths and fix 200 response od /generate --- openapi.yaml | 5 +++-- src/app.ts | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/openapi.yaml b/openapi.yaml index 11e01d1d..3bc77bce 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -38,9 +38,10 @@ paths: '200': description: Template succesfully generated. content: - application/json: + application/octet-stream: schema: - $ref: '#/components/schemas/Template' + type: string + format: binary '400': description: Failed to generate the given template due to invalid AsyncAPI document. content: diff --git a/src/app.ts b/src/app.ts index 1fba70ec..6d5de48f 100644 --- a/src/app.ts +++ b/src/app.ts @@ -60,7 +60,8 @@ export class App { private initializeControllers(controller: Controller[]) { controller.forEach(controller => { - this.app.use('/', controller.boot()); + // in the `openapi.yaml` we have prefix `v1` for all paths + this.app.use('/v1/', controller.boot()); }); } From 62192683a2a6c0cd04a26df6a5f6a348459d1b5f Mon Sep 17 00:00:00 2001 From: Matatjahu Date: Mon, 17 Jan 2022 16:16:19 +0100 Subject: [PATCH 2/2] fix tests --- src/controllers/tests/generator.controller.test.ts | 6 +++--- src/middlewares/request-body-validation.middleware.ts | 4 +++- .../tests/document-validation.middleware.test.ts | 6 +++--- src/middlewares/tests/problem.middleware.test.ts | 4 ++-- .../tests/request-body-validation.middleware.test.ts | 6 +++--- 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/controllers/tests/generator.controller.test.ts b/src/controllers/tests/generator.controller.test.ts index 938bb81e..2214fc4a 100644 --- a/src/controllers/tests/generator.controller.test.ts +++ b/src/controllers/tests/generator.controller.test.ts @@ -11,7 +11,7 @@ describe('GeneratorController', () => { const app = new App([new GenerateController()]); return await request(app.getServer()) - .post('/generate') + .post('/v1/generate') .send({ asyncapi: { asyncapi: '2.2.0', @@ -33,7 +33,7 @@ describe('GeneratorController', () => { const app = new App([new GenerateController()]); return await request(app.getServer()) - .post('/generate') + .post('/v1/generate') .send({ asyncapi: { asyncapi: '2.2.0', @@ -52,7 +52,7 @@ describe('GeneratorController', () => { const app = new App([new GenerateController()]); return await request(app.getServer()) - .post('/generate') + .post('/v1/generate') .send({ asyncapi: { asyncapi: '2.2.0', diff --git a/src/middlewares/request-body-validation.middleware.ts b/src/middlewares/request-body-validation.middleware.ts index 71fd4f30..30719c36 100644 --- a/src/middlewares/request-body-validation.middleware.ts +++ b/src/middlewares/request-body-validation.middleware.ts @@ -15,7 +15,9 @@ const ajv = new Ajv({ * Retrieve proper AJV's validator function, create or reuse it. */ async function getValidator(req: Request) { - const { path: reqPath, method } = req; + const method = req.method; + let reqPath = req.path; + reqPath = reqPath.startsWith('/v1') ? reqPath.replace('/v1', '') : reqPath; const schemaName = `${reqPath}->${method}`; const validate = ajv.getSchema(schemaName); diff --git a/src/middlewares/tests/document-validation.middleware.test.ts b/src/middlewares/tests/document-validation.middleware.test.ts index 46b7ee0e..d3e7d89e 100644 --- a/src/middlewares/tests/document-validation.middleware.test.ts +++ b/src/middlewares/tests/document-validation.middleware.test.ts @@ -21,7 +21,7 @@ describe('documentValidationMiddleware', () => { const app = new App([new TestController()]); return await request(app.getServer()) - .post('/test') + .post('/v1/test') .send({}) .expect(200, { success: true, @@ -40,7 +40,7 @@ describe('documentValidationMiddleware', () => { const app = new App([new TestController()]); return await request(app.getServer()) - .post('/test') + .post('/v1/test') .send({ asyncapi: { asyncapi: '2.2.0', @@ -97,7 +97,7 @@ describe('documentValidationMiddleware', () => { const app = new App([new TestController()]); return await request(app.getServer()) - .post('/test') + .post('/v1/test') .send({ // without title, version and channels asyncapi: { diff --git a/src/middlewares/tests/problem.middleware.test.ts b/src/middlewares/tests/problem.middleware.test.ts index 89f3d990..ac943ce9 100644 --- a/src/middlewares/tests/problem.middleware.test.ts +++ b/src/middlewares/tests/problem.middleware.test.ts @@ -23,7 +23,7 @@ describe('problemMiddleware', () => { const app = new App([new TestController()]); return await request(app.getServer()) - .post('/test') + .post('/v1/test') .send({}) .expect(422, { type: ProblemException.createType('custom-problem'), @@ -43,7 +43,7 @@ describe('problemMiddleware', () => { const app = new App([new TestController()]); return await request(app.getServer()) - .post('/test') + .post('/v1/test') .send({}) .expect(200, { success: true, diff --git a/src/middlewares/tests/request-body-validation.middleware.test.ts b/src/middlewares/tests/request-body-validation.middleware.test.ts index 2df81d31..b479ebc9 100644 --- a/src/middlewares/tests/request-body-validation.middleware.test.ts +++ b/src/middlewares/tests/request-body-validation.middleware.test.ts @@ -20,7 +20,7 @@ describe('requestBodyValidationMiddleware', () => { const app = new App([new TestController()]); return await request(app.getServer()) - .post('/generate') + .post('/v1/generate') .send({ asyncapi: { asyncapi: '2.2.0', @@ -39,7 +39,7 @@ describe('requestBodyValidationMiddleware', () => { it('should throw error when request body is invalid', async () => { const TestController = createTestController({ - path: '/test', + path: '/generate', method: 'post', callback: (_, res) => { res.status(200).send({ success: true }); @@ -48,7 +48,7 @@ describe('requestBodyValidationMiddleware', () => { const app = new App([new TestController()]); return await request(app.getServer()) - .post('/generate') + .post('/v1/generate') .send({ asyncapi: { asyncapi: '2.2.0',