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

Commit

Permalink
fix($resource): allow falsy values in URL parameters
Browse files Browse the repository at this point in the history
Close #1212

when a param value was 0 (or false) it was ignored and removed from url.
after this fix that only happens if the value is undefined or null.
  • Loading branch information
Benjamín Eidelman authored and mhevery committed Sep 6, 2012
1 parent 7079ff5 commit 4909d1d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ angular.module('ngResource', ['ng']).

params = params || {};
forEach(this.urlParams, function(_, urlParam){
if (val = (params[urlParam] || self.defaults[urlParam])) {
val = params.hasOwnProperty(urlParam) ? params[urlParam] : self.defaults[urlParam];
if (isDefined(val) && val !== null) {
encodedVal = encodeUriSegment(val);
url = url.replace(new RegExp(":" + urlParam + "(\\W)", "g"), encodedVal + "$1");
} else {
Expand Down
20 changes: 14 additions & 6 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,22 @@ describe("resource", function() {
it('should ignore slashes of undefinend parameters', function() {
var R = $resource('/Path/:a/:b/:c');

$httpBackend.when('GET').respond('{}');
$httpBackend.expect('GET', '/Path');
$httpBackend.expect('GET', '/Path/1');
$httpBackend.expect('GET', '/Path/2/3');
$httpBackend.expect('GET', '/Path/4/5');
$httpBackend.expect('GET', '/Path/6/7/8');
$httpBackend.when('GET', '/Path').respond('{}');
$httpBackend.when('GET', '/Path/0').respond('{}');
$httpBackend.when('GET', '/Path/false').respond('{}');
$httpBackend.when('GET', '/Path').respond('{}');
$httpBackend.when('GET', '/Path/').respond('{}');
$httpBackend.when('GET', '/Path/1').respond('{}');
$httpBackend.when('GET', '/Path/2/3').respond('{}');
$httpBackend.when('GET', '/Path/4/5').respond('{}');
$httpBackend.when('GET', '/Path/6/7/8').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});
Expand Down

0 comments on commit 4909d1d

Please sign in to comment.