Skip to content
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(): unescape colons in paths #1634

Merged
merged 2 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions e2e/api-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,50 @@
}
}
},
"/api/v1/express:colon:another/{prop}": {
"get": {
"operationId": "AppController_withColonExpress",
"parameters": [],
"responses": {
"200": {
"description": ""
}
}
}
},
"/api/v2/express:colon:another/{prop}": {
"get": {
"operationId": "AppController_withColonExpress",
"parameters": [],
"responses": {
"200": {
"description": ""
}
}
}
},
"/api/v1/fastify:{colon}:{another}/{prop}": {
"get": {
"operationId": "AppController_withColonFastify",
"parameters": [],
"responses": {
"200": {
"description": ""
}
}
}
},
"/api/v2/fastify:{colon}:{another}/{prop}": {
"get": {
"operationId": "AppController_withColonFastify",
"parameters": [],
"responses": {
"200": {
"description": ""
}
}
}
},
"/api/cats": {
"post": {
"operationId": "CatsController_create",
Expand Down
5 changes: 5 additions & 0 deletions e2e/fastify.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ describe('Fastify Swagger', () => {
}
});

it('should fix colons in url', async () => {
const document = SwaggerModule.createDocument(app, builder.build());
expect(document.paths['/fastify:colon:another/{prop}']).toBeDefined();
});

it('should pass uiConfig options to fastify-swagger', async () => {
const document1 = SwaggerModule.createDocument(app, builder.build());
const uiConfig = {
Expand Down
10 changes: 10 additions & 0 deletions e2e/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,14 @@ export class AppController {
withAliases(): string {
return 'Hello world!';
}

@Get('express[:]colon[:]another/:prop')
withColonExpress(): string {
return 'Hello world!';
}

@Get('fastify::colon::another/:prop')
withColonFastify(): string {
return 'Hello world!';
}
}
5 changes: 5 additions & 0 deletions e2e/validate-schema.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ describe('Validate OpenAPI schema', () => {
}
});

it('should fix colons in url', async () => {
const document = SwaggerModule.createDocument(app, options);
expect(document.paths['/api/v1/express:colon:another/{prop}']).toBeDefined();
});

it('should merge custom components passed via config', async () => {
const components = {
schemas: {
Expand Down
4 changes: 4 additions & 0 deletions lib/interfaces/module-route.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { OpenAPIObject } from '.';

export type ModuleRoute = Omit<OpenAPIObject, 'openapi' | 'info'> &
Record<'root', any>;
12 changes: 7 additions & 5 deletions lib/swagger-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
import { InstanceToken, Module } from '@nestjs/core/injector/module';
import { flatten, isEmpty } from 'lodash';
import { OpenAPIObject, SwaggerDocumentOptions } from './interfaces';
import { ModuleRoute } from './interfaces/module-route.interface';
import {
ReferenceObject,
SchemaObject
Expand All @@ -19,7 +20,7 @@ import { getGlobalPrefix } from './utils/get-global-prefix';
import { stripLastSlash } from './utils/strip-last-slash.util';

export class SwaggerScanner {
private readonly transfomer = new SwaggerTransformer();
private readonly transformer = new SwaggerTransformer();
private readonly schemaObjectFactory = new SchemaObjectFactory(
new ModelPropertiesAccessor(),
new SwaggerTypesMapper()
Expand Down Expand Up @@ -51,7 +52,7 @@ export class SwaggerScanner {

const denormalizedPaths = modules.map(
({ routes, metatype, relatedModules }) => {
let result = [];
let result: ModuleRoute[] = [];

if (deepScanRoutes) {
// only load submodules routes if asked
Expand All @@ -77,7 +78,7 @@ export class SwaggerScanner {
});
}
const modulePath = this.getModulePathMetadata(container, metatype);
return result.concat(
result = result.concat(
this.scanModuleRoutes(
routes,
modulePath,
Expand All @@ -86,14 +87,15 @@ export class SwaggerScanner {
operationIdFactory
)
);
return this.transformer.unescapeColonsInPath(app, result);
}
);

const schemas = this.explorer.getSchemas();
this.addExtraModels(schemas, extraModels);

return {
...this.transfomer.normalizePaths(flatten(denormalizedPaths)),
...this.transformer.normalizePaths(flatten(denormalizedPaths)),
components: {
schemas: schemas as Record<string, SchemaObject | ReferenceObject>
}
Expand All @@ -106,7 +108,7 @@ export class SwaggerScanner {
globalPrefix: string | undefined,
applicationConfig: ApplicationConfig,
operationIdFactory?: (controllerKey: string, methodKey: string) => string
): Array<Omit<OpenAPIObject, 'openapi' | 'info'> & Record<'root', any>> {
): ModuleRoute[] {
const denormalizedArray = [...routes.values()].map((ctrl) =>
this.explorer.exploreController(
ctrl,
Expand Down
21 changes: 21 additions & 0 deletions lib/swagger-transformer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { INestApplication } from '@nestjs/common';
import { filter, groupBy, keyBy, mapValues, omit } from 'lodash';
import { OpenAPIObject } from './interfaces';
import { ModuleRoute } from './interfaces/module-route.interface';

export class SwaggerTransformer {
public normalizePaths(
Expand All @@ -26,4 +28,23 @@ export class SwaggerTransformer {
paths
};
}

public unescapeColonsInPath(
app: INestApplication,
moduleRoutes: ModuleRoute[]
): ModuleRoute[] {
const httpAdapter = app.getHttpAdapter();
const usingFastify = httpAdapter && httpAdapter.getType() === 'fastify';
const unescapeColon = usingFastify
? (path: string) => path.replace(/:\{([^}]+)\}/g, ':$1')
: (path: string) => path.replace(/\[:\]/g, ':');

return moduleRoutes.map((moduleRoute) => ({
...moduleRoute,
root: {
...moduleRoute.root,
path: unescapeColon(moduleRoute.root.path)
}
}));
}
}