Skip to content

Commit 0337718

Browse files
committed
bugfix(core): fixed basePath issue with routes-resolver
1 parent 20cbbaf commit 0337718

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

packages/core/router/routes-resolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class RoutesResolver implements Resolver {
4242
let path = metatype
4343
? Reflect.getMetadata(MODULE_PATH, metatype)
4444
: undefined;
45-
path = path ? path + basePath : basePath;
45+
path = path ? basePath + path : basePath;
4646
this.registerRouters(routes, moduleName, path, appInstance);
4747
});
4848
}

packages/core/test/router/routes-resolver.spec.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import { BadRequestException, Post } from '@nestjs/common';
1+
import { BadRequestException, Post, Module } from '@nestjs/common';
22
import { expect } from 'chai';
33
import * as sinon from 'sinon';
44
import { Controller } from '../../../common/decorators/core/controller.decorator';
55
import { Get } from '../../../common/decorators/http/request-mapping.decorator';
66
import { ExpressAdapter } from '../../adapters/express-adapter';
77
import { ApplicationConfig } from '../../application-config';
88
import { RoutesResolver } from '../../router/routes-resolver';
9+
import { MODULE_PATH } from '@nestjs/common/constants';
910

1011
describe('RoutesResolver', () => {
12+
1113
@Controller('global')
1214
class TestRoute {
1315
@Get('test')
@@ -17,6 +19,16 @@ describe('RoutesResolver', () => {
1719
public anotherTest() {}
1820
}
1921

22+
@Module({
23+
controllers: [TestRoute]
24+
})
25+
class TestModule {}
26+
27+
@Module({
28+
controllers: [TestRoute]
29+
})
30+
class TestModule2 {}
31+
2032
let router;
2133
let routesResolver: RoutesResolver;
2234
let container;
@@ -97,6 +109,54 @@ describe('RoutesResolver', () => {
97109
);
98110
expect(spy.calledTwice).to.be.true;
99111
});
112+
113+
describe('registerRouters', () => {
114+
it('should register each module with the base path and append the __module_path__ if present ', () => {
115+
const routes = new Map();
116+
routes.set('TestRoute', {
117+
instance: new TestRoute(),
118+
metatype: TestRoute,
119+
});
120+
121+
Reflect.defineMetadata(MODULE_PATH, '/test', TestModule);
122+
modules.set('TestModule', { routes, metatype: TestModule });
123+
modules.set('TestModule2', { routes, metatype: TestModule2 });
124+
125+
const spy = sinon
126+
.stub(routesResolver, 'registerRouters')
127+
.callsFake(() => undefined);
128+
129+
routesResolver.resolve(applicationRef, 'api/v1');
130+
131+
// with module path
132+
expect(spy.getCall(0).calledWith(sinon.match.any, sinon.match.any, 'api/v1/test', sinon.match.any)).to.be.true;
133+
// without module path
134+
expect(spy.getCall(1).calledWith(sinon.match.any, sinon.match.any, 'api/v1', sinon.match.any)).to.be.true;
135+
});
136+
137+
it('should register each module with __module_path__ if present and no basePath ', () => {
138+
const routes = new Map();
139+
routes.set('TestRoute', {
140+
instance: new TestRoute(),
141+
metatype: TestRoute,
142+
});
143+
144+
Reflect.defineMetadata(MODULE_PATH, '/test', TestModule);
145+
modules.set('TestModule', { routes, metatype: TestModule });
146+
modules.set('TestModule2', { routes, metatype: TestModule2 });
147+
148+
const spy = sinon
149+
.stub(routesResolver, 'registerRouters')
150+
.callsFake(() => undefined);
151+
152+
routesResolver.resolve(applicationRef, '');
153+
154+
// with module path
155+
expect(spy.getCall(0).calledWith(sinon.match.any, sinon.match.any, '/test', sinon.match.any)).to.be.true;
156+
// without module path
157+
expect(spy.getCall(1).calledWith(sinon.match.any, sinon.match.any, '', sinon.match.any)).to.be.true;
158+
});
159+
})
100160
});
101161

102162
describe('mapExternalExceptions', () => {

0 commit comments

Comments
 (0)