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

release: version 9.0.0 #3199

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
84 changes: 20 additions & 64 deletions e2e/api-spec.json
Original file line number Diff line number Diff line change
@@ -1,53 +1,9 @@
{
"openapi": "3.0.0",
"paths": {
"/api/v1": {
"get": {
"operationId": "AppController_getHello_v1",
"parameters": [
{
"name": "x-tenant-id",
"in": "header",
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": ""
}
},
"tags": [
"App"
]
}
},
"/api/v2": {
"get": {
"operationId": "AppController_getHello_v2",
"parameters": [
{
"name": "x-tenant-id",
"in": "header",
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": ""
}
},
"tags": [
"App"
]
}
},
"/api/v1/alias1": {
"/api/v1/express:colon:another/{prop}": {
"get": {
"operationId": "AppController_withAliases[0]_v1",
"operationId": "ExpressController_withColons_v1",
"parameters": [
{
"name": "x-tenant-id",
Expand All @@ -63,13 +19,13 @@
}
},
"tags": [
"App"
"Express"
]
}
},
"/api/v1/alias2": {
"/api/v2/express:colon:another/{prop}": {
"get": {
"operationId": "AppController_withAliases[1]_v1",
"operationId": "ExpressController_withColons_v2",
"parameters": [
{
"name": "x-tenant-id",
Expand All @@ -85,13 +41,13 @@
}
},
"tags": [
"App"
"Express"
]
}
},
"/api/v2/alias1": {
"/api/v1": {
"get": {
"operationId": "AppController_withAliases[2]_v2",
"operationId": "AppController_getHello_v1",
"parameters": [
{
"name": "x-tenant-id",
Expand All @@ -111,9 +67,9 @@
]
}
},
"/api/v2/alias2": {
"/api/v2": {
"get": {
"operationId": "AppController_withAliases[3]_v2",
"operationId": "AppController_getHello_v2",
"parameters": [
{
"name": "x-tenant-id",
Expand All @@ -133,9 +89,9 @@
]
}
},
"/api/v1/express:colon:another/{prop}": {
"/api/v1/alias1": {
"get": {
"operationId": "AppController_withColonExpress_v1",
"operationId": "AppController_withAliases[0]_v1",
"parameters": [
{
"name": "x-tenant-id",
Expand All @@ -155,9 +111,9 @@
]
}
},
"/api/v2/express:colon:another/{prop}": {
"/api/v1/alias2": {
"get": {
"operationId": "AppController_withColonExpress_v2",
"operationId": "AppController_withAliases[1]_v1",
"parameters": [
{
"name": "x-tenant-id",
Expand All @@ -177,9 +133,9 @@
]
}
},
"/api/v1/fastify:{colon}:{another}/{prop}": {
"/api/v2/alias1": {
"get": {
"operationId": "AppController_withColonFastify_v1",
"operationId": "AppController_withAliases[2]_v2",
"parameters": [
{
"name": "x-tenant-id",
Expand All @@ -199,9 +155,9 @@
]
}
},
"/api/v2/fastify:{colon}:{another}/{prop}": {
"/api/v2/alias2": {
"get": {
"operationId": "AppController_withColonFastify_v2",
"operationId": "AppController_withAliases[3]_v2",
"parameters": [
{
"name": "x-tenant-id",
Expand Down Expand Up @@ -992,9 +948,9 @@
]
}
},
"/api/cats/site*": {
"/api/cats/wildcard/{splat}": {
"get": {
"operationId": "CatsController_getSite",
"operationId": "CatsController_getWildcard",
"parameters": [
{
"name": "header",
Expand Down
7 changes: 6 additions & 1 deletion e2e/express.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ import * as request from 'supertest';
import * as SwaggerParser from 'swagger-parser';
import { DocumentBuilder, SwaggerModule } from '../lib';
import { ApplicationModule } from './src/app.module';
import { ExpressController } from './src/express.controller';

describe('Express Swagger', () => {
let app: NestExpressApplication;
let builder: DocumentBuilder;

beforeEach(async () => {
app = await NestFactory.create<NestExpressApplication>(
ApplicationModule,
{
module: class {},
imports: [ApplicationModule],
controllers: [ExpressController]
},
new ExpressAdapter(),
{ logger: false }
);
Expand Down
7 changes: 6 additions & 1 deletion e2e/fastify.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ import * as request from 'supertest';
import * as SwaggerParser from 'swagger-parser';
import { DocumentBuilder, SwaggerModule } from '../lib';
import { ApplicationModule } from './src/app.module';
import { FastifyController } from './src/fastify.controller';

describe('Fastify Swagger', () => {
let app: NestFastifyApplication;
let builder: DocumentBuilder;

beforeEach(async () => {
app = await NestFactory.create<NestFastifyApplication>(
ApplicationModule,
{
module: class {},
imports: [ApplicationModule],
controllers: [FastifyController]
},
new FastifyAdapter(),
{ logger: false }
);
Expand Down
10 changes: 0 additions & 10 deletions e2e/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,4 @@ 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!';
}
}
4 changes: 2 additions & 2 deletions e2e/src/cats/cats.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ export class CatsController {
return this.catsService.create(createCatDto);
}

@Get('site*')
getSite() {}
@Get('wildcard/*')
getWildcard() {}

@Get('with-enum/:type')
@ApiParam({
Expand Down
11 changes: 11 additions & 0 deletions e2e/src/express.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Controller, Get } from '@nestjs/common';

@Controller({
version: ['1', '2']
})
export class ExpressController {
@Get('express\\:colon\\:another/:prop')
withColons(): string {
return 'Hello world!';
}
}
11 changes: 11 additions & 0 deletions e2e/src/fastify.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Controller, Get } from '@nestjs/common';

@Controller({
version: ['1', '2']
})
export class FastifyController {
@Get('fastify::colon::another/:prop')
withColons(): string {
return 'Hello world!';
}
}
14 changes: 11 additions & 3 deletions e2e/validate-schema.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,23 @@ import {
import { ApplicationModule } from './src/app.module';
import { Cat } from './src/cats/classes/cat.class';
import { TagDto } from './src/cats/dto/tag.dto';
import { ExpressController } from './src/express.controller';

describe('Validate OpenAPI schema', () => {
let app: INestApplication;
let options: Omit<OpenAPIObject, 'paths'>;

beforeEach(async () => {
app = await NestFactory.create(ApplicationModule, {
logger: false
});
app = await NestFactory.create(
{
module: class {},
imports: [ApplicationModule],
controllers: [ExpressController]
},
{
logger: false
}
);
app.setGlobalPrefix('api/');
app.enableVersioning();

Expand Down
46 changes: 38 additions & 8 deletions lib/swagger-explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
} from '@nestjs/common/interfaces';
import {
addLeadingSlash,
isString,
isUndefined
} from '@nestjs/common/utils/shared.utils';
import { ApplicationConfig, MetadataScanner } from '@nestjs/core';
Expand All @@ -31,7 +30,7 @@ import {
omitBy,
pick
} from 'lodash';
import * as pathToRegexp from 'path-to-regexp';
import { parse } from 'path-to-regexp';
import { DECORATORS } from './constants';
import { exploreApiCallbacksMetadata } from './explorers/api-callbacks.explorer';
import { exploreApiExcludeControllerMetadata } from './explorers/api-exclude-controller.explorer';
Expand Down Expand Up @@ -353,12 +352,19 @@ export class SwaggerExplorer {
);
if (requestMethod === RequestMethod.ALL) {
// Workaround for the invalid "ALL" Method
const validMethods = Object.values(RequestMethod).filter(
(meth) => meth !== 'ALL' && typeof meth === 'string'
) as string[];
const validMethods = [
'get',
'post',
'put',
'delete',
'patch',
'options',
'head',
'search'
];

return validMethods.map((requestMethod) => ({
method: requestMethod.toLowerCase(),
method: requestMethod,
path: fullPath === '' ? '/' : fullPath,
operationId: `${this.getOperationId(
instance,
Expand Down Expand Up @@ -430,8 +436,32 @@ export class SwaggerExplorer {
path = head(path);
}
let pathWithParams = '';
for (const item of pathToRegexp.parse(path)) {
pathWithParams += isString(item) ? item : `${item.prefix}{${item.name}}`;

try {
let normalizedPath = path.endsWith('*')
? path.replace(/\*$/, '*splat')
: path;

normalizedPath = normalizedPath.replace(/::/g, '\\:');
normalizedPath = normalizedPath.replace(/\[:\]/g, '\\:');

const { tokens } = parse(normalizedPath);
for (const item of tokens) {
if (item.type === 'text') {
pathWithParams += item.value;
} else if (item.type === 'param') {
pathWithParams += `{${item.name}}`;
} else if (item.type === 'wildcard') {
pathWithParams += `{splat}`;
}
}
} catch (err) {
// TODO: add error logging
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO

console.error(err);
console.log('faiel to parse path', {
path
});
return '';
}
return pathWithParams === '/' ? '' : addLeadingSlash(pathWithParams);
}
Expand Down
3 changes: 1 addition & 2 deletions lib/swagger-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class SwaggerScanner {
});
}
const modulePath = this.getModulePathMetadata(container, metatype);
result = result.concat(
return result.concat(
this.scanModuleControllers(
controllers,
modulePath,
Expand All @@ -96,7 +96,6 @@ export class SwaggerScanner {
autoTagControllers
)
);
return this.transformer.unescapeColonsInPath(app, result);
}
);

Expand Down
Loading