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

Commit

Permalink
fix(ngResource): correct leading slash removal.
Browse files Browse the repository at this point in the history
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'.
  • Loading branch information
pavelgj authored and mhevery committed Jan 19, 2013
1 parent a26234f commit b2f4625
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,14 @@ angular.module('ngResource', ['ng']).
encodedVal = encodeUriSegment(val);
url = url.replace(new RegExp(":" + urlParam + "(\\W)", "g"), encodedVal + "$1");
} else {
url = url.replace(new RegExp("/?:" + urlParam + "(\\W)", "g"), '$1');
url = url.replace(new RegExp("(\/?):" + urlParam + "(\\W)", "g"), function(match,
leadingSlashes, tail) {
if (tail.charAt(0) == '/') {
return tail;
} else {
return leadingSlashes + tail;
}
});
}
});
url = url.replace(/\/?#$/, '');
Expand Down
25 changes: 25 additions & 0 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,31 @@ describe("resource", function() {
R.get({a:6, b:7, c:8});
});

it('should not ignore leading slashes of undefinend parameters that have non-slash trailing sequence', function() {
var R = $resource('/Path/:a.foo/:b.bar/:c.baz');

$httpBackend.when('GET', '/Path/.foo/.bar/.baz').respond('{}');
$httpBackend.when('GET', '/Path/0.foo/.bar/.baz').respond('{}');
$httpBackend.when('GET', '/Path/false.foo/.bar/.baz').respond('{}');
$httpBackend.when('GET', '/Path/.foo/.bar/.baz').respond('{}');
$httpBackend.when('GET', '/Path/.foo/.bar/.baz').respond('{}');
$httpBackend.when('GET', '/Path/1.foo/.bar/.baz').respond('{}');
$httpBackend.when('GET', '/Path/2.foo/3.bar/.baz').respond('{}');
$httpBackend.when('GET', '/Path/4.foo/.bar/5.baz').respond('{}');
$httpBackend.when('GET', '/Path/6.foo/7.bar/8.baz').respond('{}');

R.get({});
R.get({a:0});
R.get({a:false});
R.get({a:null});
R.get({a:undefined});
R.get({a:''});
R.get({a:1});
R.get({a:2, b:3});
R.get({a:4, c:5});
R.get({a:6, b:7, c:8});
});


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

1 comment on commit b2f4625

@wallin
Copy link

@wallin wallin commented on b2f4625 Feb 20, 2013

Choose a reason for hiding this comment

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

Hi,

Prior to the 1.0.4 release, I was relying on this "bug" in order to specify format for my requests (Rails style). Eg.

$resource('/products/:id.json')

But since 1.0.4 it stops working, since the requested URL now becomes "/products/.json" when no id is given. Is there any way to solve this angular-wise? I know that I can remove it and rely on the Accept-header on the request, but anyway?

Please sign in to comment.