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

fix(ngResource): fix query url params encoding #12201

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ angular.module('ngResource', ['ng']).
}
if (!(new RegExp("^\\d+$").test(param)) && param &&
(new RegExp("(^|[^\\\\]):" + param + "(\\W|$)").test(url))) {
urlParams[param] = true;
urlParams[param] = { isQueryParamValue: (new RegExp("\\?.*=:" + param + "(?:\\W|$)")).test(url) };
}
});
url = url.replace(/\\:/g, ':');
Expand All @@ -443,10 +443,14 @@ angular.module('ngResource', ['ng']).
});

params = params || {};
forEach(self.urlParams, function(_, urlParam) {
forEach(self.urlParams, function(paramInfo, urlParam) {
val = params.hasOwnProperty(urlParam) ? params[urlParam] : self.defaults[urlParam];
if (angular.isDefined(val) && val !== null) {
encodedVal = encodeUriSegment(val);
if (paramInfo.isQueryParamValue === true) {
encodedVal = encodeUriQuery(val, true);
} else {
encodedVal = encodeUriSegment(val);
}
url = url.replace(new RegExp(":" + urlParam + "(\\W|$)", "g"), function(match, p1) {
return encodedVal + p1;
});
Expand Down
14 changes: 11 additions & 3 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,18 @@ describe("resource", function() {
});


it('should encode & in url params', function() {
var R = $resource('/Path/:a');
it('should encode & in query params unless in query param value', function() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm wondering if part of this test is a duplicate of the one preceding this test.

'should encode & in url params'

Copy link
Member

Choose a reason for hiding this comment

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

I would say close but not quite.
I would be happy to have them merged into one it block (keeping all 3 cases).

Feel free to merge them.

var R1 = $resource('/api/myapp/resource?:query');
$httpBackend.expect('GET', '/api/myapp/resource?foo&bar').respond('{}');
R1.get({query: 'foo&bar'});

var R2 = $resource('/api/myapp/resource?from=:from');
$httpBackend.expect('GET', '/api/myapp/resource?from=bar%20%26%20blanks').respond('{}');
R2.get({from: 'bar & blanks'});

var R3 = $resource('/Path/:a');
$httpBackend.expect('GET', '/Path/doh&foo?bar=baz%261').respond('{}');
R.get({a: 'doh&foo', bar: 'baz&1'});
R3.get({a: 'doh&foo', bar: 'baz&1'});
});


Expand Down