diff --git a/Dockerfile b/Dockerfile index f12f959c..dd2034c3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -32,4 +32,4 @@ RUN npm ci --only=production COPY openapi.yaml ./ EXPOSE 80 -CMD ["npm", "run", "start:prod"] +CMD ["npm", "run", "start:prod"] \ No newline at end of file diff --git a/openapi.yaml b/openapi.yaml index 886337a1..f01e3e90 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -38,9 +38,10 @@ paths: '200': description: Template successfully generated. content: - application/json: + application/zip: 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()); }); } diff --git a/src/controllers/tests/generator.controller.test.ts b/src/controllers/tests/generator.controller.test.ts index 165b58f1..586e6c89 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 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 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 request(app.getServer()) - .post('/generate') + .post('/v1/generate') .send({ asyncapi: { asyncapi: '2.2.0', diff --git a/src/controllers/tests/validate.controller.test.ts b/src/controllers/tests/validate.controller.test.ts index 0327d96d..de13d494 100644 --- a/src/controllers/tests/validate.controller.test.ts +++ b/src/controllers/tests/validate.controller.test.ts @@ -82,7 +82,7 @@ describe('ValidateController', () => { const app = new App([new ValidateController()]); return request(app.getServer()) - .post('/validate') + .post('/v1/validate') .send({ asyncapi: validJSONAsyncAPI }) @@ -93,7 +93,7 @@ describe('ValidateController', () => { const app = new App([new ValidateController()]); return request(app.getServer()) - .post('/validate') + .post('/v1/validate') .send({ asyncapi: validYAMLAsyncAPI }) @@ -128,7 +128,7 @@ describe('ValidateController', () => { const app = new App([new ValidateController()]); return request(app.getServer()) - .post('/validate') + .post('/v1/validate') .send({ asyncapi: invalidJSONAsyncAPI }) diff --git a/src/middlewares/request-body-validation.middleware.ts b/src/middlewares/request-body-validation.middleware.ts index 3d78438b..6a5834ee 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',