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: Duplicate path parameters #829

Merged
merged 7 commits into from
Nov 28, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 66 additions & 0 deletions packages/cli/src/metadataGeneration/metadataGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class MetadataGenerator {
const controllers = this.buildControllers();

this.checkForMethodSignatureDuplicates(controllers);
this.checkForPathParamSignatureDuplicates(controllers);
this.circularDependencyResolvers.forEach(c => c(this.referenceTypeMap));

return {
Expand Down Expand Up @@ -111,6 +112,71 @@ export class MetadataGenerator {
}
};

private checkForPathParamSignatureDuplicates = (controllers: Tsoa.Controller[]) => {
const controllerDup: { [key: string]: any } = {};
let message = '';

controllers.forEach(controller => {
const chunks: { [key: string]: Tsoa.Method[] } = {};
controller.methods.forEach(method => {
if (chunks[method.method] === undefined) {
chunks[method.method] = [];
}
chunks[method.method].push(method);
});

const chunkDupPath: { [key: string]: Tsoa.Method[] } = {};
const regex = new RegExp('^{.*?}/?');
jackey8616 marked this conversation as resolved.
Show resolved Hide resolved
Object.keys(chunks).forEach((key: string) => {
let containFirstPath = false;
const chunk = chunks[key];
const duplicates: Tsoa.Method[] = [];
chunk.forEach(value => {
const currentMatch = regex.test(value.path);
if (currentMatch) {
duplicates.push(value);
}
containFirstPath = containFirstPath || currentMatch;
});

if (duplicates.length > 1) {
chunkDupPath[key] = duplicates;
}
});

if (Object.keys(chunkDupPath).length > 0) {
controllerDup[controller.name] = chunkDupPath;
}
});

if (Object.keys(controllerDup).length > 0) {
message = `Duplicate path parameter definition signature found in controller `;
Object.keys(controllerDup).forEach((conKey, i, iArr) => {
message += `${conKey} at `;
const methodDup = controllerDup[conKey];
Object.keys(methodDup).forEach((methodKey, j, jArr) => {
message += `[method ${methodKey.toUpperCase()} `;
const dup = methodDup[methodKey];
dup.forEach((each: Tsoa.Method, k, kArr) => {
message += `${each.name}`;
if (k !== kArr.length - 1) {
message += ', ';
}
});
message += j !== jArr.length - 1 ? '], ' : ']';
});
if (i !== iArr.length - 1) {
message += ', ';
}
});
jackey8616 marked this conversation as resolved.
Show resolved Hide resolved
message += '\n';
}

if (message) {
throw new GenerateMetadataError(message);
}
};

public TypeChecker() {
return this.typeChecker;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/fixtures/controllers/duplicatePathParamController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Route, Get, Post, Path } from '@tsoa/runtime';

@Route('GetTest')
export class DuplicatePathParamTestController {
@Get('{id}') public getPathParamTest(@Path() id: string) {
return id;
}

@Get('{identifier}') public getPathParamTest2(@Path() identifier: string) {
return identifier;
}

@Post('{id}') public postPathParamTest(@Path() id: string) {
return id;
}

@Post('{identifier}') public postPathParamTest2(@Path() identifier: string) {
return identifier;
}

@Post('{anotherIdentifier}') public postPathParamTest3(@Path() anotherIdentifier: string) {
return anotherIdentifier;
}
}
18 changes: 14 additions & 4 deletions tests/unit/swagger/pathGeneration/routeDuplicates.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { expect } from 'chai';
import { MetadataGenerator } from '@tsoa/cli/metadataGeneration/metadataGenerator';

it('should reject methods with same routes', () => {
expect(() => {
new MetadataGenerator('./fixtures/controllers/duplicateMethodsController.ts').Generate();
}).to.throw(`Duplicate method signature @get(GetTest/Complex) found in controllers: DuplicateMethodsTestController#getModel, DuplicateMethodsTestController#duplicateGetModel\n`);
describe('reject when something duplicate', () => {
it('should reject methods with same routes', () => {
expect(() => {
new MetadataGenerator('./fixtures/controllers/duplicateMethodsController.ts').Generate();
}).to.throw(`Duplicate method signature @get(GetTest/Complex) found in controllers: DuplicateMethodsTestController#getModel, DuplicateMethodsTestController#duplicateGetModel\n`);
});

it('should reject methods with multiple first path parameter', () => {
expect(() => {
new MetadataGenerator('./fixtures/controllers/duplicatePathParamController.ts').Generate();
}).to.throw(
`Duplicate path parameter definition signature found in controller DuplicatePathParamTestController at [method GET getPathParamTest, getPathParamTest2], [method POST postPathParamTest, postPathParamTest2, postPathParamTest3]\n`,
);
});
});