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

Commit a644ca7

Browse files
fix(resource): check whether response matches action.isArray
When using $resource you must setup your actions carefully based on what the server returns. If the server responds to a request with an array then you must configure the action with `isArray:true` and vice versa. The built-in `get` action defaults to `isArray:false` and the `query` action defaults to `isArray:true`, which is must be changed if the server does not do this. Before the error message was an exception inside angular.copy, which didn't explain what the real problem was. Rather than changing the way that angular.copy works, this change ensures that a better error message is provided to the programmer if they do not set up their resource actions correctly. Closes #2255, #1044
1 parent 24a4450 commit a644ca7

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@ngdoc error
2+
@name ngResource:badcfg
3+
@fullName Response does not match configured parameter
4+
@description

src/ngResource/resource.js

+5
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,11 @@ angular.module('ngResource', ['ng']).
473473
promise = value.$promise;
474474

475475
if (data) {
476+
if ( angular.isArray(data) != !!action.isArray ) {
477+
throw ngResourceMinErr('badcfg', 'Error in resource configuration. Expected response' +
478+
' to contain an {0} but got an {1}',
479+
action.isArray?'array':'object', angular.isArray(data)?'array':'object');
480+
}
476481
if (action.isArray) {
477482
value.length = 0;
478483
forEach(data, function(item) {

test/ngResource/resourceSpec.js

+48-1
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,6 @@ describe("resource", function() {
807807
});
808808
});
809809

810-
811810
it('should transform request/response', function() {
812811
var Person = $resource('/Person/:id', {}, {
813812
save: {
@@ -1034,3 +1033,51 @@ describe("resource", function() {
10341033
});
10351034
});
10361035
});
1036+
1037+
describe('resource', function() {
1038+
var $httpBackend, $resource;
1039+
1040+
beforeEach(module(function($exceptionHandlerProvider) {
1041+
$exceptionHandlerProvider.mode('log');
1042+
}));
1043+
1044+
beforeEach(module('ngResource'));
1045+
1046+
beforeEach(inject(function($injector) {
1047+
$httpBackend = $injector.get('$httpBackend');
1048+
$resource = $injector.get('$resource');
1049+
}));
1050+
1051+
1052+
it('should fail if action expects an object but response is an array', function() {
1053+
var successSpy = jasmine.createSpy('successSpy');
1054+
var failureSpy = jasmine.createSpy('failureSpy');
1055+
1056+
$httpBackend.expect('GET', '/Customer/123').respond({id: 'abc'});
1057+
1058+
$resource('/Customer/123').query()
1059+
.$promise.then(successSpy, function(e) { failureSpy(e.message); });
1060+
$httpBackend.flush();
1061+
1062+
expect(successSpy).not.toHaveBeenCalled();
1063+
expect(failureSpy).toHaveBeenCalledWith(
1064+
'[ngResource:badcfg] Error in resource configuration. Expected response to contain an array but got an object');
1065+
});
1066+
1067+
it('should fail if action expects an array but response is an object', function() {
1068+
var successSpy = jasmine.createSpy('successSpy');
1069+
var failureSpy = jasmine.createSpy('failureSpy');
1070+
1071+
$httpBackend.expect('GET', '/Customer/123').respond([1,2,3]);
1072+
1073+
$resource('/Customer/123').get()
1074+
.$promise.then(successSpy, function(e) { failureSpy(e.message); });
1075+
$httpBackend.flush();
1076+
1077+
expect(successSpy).not.toHaveBeenCalled();
1078+
expect(failureSpy).toHaveBeenCalledWith(
1079+
'[ngResource:badcfg] Error in resource configuration. Expected response to contain an object but got an array');
1080+
});
1081+
1082+
1083+
});

0 commit comments

Comments
 (0)