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

Commit acb545e

Browse files
committed
fix($resource): pass all extra, owned properties as params
Previously, a property would not be passed as query param, if `Object.prototype` had a property with the same name. Fixes #14866 Closes #14867 BREAKING CHANGE: All owned properties of the `params` object that are not used to replace URL params, will be passed to `$http` as `config.params` (to be used as query parameters in the URL), even if `Object.prototype` has a property with same name. E.g.: Before: ```js var Foo = $resource('/foo/:id'); Foo.get({id: 42, bar: 'baz', toString: 'hmm'}); // URL: /foo/42?bar=baz // Note that `toString` is _not_ included in the query, // because `Object.prototype.toString` is defined :( ``` After: ```js var Foo = $resource('/foo/:id'); Foo.get({id: 42, bar: 'baz', toString: 'hmm'}); // URL: /foo/42?bar=baz&toString=hmm // Note that `toString` _is_ included in the query, as expected :) ```
1 parent c4da2f0 commit acb545e

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/ngResource/resource.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ angular.module('ngResource', ['ng']).
535535
encodedVal,
536536
protocolAndDomain = '';
537537

538-
var urlParams = self.urlParams = {};
538+
var urlParams = self.urlParams = Object.create(null);
539539
forEach(url.split(/\W/), function(param) {
540540
if (param === 'hasOwnProperty') {
541541
throw $resourceMinErr('badname', "hasOwnProperty is not a valid parameter name.");

test/ngResource/resourceSpec.js

+43
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,49 @@ describe("basic usage", function() {
13631363
});
13641364
});
13651365

1366+
describe('extra params', function() {
1367+
var $http;
1368+
var $httpBackend;
1369+
var $resource;
1370+
1371+
beforeEach(module('ngResource'));
1372+
1373+
beforeEach(module(function($provide) {
1374+
$provide.decorator('$http', function($delegate) {
1375+
return jasmine.createSpy('$http').and.callFake($delegate);
1376+
});
1377+
}));
1378+
1379+
beforeEach(inject(function(_$http_, _$httpBackend_, _$resource_) {
1380+
$http = _$http_;
1381+
$httpBackend = _$httpBackend_;
1382+
$resource = _$resource_;
1383+
}));
1384+
1385+
afterEach(function() {
1386+
$httpBackend.verifyNoOutstandingExpectation();
1387+
});
1388+
1389+
1390+
it('should pass extra params to `$http` as `config.params`', function() {
1391+
$httpBackend.expectGET('/bar?baz=qux').respond('{}');
1392+
1393+
var R = $resource('/:foo');
1394+
R.get({foo: 'bar', baz: 'qux'});
1395+
1396+
expect($http).toHaveBeenCalledWith(jasmine.objectContaining({params: {baz: 'qux'}}));
1397+
});
1398+
1399+
it('should pass extra params even if `Object.prototype` has properties with the same name',
1400+
function() {
1401+
$httpBackend.expectGET('/foo?toString=bar').respond('{}');
1402+
1403+
var R = $resource('/foo');
1404+
R.get({toString: 'bar'});
1405+
}
1406+
);
1407+
});
1408+
13661409
describe('errors', function() {
13671410
var $httpBackend, $resource, $q;
13681411

0 commit comments

Comments
 (0)