-
Notifications
You must be signed in to change notification settings - Fork 27.4k
fix(ngResource): fix query url params encoding #12201
Changes from 3 commits
d248a07
f671ec0
175f549
87b8cc7
1ac0e7e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, ':'); | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 😃) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -338,6 +338,17 @@ describe("resource", function() { | |
}); | ||
|
||
|
||
it('should handle url encoding in mapped params', function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer a more descriptive testcase name (e.g. |
||
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', | ||
|
There was a problem hiding this comment.
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.*
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense