-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #885 from vincentvanderweele/feature/allow-path-pa…
…rameters-on-controllers Feat: allow path parameters on controllers
- Loading branch information
Showing
11 changed files
with
148 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Get, Path, Route } from '@tsoa/runtime'; | ||
|
||
@Route('SubResourceTest/{mainResourceId}/SubResource') | ||
export class SubResourceTestController { | ||
@Get() | ||
public async getSubResource(@Path('mainResourceId') mainResourceId: string): Promise<string> { | ||
return mainResourceId; | ||
} | ||
|
||
@Get('{subResourceId}') | ||
public async getWithParameter(@Path('mainResourceId') mainResourceId: string, @Path('subResourceId') subResourceId: string): Promise<string> { | ||
return `${mainResourceId}:${subResourceId}`; | ||
} | ||
} | ||
|
||
@Route('SubResourceColonTest/:mainResourceId/SubResource') | ||
export class SubResourceColonTestController { | ||
@Get() | ||
public async getSubResource(@Path('mainResourceId') mainResourceId: string): Promise<string> { | ||
return mainResourceId; | ||
} | ||
|
||
@Get('{subResourceId}') | ||
public async getWithParameter(@Path('mainResourceId') mainResourceId: string, @Path('subResourceId') subResourceId: string): Promise<string> { | ||
return `${mainResourceId}:${subResourceId}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
tests/unit/swagger/pathGeneration/subResourceRoutes.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import 'mocha'; | ||
import { expect } from 'chai'; | ||
import { MetadataGenerator } from '@tsoa/cli/metadataGeneration/metadataGenerator'; | ||
import { Tsoa } from '@tsoa/runtime'; | ||
|
||
describe('Sub resource route generation', () => { | ||
const metadata = new MetadataGenerator('./fixtures/controllers/subresourceController.ts').Generate(); | ||
|
||
const variants = [ | ||
{ | ||
name: 'Using brackets', | ||
baseRoute: 'SubResourceTest/{mainResourceId}/SubResource', | ||
}, | ||
{ | ||
name: 'Using colon', | ||
baseRoute: 'SubResourceColonTest/:mainResourceId/SubResource', | ||
}, | ||
]; | ||
|
||
variants.forEach(({ name, baseRoute }) => { | ||
describe(name, () => { | ||
const getParameters = (methodPath: string) => { | ||
const controller = metadata.controllers.find(c => c.path === baseRoute); | ||
if (!controller) { | ||
throw new Error(`Missing controller for ${baseRoute}`); | ||
} | ||
|
||
const method = controller.methods.find(m => m.path === methodPath); | ||
if (!method) { | ||
throw new Error('Unknown method '); | ||
} | ||
|
||
return method.parameters; | ||
}; | ||
|
||
const validatePathParameter = (parameters: Tsoa.Parameter[], name: string) => { | ||
const parameter = parameters.find(p => p.name === name); | ||
expect(parameter, `Path parameter '${name}' wasn't generated.`).to.exist; | ||
expect(parameter!.in).to.equal('path'); | ||
expect(parameter!.type).to.eql({ dataType: 'string' }); | ||
}; | ||
|
||
it('should generate a path parameter for method without path parameter', () => { | ||
const parameters = getParameters(''); | ||
validatePathParameter(parameters, 'mainResourceId'); | ||
}); | ||
|
||
it('should generate two path parameters for method with path parameter', () => { | ||
const parameters = getParameters('{subResourceId}'); | ||
validatePathParameter(parameters, 'mainResourceId'); | ||
validatePathParameter(parameters, 'subResourceId'); | ||
}); | ||
}); | ||
}); | ||
}); |