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(urlMatcherFactory): fix configuring a parameter type by name in a… #2296

Merged
merged 1 commit into from
Oct 9, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 11 additions & 2 deletions src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) });

Copy link
Contributor Author

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.

Copy link
Contributor

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.

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
};
Expand Down Expand Up @@ -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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
Expand Down
31 changes: 31 additions & 0 deletions test/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' } } },
Copy link
Contributor Author

Choose a reason for hiding this comment

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

non-optional non-url param of type int

ISS2101 = { params: { bar: { squash: false, value: 'qux'}}, url: '/2101/{bar:string}' };
AppInjectable = {};

Expand All @@ -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)
Expand Down Expand Up @@ -989,6 +991,7 @@ describe('state', function () {
'OPT.OPT2',
'RS',
'RSP',
'URLLESS',
'about',
'about.person',
'about.person.item',
Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 21 additions & 0 deletions test/urlMatcherFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,27 @@ describe("urlMatcherFactory", function () {
expect(m.format({ foo: 5, flag: true })).toBe("/5/1");
});

it("should match types named only in params", function () {
var m = new UrlMatcher("/{foo}/{flag}", {
params: {
foo: { type: 'int'},
flag: { type: 'bool'}
}
});
expect(m.exec("/1138/1")).toEqual({foo: 1138, flag: true});
expect(m.format({foo: 5, flag: true})).toBe("/5/1");
});

it("should throw an error if a param type is declared twice", function () {
expect(function() {
new UrlMatcher("/{foo:int}", {
params: {
foo: {type: 'int'}
}
});
}).toThrow("Param 'foo' has two type configurations.");
});

it("should encode/decode dates", function () {
var m = new UrlMatcher("/calendar/{date:date}"),
result = m.exec("/calendar/2014-03-26");
Expand Down