Skip to content

Commit

Permalink
Merge branch 'fix/fastify_ignoreTrailingSlash' of github.com:flamewow…
Browse files Browse the repository at this point in the history
…/swagger into flamewow-fix/fastify_ignoreTrailingSlash
  • Loading branch information
kamilmysliwiec committed Jul 11, 2022
2 parents 05d9ca2 + 3eaf783 commit a45d527
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 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
17 changes: 13 additions & 4 deletions lib/swagger-module.ts
Original file line number Diff line number Diff line change
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) {
/**
if in Fastify adapter options we pass "ignoreTrailingSlash: true"
the declaration of the route finalPath/ will throw an error because of duplication:
Method '${method}' already declared for route '${path}' with constraints '${JSON.stringify(constraints)}.
To ignore that error, it's wrapped in try-catch
**/
}

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

0 comments on commit a45d527

Please sign in to comment.