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 3 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) };
Copy link
Member

Choose a reason for hiding this comment

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

Hm...looking at it again, why not change (?:.*) to .* ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense

}
});
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.isQueryParamiValue === true) {
Copy link
Member

Choose a reason for hiding this comment

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

isQueryParamiValue --> isQueryParamValue

(It's a good idea to locally run the tests before pushing, to ensure nothing is (obviously) breaking 😃)

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 sorry for that. Actually I thought I pushed just to my origin and not to upstream as well. Still didn't manage to run the tests. Will do ASAP

Copy link
Member

Choose a reason for hiding this comment

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

NP :)

BTW, when you have submitted a PR, you don't need to push upstream or anything; pushing to your origin/ will auto-update the PR (GitHub feature), so that's probably what happened.

Just ping me when you're done, so I take a final look. Thx for all the work so far !

encodedVal = encodeUriQuery(val, true);
} else {
encodedVal = encodeUriSegment(val);
}
url = url.replace(new RegExp(":" + urlParam + "(\\W|$)", "g"), function(match, p1) {
return encodedVal + p1;
});
Expand Down
11 changes: 11 additions & 0 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,17 @@ describe("resource", function() {
});


it('should handle url encoding in mapped params', function() {
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer a more descriptive testcase name (e.g. should encode & in query params unless in query param value or something).

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'});
});


it('should build resource with default param', function() {
$httpBackend.expect('GET', '/Order/123/Line/456.visa?minimum=0.05').respond({id: 'abc'});
var LineItem = $resource('/Order/:orderId/Line/:id:verb',
Expand Down