Skip to content

Commit

Permalink
Merge branch 'flamewow-fix/fastify_ignoreTrailingSlash'
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jul 11, 2022
2 parents 05d9ca2 + 6b1a099 commit 6d8cd31
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
24 changes: 14 additions & 10 deletions e2e/manual-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ const host = 'localhost';
const docRelPath = '/api-docs';

const USE_FASTIFY = true;
const ENABLE_BASIC_AUTH = true;
const ENABLE_BASIC_AUTH = false;

const adapter = USE_FASTIFY ? new FastifyAdapter() : new ExpressAdapter();
const adapter = USE_FASTIFY
? new FastifyAdapter({
ignoreTrailingSlash: false
})
: new ExpressAdapter();
const publicFolderPath = join(__dirname, '../../e2e', 'public');

async function bootstrap() {
Expand All @@ -30,7 +34,7 @@ async function bootstrap() {
const httpAdapter = app.getHttpAdapter();

ENABLE_BASIC_AUTH &&
httpAdapter.use('/api-docs', (req, res, next) => {
httpAdapter.use(docRelPath, (req, res, next) => {
function parse(input: string): { name: string; pass: string } {
const [, encodedPart] = input.split(' ');

Expand Down Expand Up @@ -109,13 +113,13 @@ async function bootstrap() {
customCssUrl: '/public/theme.css' // to showcase that in new implementation u can use custom css with fastify
});

SwaggerModule.setup('/swagger-docs', app, document, {
customSiteTitle: 'Demo API - Swagger UI 2',
swaggerOptions: {
persistAuthorization: true,
defaultModelsExpandDepth: -1
}
});
// SwaggerModule.setup('/swagger-docs', app, document, {
// customSiteTitle: 'Demo API - Swagger UI 2',
// swaggerOptions: {
// persistAuthorization: true,
// defaultModelsExpandDepth: -1
// }
// });

SwaggerModule.setup('/swagger-docs', app, document, {
customSiteTitle: 'Demo API - Swagger UI 3',
Expand Down
35 changes: 22 additions & 13 deletions lib/swagger-module.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { INestApplication } from '@nestjs/common';
import { HttpServer } from '@nestjs/common/interfaces/http/http-server.interface';
import { NestExpressApplication } from '@nestjs/platform-express';
import { NestFastifyApplication } from '@nestjs/platform-fastify';
import * as jsyaml from 'js-yaml';
import {
OpenAPIObject,
SwaggerDocumentOptions,
SwaggerCustomOptions
SwaggerCustomOptions,
SwaggerDocumentOptions
} from './interfaces';
import { SwaggerScanner } from './swagger-scanner';
import { assignTwoLevelsDeep } from './utils/assign-two-levels-deep';
import { getGlobalPrefix } from './utils/get-global-prefix';
import { validatePath } from './utils/validate-path.util';
import * as jsyaml from 'js-yaml';
import {
buildSwaggerHTML,
buildSwaggerInitJS,
swaggerAssetsAbsoluteFSPath
} from './swagger-ui';
import { NestFastifyApplication } from '@nestjs/platform-fastify';
import { NestExpressApplication } from '@nestjs/platform-express';
import { HttpServer } from '@nestjs/common/interfaces/http/http-server.interface';
import { assignTwoLevelsDeep } from './utils/assign-two-levels-deep';
import { getGlobalPrefix } from './utils/get-global-prefix';
import { validatePath } from './utils/validate-path.util';

export class SwaggerModule {
public static createDocument(
Expand Down Expand Up @@ -77,10 +77,19 @@ export class SwaggerModule {
});

// fastify doesn't resolve 'routePath/' -> 'routePath', that's why we handle it manually
httpAdapter.get(finalPath + '/', (req, res) => {
res.type('text/html');
res.send(html);
});
try {
httpAdapter.get(finalPath + '/', (req, res) => {
res.type('text/html');
res.send(html);
});
} catch (err) {
/**
* When Fastify adapter is being used with the "ignoreTrailingSlash" configuration option set to "true",
* declaration of the route "finalPath/" will throw an error because of the following conflict:
* Method '${method}' already declared for route '${path}' with constraints '${JSON.stringify(constraints)}.
* We can simply ignore that error here.
*/
}

httpAdapter.get(`${finalPath}-json`, (req, res) => {
res.type('application/json');
Expand Down

0 comments on commit 6d8cd31

Please sign in to comment.