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

Commit 4748652

Browse files
gkalpakpetebacondarwin
authored andcommitted
fix($resource): don't allow using promises as timeout and log a warning
Promises never worked correctly as values for `timeout` in `$resource`, because the same value has to be re-used for multiple requests (and it is not possible to `angular.copy()` a promise). Now (in addition to ignoring a non-numeric `timeout`), a warning is logged to the console using `$log.debug()`. Partly fixes #13393. BREAKING CHANGE: Possible breaking change for users who updated their code to provide a `timeout` promise for a `$resource` request in version 1.4.8. Up to v1.4.7 (included), using a promise as a timeout in `$resource`, would silently fail (i.e. have no effect). In v1.4.8, using a promise as timeout would have the (buggy) behaviour described in #12657 (comment) (i.e. it will work as expected for the first time you resolve the promise and will cancel all subsequent requests after that - one has to re-create the resource class. This is feature was not documented.) With this change, using a promise as timeout in 1.4.9 onwsards is not allowed. It will log a warning and ignore the timeout value. If you need support for cancellable `$resource` actions, you should upgrade to version 1.5 or higher.
1 parent 0292e6a commit 4748652

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

Diff for: src/ngResource/resource.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ angular.module('ngResource', ['ng']).
368368
}
369369
};
370370

371-
this.$get = ['$http', '$q', function($http, $q) {
371+
this.$get = ['$http', '$log', '$q', function($http, $log, $q) {
372372

373373
var noop = angular.noop,
374374
forEach = angular.forEach,
@@ -579,6 +579,16 @@ angular.module('ngResource', ['ng']).
579579
case 'isArray':
580580
case 'interceptor':
581581
break;
582+
case 'timeout':
583+
if (value && !angular.isNumber(value)) {
584+
$log.debug('ngResource:\n' +
585+
' Only numeric values are allowed as `timeout`.\n' +
586+
' Promises are not supported in $resource, because the same value would ' +
587+
'be used for multiple requests.\n' +
588+
' If you need support for cancellable $resource actions, you should ' +
589+
'upgrade to version 1.5 or higher.');
590+
}
591+
break;
582592
}
583593
});
584594

Diff for: test/ngResource/resourceSpec.js

+42
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,48 @@ describe('resource', function() {
13551355
/^\[\$resource:badcfg\] Error in resource configuration for action `get`\. Expected response to contain an object but got an array \(Request: GET \/Customer\/123\)/
13561356
);
13571357
});
1358+
});
1359+
1360+
describe('resource with promises as `timeout`', function() {
1361+
var httpSpy;
1362+
var $httpBackend;
1363+
var $resource;
1364+
1365+
beforeEach(module('ngResource', function($provide) {
1366+
$provide.decorator('$http', function($delegate) {
1367+
httpSpy = jasmine.createSpy('$http').andCallFake($delegate);
1368+
return httpSpy;
1369+
});
1370+
}));
1371+
1372+
beforeEach(inject(function(_$httpBackend_, _$resource_) {
1373+
$httpBackend = _$httpBackend_;
1374+
$resource = _$resource_;
1375+
}));
13581376

1377+
it('should ignore non-numeric timeouts in actions and log a $debug message',
1378+
inject(function($log, $q) {
1379+
spyOn($log, 'debug');
1380+
$httpBackend.whenGET('/CreditCard').respond({});
1381+
1382+
var CreditCard = $resource('/CreditCard', {}, {
1383+
get: {
1384+
method: 'GET',
1385+
timeout: $q.defer().promise
1386+
}
1387+
});
1388+
1389+
CreditCard.get();
1390+
$httpBackend.flush();
13591391

1392+
expect(httpSpy).toHaveBeenCalledOnce();
1393+
expect(httpSpy.calls[0].args[0].timeout).toBeUndefined();
1394+
expect($log.debug).toHaveBeenCalledOnceWith('ngResource:\n' +
1395+
' Only numeric values are allowed as `timeout`.\n' +
1396+
' Promises are not supported in $resource, because the same value would ' +
1397+
'be used for multiple requests.\n' +
1398+
' If you need support for cancellable $resource actions, you should ' +
1399+
'upgrade to version 1.5 or higher.');
1400+
})
1401+
);
13601402
});

0 commit comments

Comments
 (0)