Skip to content

feat(urlMatcherFactory): Added path param type to allow forward slashes in parameters, and not encode them #1738

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
wants to merge 1 commit into from
Closed
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
14 changes: 13 additions & 1 deletion src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,11 @@ UrlMatcher.prototype.format = function (values) {
if (squash === false) {
if (encoded != null) {
if (isArray(encoded)) {
result += map(encoded, encodeDashes).join("-");
if (param.type.name == 'path') {
result += map(encoded, encodeURIComponent).join("/");
} else {
result += map(encoded, encodeDashes).join("-");
}
} else {
result += encodeURIComponent(encoded);
}
Expand Down Expand Up @@ -620,6 +624,14 @@ function $UrlMatcherFactory() {
equals: angular.equals,
pattern: /[^/]*/
},
path: {
encode: function(val) {
return val.split('/');
},
decode: angular.identity,
is: regexpMatches,
pattern: /.*/
},
any: { // does not encode/decode
encode: angular.identity,
decode: angular.identity,
Expand Down
11 changes: 11 additions & 0 deletions test/urlMatcherFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,17 @@ describe("urlMatcherFactory", function () {
expect(m.exec("/state/" + json1 + "/" + json2)).toEqual(params);
});

it("should encode/decode path parameters without encoding the slashes", function () {
var m = new UrlMatcher("/{path:path}");
expect(m.exec("/some/path")).toEqual({ path: 'some/path' });
expect(m.format({ path: 'some/path' })).toBe('/some/path');
});

it("should encode path parameters with encoding of the path parameter components", function () {
var m = new UrlMatcher("/{path:path}");
expect(m.format({ path: 'some /path' })).toBe('/some%20/path');
});

it("should not match invalid typed parameter values", function() {
var m = new UrlMatcher('/users/{id:int}');

Expand Down