diff --git a/src/resolvers.ts b/src/resolvers.ts index 68dd21b7..3092e3a3 100644 --- a/src/resolvers.ts +++ b/src/resolvers.ts @@ -35,7 +35,7 @@ export function defaultResolver( tmpModules[modulePath] = require(modulePath); } - const handler = tmpModules[modulePath][oId] || tmpModules[modulePath].default; + const handler = tmpModules[modulePath][oId] || tmpModules[modulePath].default[oId] || tmpModules[modulePath].default; if (!handler) { throw Error( diff --git a/test/default.export.fn.spec.ts b/test/default.export.fn.spec.ts new file mode 100644 index 00000000..d0cdaebe --- /dev/null +++ b/test/default.export.fn.spec.ts @@ -0,0 +1,46 @@ +import * as express from 'express'; +import * as OpenApiValidator from '../src'; +import { expect } from 'chai'; +import * as request from 'supertest'; +import * as path from 'path'; + +describe('default export resolver', () => { + let server = null; + let app = express(); + + before(async () => { + app.use( + OpenApiValidator.middleware({ + apiSpec: { + openapi: '3.0.0', + info: { version: '1.0.0', title: 'test bug OpenApiValidator' }, + paths: { + '/': { + get: { + operationId: 'test#get', + // @ts-ignore + 'x-eov-operation-handler': 'routes/default-export-fn', + responses: { 200: { description: 'homepage' } } + } + }, + }, + }, + operationHandlers: path.join(__dirname, 'resources'), + }), + ); + + server = app.listen(3000); + console.log('server start port 3000'); + }); + + after(async () => server.close()); + + it('should use default export operation', async () => { + return request(app) + .get(`/`) + .expect(200) + .then((r) => { + expect(r.body).to.have.property('message').that.equals("It Works!"); + }); + }); +}); diff --git a/test/resources/routes/default-export-fn.js b/test/resources/routes/default-export-fn.js new file mode 100644 index 00000000..a5d27615 --- /dev/null +++ b/test/resources/routes/default-export-fn.js @@ -0,0 +1,5 @@ +exports.default = { + 'test#get': (req, res) => { + res.status(200).json({ message: 'It Works!' }); + }, +};