Skip to content

Commit c0af8af

Browse files
committed
feat(fastify): Do not crash if enableVersioning is not used (fixes #13496)
1 parent 1db72fd commit c0af8af

File tree

2 files changed

+101
-3
lines changed

2 files changed

+101
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { INestApplication, VersioningType } from '@nestjs/common';
2+
import {
3+
FastifyAdapter,
4+
NestFastifyApplication,
5+
} from '@nestjs/platform-fastify';
6+
import { Test } from '@nestjs/testing';
7+
import * as request from 'supertest';
8+
import { AppModule } from '../src/app.module';
9+
10+
/**
11+
* `.enableVersioning()` uses `VersioningType.URI` type by default
12+
* Regression test for #13496
13+
* @see [Versioning](https://docs.nestjs.com/techniques/versioning)
14+
*/
15+
describe('Default Versioning behavior', () => {
16+
// ======================================================================== //
17+
describe('Express', () => {
18+
let app: INestApplication;
19+
before(async () => {
20+
const moduleRef = await Test.createTestingModule({
21+
imports: [AppModule],
22+
}).compile();
23+
24+
app = moduleRef.createNestApplication();
25+
app.enableVersioning();
26+
await app.init();
27+
});
28+
29+
describe('GET /', () => {
30+
it('V1', () => {
31+
return request(app.getHttpServer())
32+
.get('/v1')
33+
.expect(200)
34+
.expect('Hello World V1!');
35+
});
36+
37+
it('No Version', () => {
38+
return request(app.getHttpServer()).get('/').expect(404);
39+
});
40+
});
41+
42+
describe('GET /neutral', () => {
43+
it('No Version', () => {
44+
return request(app.getHttpServer())
45+
.get('/neutral')
46+
.expect(200)
47+
.expect('Neutral');
48+
});
49+
});
50+
51+
after(async () => {
52+
await app.close();
53+
});
54+
});
55+
56+
// ======================================================================== //
57+
describe('Fastify', () => {
58+
let app: INestApplication;
59+
before(async () => {
60+
const moduleRef = await Test.createTestingModule({
61+
imports: [AppModule],
62+
}).compile();
63+
64+
app = moduleRef.createNestApplication<NestFastifyApplication>(
65+
new FastifyAdapter(),
66+
);
67+
app.enableVersioning();
68+
await app.init();
69+
await app.getHttpAdapter().getInstance().ready();
70+
});
71+
72+
describe('GET /', () => {
73+
it('V1', () => {
74+
return request(app.getHttpServer())
75+
.get('/v1')
76+
.expect(200)
77+
.expect('Hello World V1!');
78+
});
79+
80+
it('No Version', () => {
81+
return request(app.getHttpServer()).get('/').expect(404);
82+
});
83+
});
84+
85+
describe('GET /neutral', () => {
86+
it('No Version', () => {
87+
return request(app.getHttpServer())
88+
.get('/neutral')
89+
.expect(200)
90+
.expect('Neutral');
91+
});
92+
});
93+
94+
after(async () => {
95+
await app.close();
96+
});
97+
});
98+
});

packages/platform-fastify/adapters/fastify-adapter.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export class FastifyAdapter<
166166
},
167167
deriveConstraint: (req: FastifyRequest) => {
168168
// Media Type (Accept Header) Versioning Handler
169-
if (this.versioningOptions.type === VersioningType.MEDIA_TYPE) {
169+
if (this.versioningOptions?.type === VersioningType.MEDIA_TYPE) {
170170
const MEDIA_TYPE_HEADER = 'Accept';
171171
const acceptHeaderValue: string | undefined = (req.headers?.[
172172
MEDIA_TYPE_HEADER
@@ -181,7 +181,7 @@ export class FastifyAdapter<
181181
: acceptHeaderVersionParameter.split(this.versioningOptions.key)[1];
182182
}
183183
// Header Versioning Handler
184-
else if (this.versioningOptions.type === VersioningType.HEADER) {
184+
else if (this.versioningOptions?.type === VersioningType.HEADER) {
185185
const customHeaderVersionParameter: string | string[] | undefined =
186186
req.headers?.[this.versioningOptions.header] ||
187187
req.headers?.[this.versioningOptions.header.toLowerCase()];
@@ -191,7 +191,7 @@ export class FastifyAdapter<
191191
: customHeaderVersionParameter;
192192
}
193193
// Custom Versioning Handler
194-
else if (this.versioningOptions.type === VersioningType.CUSTOM) {
194+
else if (this.versioningOptions?.type === VersioningType.CUSTOM) {
195195
return this.versioningOptions.extractor(req);
196196
}
197197
return undefined;

0 commit comments

Comments
 (0)