Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit b2f4625

Browse files
pavelgjmhevery
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 a26234f commit b2f4625

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/ngResource/resource.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,14 @@ angular.module('ngResource', ['ng']).
311311
encodedVal = encodeUriSegment(val);
312312
url = url.replace(new RegExp(":" + urlParam + "(\\W)", "g"), encodedVal + "$1");
313313
} else {
314-
url = url.replace(new RegExp("/?:" + urlParam + "(\\W)", "g"), '$1');
314+
url = url.replace(new RegExp("(\/?):" + urlParam + "(\\W)", "g"), function(match,
315+
leadingSlashes, tail) {
316+
if (tail.charAt(0) == '/') {
317+
return tail;
318+
} else {
319+
return leadingSlashes + tail;
320+
}
321+
});
315322
}
316323
});
317324
url = url.replace(/\/?#$/, '');

test/ngResource/resourceSpec.js

+25
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,31 @@ describe("resource", function() {
7373
R.get({a:6, b:7, c:8});
7474
});
7575

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

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

0 commit comments

Comments
 (0)