-
Notifications
You must be signed in to change notification settings - Fork 3k
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(urlMatcherFactory): fix configuring a parameter type by name in a… #2296
Merged
Merged
Changes from all commits
Commits
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 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 |
---|---|---|
|
@@ -122,7 +122,11 @@ function UrlMatcher(pattern, config, parentMatcher) { | |
cfg = config.params[id]; | ||
segment = pattern.substring(last, m.index); | ||
regexp = isSearch ? m[4] : m[4] || (m[1] == '*' ? '.*' : null); | ||
type = $$UMFP.type(regexp || "string") || inherit($$UMFP.type("string"), { pattern: new RegExp(regexp, config.caseInsensitive ? 'i' : undefined) }); | ||
|
||
if (regexp) { | ||
type = $$UMFP.type(regexp) || inherit($$UMFP.type("string"), { pattern: new RegExp(regexp, config.caseInsensitive ? 'i' : undefined) }); | ||
} | ||
|
||
return { | ||
id: id, regexp: regexp, segment: segment, type: type, cfg: cfg | ||
}; | ||
|
@@ -920,7 +924,12 @@ function $UrlMatcherFactory() { | |
if (config.type && urlType) throw new Error("Param '"+id+"' has two type configurations."); | ||
if (urlType) return urlType; | ||
if (!config.type) return (location === "config" ? $types.any : $types.string); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Heres where we were applying default type 'string' to url params (Actually, to non-non-url params ;) ) |
||
return config.type instanceof Type ? config.type : new Type(config.type); | ||
|
||
if (angular.isString(config.type)) | ||
return $types[config.type]; | ||
if (config.type instanceof Type) | ||
return config.type; | ||
return new Type(config.type); | ||
} | ||
|
||
// array config: param name (param[]) overrides default settings. explicit config overrides param name. | ||
|
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 |
---|---|---|
|
@@ -32,6 +32,7 @@ describe('state', function () { | |
RSP = { url: '^/:doReload/search?term', reloadOnSearch: false }, | ||
OPT = { url: '/opt/:param', params: { param: "100" } }, | ||
OPT2 = { url: '/opt2/:param2/:param3', params: { param3: "300", param4: "400" } }, | ||
URLLESS = { url: '/urllessparams', params: { myparam: { type: 'int' } } }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. non-optional non-url param of type |
||
ISS2101 = { params: { bar: { squash: false, value: 'qux'}}, url: '/2101/{bar:string}' }; | ||
AppInjectable = {}; | ||
|
||
|
@@ -56,6 +57,7 @@ describe('state', function () { | |
.state('HHH', HHH) | ||
.state('OPT', OPT) | ||
.state('OPT.OPT2', OPT2) | ||
.state('URLLESS', URLLESS) | ||
.state('ISS2101', ISS2101) | ||
.state('RS', RS) | ||
.state('RSP', RSP) | ||
|
@@ -989,6 +991,7 @@ describe('state', function () { | |
'OPT.OPT2', | ||
'RS', | ||
'RSP', | ||
'URLLESS', | ||
'about', | ||
'about.person', | ||
'about.person.item', | ||
|
@@ -1255,6 +1258,34 @@ describe('state', function () { | |
extend(params, { p5: true }); | ||
check('types.substate', "/types/foo/2014-11-15/sub/10/%7B%22baz%22:%22qux%22%7D?p5=1", params, defaults, nonurl); | ||
})); | ||
|
||
it('should support non-url parameters', inject(function($state, $q, $stateParams) { | ||
$state.transitionTo(A); $q.flush(); | ||
expect($state.is(A)).toBe(true); | ||
|
||
$state.go('URLLESS', { myparam: "0"}); $q.flush(); // string "0" decodes to 0 | ||
expect($state.current.name).toBe("URLLESS"); | ||
expect($stateParams.myparam).toBe(0); | ||
|
||
$state.go('URLLESS', { myparam: "1"}); $q.flush(); // string "1" decodes to 1 | ||
expect($stateParams.myparam).toBe(1); | ||
})); | ||
|
||
it('should not transition if a required non-url parameter is missing', inject(function($state, $q, $stateParams) { | ||
$state.transitionTo(A); $q.flush(); | ||
expect($state.current.name).toBe("A"); | ||
|
||
$state.go('URLLESS'); $q.flush(); // Missing required parameter; transition fails | ||
expect($state.current.name).toBe("A"); | ||
})); | ||
|
||
it('should not transition if a required non-url parameter is invalid', inject(function($state, $q, $stateParams) { | ||
$state.transitionTo(A); $q.flush(); | ||
expect($state.current.name).toBe("A"); | ||
|
||
$state.go('URLLESS', { myparam: "somestring"}); $q.flush(); // string "somestring" is not an int | ||
expect($state.current.name).toBe("A"); | ||
})); | ||
}); | ||
|
||
it('should revert to last known working url on state change failure', inject(function ($state, $rootScope, $location, $q) { | ||
|
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
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.
defer applying the default path/query param type of "string" until the
getType
function.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.
Yes this was the bit that was setting things as type string for me. I didn't fully understand all the code so I tried to confine my changes to
getType
as to limit impact.