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

Commit 1c97a60

Browse files
alediaferiagkalpak
authored andcommitted
fix(ngResource): encode & in URL query param values
Closes #12201
1 parent 658a865 commit 1c97a60

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/ngResource/resource.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,9 @@ angular.module('ngResource', ['ng']).
433433
}
434434
if (!(new RegExp("^\\d+$").test(param)) && param &&
435435
(new RegExp("(^|[^\\\\]):" + param + "(\\W|$)").test(url))) {
436-
urlParams[param] = true;
436+
urlParams[param] = {
437+
isQueryParamValue: (new RegExp("\\?.*=:" + param + "(?:\\W|$)")).test(url)
438+
};
437439
}
438440
});
439441
url = url.replace(/\\:/g, ':');
@@ -443,10 +445,14 @@ angular.module('ngResource', ['ng']).
443445
});
444446

445447
params = params || {};
446-
forEach(self.urlParams, function(_, urlParam) {
448+
forEach(self.urlParams, function(paramInfo, urlParam) {
447449
val = params.hasOwnProperty(urlParam) ? params[urlParam] : self.defaults[urlParam];
448450
if (angular.isDefined(val) && val !== null) {
449-
encodedVal = encodeUriSegment(val);
451+
if (paramInfo.isQueryParamValue) {
452+
encodedVal = encodeUriQuery(val, true);
453+
} else {
454+
encodedVal = encodeUriSegment(val);
455+
}
450456
url = url.replace(new RegExp(":" + urlParam + "(\\W|$)", "g"), function(match, p1) {
451457
return encodedVal + p1;
452458
});

test/ngResource/resourceSpec.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,18 @@ describe("resource", function() {
331331
});
332332

333333

334-
it('should encode & in url params', function() {
335-
var R = $resource('/Path/:a');
334+
it('should encode & in query params unless in query param value', function() {
335+
var R1 = $resource('/Path/:a');
336336
$httpBackend.expect('GET', '/Path/doh&foo?bar=baz%261').respond('{}');
337-
R.get({a: 'doh&foo', bar: 'baz&1'});
337+
R1.get({a: 'doh&foo', bar: 'baz&1'});
338+
339+
var R2 = $resource('/api/myapp/resource?:query');
340+
$httpBackend.expect('GET', '/api/myapp/resource?foo&bar').respond('{}');
341+
R2.get({query: 'foo&bar'});
342+
343+
var R3 = $resource('/api/myapp/resource?from=:from');
344+
$httpBackend.expect('GET', '/api/myapp/resource?from=bar%20%26%20blanks').respond('{}');
345+
R3.get({from: 'bar & blanks'});
338346
});
339347

340348

0 commit comments

Comments
 (0)