-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
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): middleware does not apply to a controller that has versioning #10835
Closed
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { INestApplication, VersioningType } from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
import * as request from 'supertest'; | ||
import { expect } from 'chai'; | ||
import { AppWithMiddlewareModule } from '../src/app-with-middleware.module'; | ||
import * as sinon from 'sinon'; | ||
import { SpyInjectToken } from '../src/versioning-middleware'; | ||
|
||
describe('MiddlewareVersioning', () => { | ||
let app: INestApplication; | ||
let spy = sinon.spy(); | ||
|
||
before(async () => { | ||
const moduleRef = await Test.createTestingModule({ | ||
imports: [AppWithMiddlewareModule], | ||
}).compile(); | ||
|
||
app = moduleRef.createNestApplication(); | ||
app.enableVersioning({ | ||
type: VersioningType.URI, | ||
defaultVersion: '-default-version', | ||
}); | ||
await app.init(); | ||
|
||
spy = app.get<sinon.SinonSpy>(SpyInjectToken); | ||
}); | ||
|
||
beforeEach(() => spy.resetHistory()); | ||
|
||
['/v-default-version/foo/bar', '/v1/', '/v3', '/v4'].forEach(path => { | ||
it('should call middleware for route with version', async () => { | ||
const response = await request(app.getHttpServer()).get(path); | ||
|
||
expect(response.status).to.eq(200); | ||
expect(spy.called).to.be.true; | ||
}); | ||
}); | ||
|
||
it('should not call middleware if the controller route is not defined', async () => { | ||
const response = await request(app.getHttpServer()).get('/v2/'); | ||
|
||
expect(response.status).to.eq(200); | ||
expect(spy.called).to.be.false; | ||
}); | ||
|
||
after(() => app.close()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Controller, Get, Version } from '@nestjs/common'; | ||
|
||
@Controller({ | ||
version: '3', | ||
}) | ||
export class AppV3Controller { | ||
@Get() | ||
paramV3() { | ||
return 'V3'; | ||
} | ||
|
||
@Version('4') | ||
@Get() | ||
paramV4() { | ||
return 'V4'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; | ||
import { SpyInjectToken, VersioningMiddleware } from './versioning-middleware'; | ||
import { AppV1Controller } from './app-v1.controller'; | ||
import * as sinon from 'sinon'; | ||
import { AppV3Controller } from './app-v3.controller'; | ||
import { NoVersioningController } from './no-versioning.controller'; | ||
import { AppV2Controller } from './app-v2.controller'; | ||
|
||
@Module({ | ||
providers: [ | ||
{ | ||
provide: SpyInjectToken, | ||
useValue: sinon.spy(), | ||
}, | ||
], | ||
controllers: [ | ||
NoVersioningController, | ||
AppV1Controller, | ||
AppV2Controller, | ||
AppV3Controller, | ||
], | ||
}) | ||
export class AppWithMiddlewareModule implements NestModule { | ||
configure(consumer: MiddlewareConsumer) { | ||
consumer | ||
.apply(VersioningMiddleware) | ||
.forRoutes(NoVersioningController, AppV1Controller, AppV3Controller); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Inject, Injectable, NestMiddleware } from '@nestjs/common'; | ||
import * as sinon from 'sinon'; | ||
import { NextFunction } from 'express'; | ||
|
||
export const SpyInjectToken = 'SpyInjectToken'; | ||
|
||
@Injectable() | ||
export class VersioningMiddleware implements NestMiddleware { | ||
constructor(@Inject(SpyInjectToken) private readonly spy: sinon.SinonSpy) {} | ||
|
||
use(req: any, res: any, next: NextFunction): any { | ||
this.spy(req, res); | ||
next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIR this may cause some unexpected side-effects.
RouterExplorer#applyPathsToRouterProxy
assumes that "version" attribute represents the method-level version https://github.com/nestjs/nest/pull/10835/files#diff-652791ce58d92c15826b2be408ac4b6b0b1c7269664536c3d2e89b9e6f49699fR144 while now it equals to "method"/"controller"/"global"-level.Ideally, to avoid regressions, we should make sure not to update the "scanForPaths" method (leave it as is) :(