-
Notifications
You must be signed in to change notification settings - Fork 402
feat: Impossible to use UUID as param for object construction #706 #708
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
Closed
ivanproskuryakov
wants to merge
8
commits into
typestack:develop
from
ivanproskuryakov:uuid-parameters-703
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b03d7b4
fix: uuid support added with test
ivanproskuryakov 5701639
fix: cs & linter
ivanproskuryakov 1a3618b
fix: adding more test cases according to the feedback provided, cs im…
ivanproskuryakov 9861f71
fix: adding more test cases according to the feedback provided, negat…
ivanproskuryakov 239fb0d
fix: fixing cs with running prettier:fix
ivanproskuryakov c4bb9c8
fix: code review ref: https://github.com/typestack/routing-controller…
ivanproskuryakov 6af1287
Update src/ActionParameterHandler.ts
ivanproskuryakov 039c89f
fix: Update src/ActionParameterHandler.ts
ivanproskuryakov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,156 @@ | ||
import { ActionParameterHandler } from '../src/ActionParameterHandler'; | ||
import { ActionMetadata, ControllerMetadata, ExpressDriver, ParamMetadata } from '../src'; | ||
import { ActionMetadataArgs } from '../src/metadata/args/ActionMetadataArgs'; | ||
import { ControllerMetadataArgs } from '../src/metadata/args/ControllerMetadataArgs'; | ||
import { ParamType } from '../src/metadata/types/ParamType'; | ||
|
||
const expect = require('chakram').expect; | ||
|
||
describe('ActionParameterHandler', () => { | ||
const buildParamMetadata = ( | ||
name: string = 'id', | ||
type: ParamType = 'param', | ||
isRequired: boolean = false | ||
): ParamMetadata => { | ||
const controllerMetadataArgs: ControllerMetadataArgs = { | ||
target: function () {}, | ||
route: '', | ||
type: 'json', | ||
options: {}, | ||
}; | ||
const controllerMetadata = new ControllerMetadata(controllerMetadataArgs); | ||
const args: ActionMetadataArgs = { | ||
route: '', | ||
method: 'getProduct', | ||
options: {}, | ||
target: function () {}, | ||
type: 'get', | ||
appendParams: undefined, | ||
}; | ||
const actionMetadata = new ActionMetadata(controllerMetadata, args, {}); | ||
|
||
return { | ||
type, | ||
name, | ||
targetName: 'product', | ||
isTargetObject: true, | ||
actionMetadata, | ||
target: () => {}, | ||
method: 'getProduct', | ||
object: 'getProduct', | ||
extraOptions: undefined, | ||
index: 0, | ||
parse: undefined, | ||
required: isRequired, | ||
transform: function (action, value) { | ||
return value; | ||
}, | ||
classTransform: undefined, | ||
validate: undefined, | ||
targetType: function () {}, | ||
}; | ||
}; | ||
const driver = new ExpressDriver(); | ||
const actionParameterHandler = new ActionParameterHandler(driver); | ||
|
||
describe('positive', () => { | ||
it('handle - should process string parameters', async () => { | ||
const param = buildParamMetadata('uuid'); | ||
const action = { | ||
request: { | ||
params: { | ||
uuid: '0b5ec98f-e26d-4414-b798-dcd35a5ef859', | ||
}, | ||
}, | ||
response: {}, | ||
}; | ||
|
||
const processedValue = await actionParameterHandler.handle(action, param); | ||
|
||
expect(processedValue).to.be.eq(action.request.params.uuid); | ||
}); | ||
|
||
it('handle - should process string parameters, returns empty if a given string is empty', async () => { | ||
const param = buildParamMetadata('uuid'); | ||
const action = { | ||
request: { | ||
params: { | ||
uuid: '', | ||
}, | ||
}, | ||
response: {}, | ||
}; | ||
|
||
const processedValue = await actionParameterHandler.handle(action, param); | ||
|
||
expect(processedValue).to.be.eq(action.request.params.uuid); | ||
}); | ||
|
||
it('handle - should process number parameters', async () => { | ||
const param = buildParamMetadata('id'); | ||
const action = { | ||
request: { | ||
params: { | ||
id: 10000, | ||
}, | ||
}, | ||
response: {}, | ||
}; | ||
|
||
const processedValue = await actionParameterHandler.handle(action, param); | ||
|
||
expect(processedValue).to.be.eq(action.request.params.id); | ||
}); | ||
|
||
it('handle - undefined on empty object provided', async () => { | ||
const param = buildParamMetadata(); | ||
const action = { | ||
request: { | ||
params: {}, | ||
}, | ||
response: {}, | ||
}; | ||
|
||
const processedValue = await actionParameterHandler.handle(action, param); | ||
|
||
expect(processedValue).to.be.eq(undefined); | ||
}); | ||
}); | ||
describe('negative', () => { | ||
it('handle - throws error if the parameter is required', async () => { | ||
const param = buildParamMetadata('uuid', 'param', true); | ||
const action = { | ||
request: {}, | ||
response: {}, | ||
}; | ||
let error; | ||
|
||
try { | ||
await actionParameterHandler.handle(action, param); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error.toString()).to.be.eq("TypeError: Cannot read property 'uuid' of undefined"); | ||
}); | ||
|
||
it('handle - throws error if the parameter is required, type file provided', async () => { | ||
const param = buildParamMetadata('uuid', 'file', true); | ||
const action = { | ||
request: {}, | ||
response: {}, | ||
}; | ||
let error; | ||
|
||
try { | ||
await actionParameterHandler.handle(action, param); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
expect(error.httpCode).to.be.eq(400); | ||
expect(error.name).to.be.eq('ParamRequiredError'); | ||
expect(error.message).to.be.eq('Uploaded file "uuid" is required for request on undefined undefined'); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.