Skip to content

Commit 4439e39

Browse files
pavelgjIgorMinar
authored andcommitted
fix(ngResource): correct leading slash removal.
Fixed an issues with ngResource param substitution where it was incorrectly removing leading slash when param was followed by a non-slash character. Ex: '/:foo/:bar.baz/:aux' params = { foo: 'aaa', bar: undefined, aux: undefined } The above params were incorrectly producing '/aaa.baz' but now it results in '/aaa/.baz'.
1 parent b49c3a1 commit 4439e39

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/ngResource/resource.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,14 @@ angular.module('ngResource', ['ng']).
291291
encodedVal = encodeUriSegment(val);
292292
url = url.replace(new RegExp(":" + urlParam + "(\\W)", "g"), encodedVal + "$1");
293293
} else {
294-
url = url.replace(new RegExp("/?:" + urlParam + "(\\W)", "g"), '$1');
294+
url = url.replace(new RegExp("(\/?):" + urlParam + "(\\W)", "g"), function(match,
295+
leadingSlashes, tail) {
296+
if (tail.charAt(0) == '/') {
297+
return tail;
298+
} else {
299+
return leadingSlashes + tail;
300+
}
301+
});
295302
}
296303
});
297304
url = url.replace(/\/?#$/, '');

test/ngResource/resourceSpec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,31 @@ describe("resource", function() {
6666
R.get({a:6, b:7, c:8});
6767
});
6868

69+
it('should not ignore leading slashes of undefinend parameters that have non-slash trailing sequence', function() {
70+
var R = $resource('/Path/:a.foo/:b.bar/:c.baz');
71+
72+
$httpBackend.when('GET', '/Path/.foo/.bar/.baz').respond('{}');
73+
$httpBackend.when('GET', '/Path/0.foo/.bar/.baz').respond('{}');
74+
$httpBackend.when('GET', '/Path/false.foo/.bar/.baz').respond('{}');
75+
$httpBackend.when('GET', '/Path/.foo/.bar/.baz').respond('{}');
76+
$httpBackend.when('GET', '/Path/.foo/.bar/.baz').respond('{}');
77+
$httpBackend.when('GET', '/Path/1.foo/.bar/.baz').respond('{}');
78+
$httpBackend.when('GET', '/Path/2.foo/3.bar/.baz').respond('{}');
79+
$httpBackend.when('GET', '/Path/4.foo/.bar/5.baz').respond('{}');
80+
$httpBackend.when('GET', '/Path/6.foo/7.bar/8.baz').respond('{}');
81+
82+
R.get({});
83+
R.get({a:0});
84+
R.get({a:false});
85+
R.get({a:null});
86+
R.get({a:undefined});
87+
R.get({a:''});
88+
R.get({a:1});
89+
R.get({a:2, b:3});
90+
R.get({a:4, c:5});
91+
R.get({a:6, b:7, c:8});
92+
});
93+
6994

7095
it('should support escaping colons in url template', function() {
7196
var R = $resource('http://localhost\\:8080/Path/:a/\\:stillPath/:b');

0 commit comments

Comments
 (0)