Skip to content

Commit

Permalink
Merge pull request #13536 from Fcmam5/fix/do-not-crash-when_not_using…
Browse files Browse the repository at this point in the history
…-enabledVersioning

feat(fastify): Do not crash if enableVersioning is not used
  • Loading branch information
kamilmysliwiec authored Jun 3, 2024
2 parents 4ff2d6e + 7a61ecc commit 2d5a7b8
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 4 deletions.
98 changes: 98 additions & 0 deletions integration/versioning/e2e/default-versioning.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { INestApplication, VersioningType } from '@nestjs/common';
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { AppModule } from '../src/app.module';

/**
* `.enableVersioning()` uses `VersioningType.URI` type by default
* Regression test for #13496
* @see [Versioning](https://docs.nestjs.com/techniques/versioning)
*/
describe('Default Versioning behavior', () => {
// ======================================================================== //
describe('Express', () => {
let app: INestApplication;
before(async () => {
const moduleRef = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = moduleRef.createNestApplication();
app.enableVersioning();
await app.init();
});

describe('GET /', () => {
it('V1', () => {
return request(app.getHttpServer())
.get('/v1')
.expect(200)
.expect('Hello World V1!');
});

it('No Version', () => {
return request(app.getHttpServer()).get('/').expect(404);
});
});

describe('GET /neutral', () => {
it('No Version', () => {
return request(app.getHttpServer())
.get('/neutral')
.expect(200)
.expect('Neutral');
});
});

after(async () => {
await app.close();
});
});

// ======================================================================== //
describe('Fastify', () => {
let app: INestApplication;
before(async () => {
const moduleRef = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = moduleRef.createNestApplication<NestFastifyApplication>(
new FastifyAdapter(),
);
app.enableVersioning();
await app.init();
await app.getHttpAdapter().getInstance().ready();
});

describe('GET /', () => {
it('V1', () => {
return request(app.getHttpServer())
.get('/v1')
.expect(200)
.expect('Hello World V1!');
});

it('No Version', () => {
return request(app.getHttpServer()).get('/').expect(404);
});
});

describe('GET /neutral', () => {
it('No Version', () => {
return request(app.getHttpServer())
.get('/neutral')
.expect(200)
.expect('Neutral');
});
});

after(async () => {
await app.close();
});
});
});
8 changes: 4 additions & 4 deletions packages/platform-fastify/adapters/fastify-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class FastifyAdapter<

private _isParserRegistered: boolean;
private isMiddieRegistered: boolean;
private versioningOptions: VersioningOptions;
private versioningOptions?: VersioningOptions;
private readonly versionConstraint = {
name: 'version',
validate(value: unknown) {
Expand Down Expand Up @@ -166,7 +166,7 @@ export class FastifyAdapter<
},
deriveConstraint: (req: FastifyRequest) => {
// Media Type (Accept Header) Versioning Handler
if (this.versioningOptions.type === VersioningType.MEDIA_TYPE) {
if (this.versioningOptions?.type === VersioningType.MEDIA_TYPE) {
const MEDIA_TYPE_HEADER = 'Accept';
const acceptHeaderValue: string | undefined = (req.headers?.[
MEDIA_TYPE_HEADER
Expand All @@ -181,7 +181,7 @@ export class FastifyAdapter<
: acceptHeaderVersionParameter.split(this.versioningOptions.key)[1];
}
// Header Versioning Handler
else if (this.versioningOptions.type === VersioningType.HEADER) {
else if (this.versioningOptions?.type === VersioningType.HEADER) {
const customHeaderVersionParameter: string | string[] | undefined =
req.headers?.[this.versioningOptions.header] ||
req.headers?.[this.versioningOptions.header.toLowerCase()];
Expand All @@ -191,7 +191,7 @@ export class FastifyAdapter<
: customHeaderVersionParameter;
}
// Custom Versioning Handler
else if (this.versioningOptions.type === VersioningType.CUSTOM) {
else if (this.versioningOptions?.type === VersioningType.CUSTOM) {
return this.versioningOptions.extractor(req);
}
return undefined;
Expand Down

0 comments on commit 2d5a7b8

Please sign in to comment.