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 4 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
2 changes: 1 addition & 1 deletion scripts/npm/install-dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if diff -q $SHRINKWRAP_FILE $SHRINKWRAP_CACHED_FILE; then
else
echo 'Blowing away node_modules and reinstalling npm dependencies...'
rm -rf node_modules
npm install
npm install --registry http://registry.npmjs.org/
Copy link
Member

Choose a reason for hiding this comment

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

I guess this has to do with your grunt package issues, but it definitely does not belong in this PR :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Woops! sorry :)

cp $SHRINKWRAP_FILE $SHRINKWRAP_CACHED_FILE
echo 'npm install successful!'
fi
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
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 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'});
});


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