Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): root path middleware not applied when setting the global prefix #12179

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ describe('Global prefix', () => {
await request(server).get('/health').expect(404);

await request(server).get('/api/v1/health').expect(200);

await request(server)
.get('/api/v1')
.expect(200, 'Root: Data attached in middleware');
});

it(`should exclude the path as string`, async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { Controller, Get, Post, Req } from '@nestjs/common';

@Controller()
export class AppController {
@Get()
root(@Req() req): string {
return 'Root: ' + req.extras?.data;
}

@Get('hello/:name')
getHello(@Req() req): string {
return 'Hello: ' + req.extras?.data;
Expand Down
12 changes: 8 additions & 4 deletions packages/core/middleware/route-info-path-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ export class RouteInfoPathExtractor {
if (this.isAWildcard(path)) {
const entries =
versionPaths.length > 0
? versionPaths.map(
versionPath =>
? versionPaths
.map(versionPath => [
this.prefixPath + versionPath,
this.prefixPath + versionPath + addLeadingSlash(path),
)
: [this.prefixPath + addLeadingSlash(path)];
])
.flat()
: this.prefixPath
? [this.prefixPath, this.prefixPath + addLeadingSlash(path)]
: [addLeadingSlash(path)];

return Array.isArray(this.excludedGlobalPrefixRoutes)
? [
Expand Down
3 changes: 2 additions & 1 deletion packages/core/middleware/routes-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Module } from '../injector/module';
import { MetadataScanner } from '../metadata-scanner';
import { PathsExplorer, RouteDefinition } from '../router/paths-explorer';
import { targetModulesByContainer } from '../router/router-module';
import { RequestMethod } from '@nestjs/common';

export class RoutesMapper {
private readonly pathsExplorer: PathsExplorer;
Expand All @@ -46,7 +47,7 @@ export class RoutesMapper {
}

private getRouteInfoFromPath(routePath: string): RouteInfo[] {
const defaultRequestMethod = -1;
const defaultRequestMethod = RequestMethod.ALL;
return [
{
path: addLeadingSlash(routePath),
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/middleware/builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ describe('MiddlewareBuilder', () => {
expect(proxy.getExcludedRoutes()).to.be.eql([
{
path,
method: -1 as any,
method: RequestMethod.ALL,
},
]);
});
Expand Down
10 changes: 5 additions & 5 deletions packages/core/test/middleware/route-info-path-extractor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('RouteInfoPathExtractor', () => {
method: RequestMethod.ALL,
version: '1',
}),
).to.eql(['/v1/*']);
).to.eql(['/v1', '/v1/*']);
});

it(`should return correct paths when set global prefix`, () => {
Expand All @@ -42,15 +42,15 @@ describe('RouteInfoPathExtractor', () => {
path: '*',
method: RequestMethod.ALL,
}),
).to.eql(['/api/*']);
).to.eql(['/api', '/api/*']);

expect(
routeInfoPathExtractor.extractPathsFrom({
path: '*',
method: RequestMethod.ALL,
version: '1',
}),
).to.eql(['/api/v1/*']);
).to.eql(['/api/v1', '/api/v1/*']);
});

it(`should return correct paths when set global prefix and global prefix options`, () => {
Expand All @@ -66,15 +66,15 @@ describe('RouteInfoPathExtractor', () => {
path: '*',
method: RequestMethod.ALL,
}),
).to.eql(['/api/*', '/foo']);
).to.eql(['/api', '/api/*', '/foo']);

expect(
routeInfoPathExtractor.extractPathsFrom({
path: '*',
method: RequestMethod.ALL,
version: '1',
}),
).to.eql(['/api/v1/*', '/v1/foo']);
).to.eql(['/api/v1', '/api/v1/*', '/v1/foo']);

expect(
routeInfoPathExtractor.extractPathsFrom({
Expand Down
Loading