Skip to content

Commit 4c5574b

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 angular#2255, angular#1044
1 parent aef0980 commit 4c5574b

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/ngResource/resource.js

+4
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,10 @@ angular.module('ngResource', ['ng']).
472472
promise = value.$promise;
473473

474474
if (data) {
475+
if ( angular.isArray(data) != !!action.isArray ) {
476+
return $q.reject("Error in resource configuration. Expected response to contain an " +
477+
(action.isArray?'array':'object') + " but got an " + (angular.isArray(data)?'array':'object'));
478+
}
475479
if (action.isArray) {
476480
value.length = 0;
477481
forEach(data, function(item) {

test/ngResource/resourceSpec.js

+21
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,27 @@ describe("resource", function() {
800800
});
801801
});
802802

803+
it('should fail if action expects an object but response is an array', function() {
804+
var successHandler = jasmine.createSpy('successHandler');
805+
var failureHandler = jasmine.createSpy('failureHandler');
806+
$httpBackend.expect('GET', '/Customer/123').respond({id: 'abc'});
807+
$resource('/Customer/123').query().$promise.then(successHandler, failureHandler);
808+
$httpBackend.flush();
809+
expect(successHandler).not.toHaveBeenCalled();
810+
expect(failureHandler).toHaveBeenCalledWith(
811+
'Error in resource configuration. Expected response to contain an array but got an object');
812+
});
813+
814+
it('should fail if action expects an array but response is an object', function() {
815+
var successHandler = jasmine.createSpy('successHandler');
816+
var failureHandler = jasmine.createSpy('failureHandler');
817+
$httpBackend.expect('GET', '/Customer/123').respond([1,2,3]);
818+
$resource('/Customer/123').get().$promise.then(successHandler, failureHandler);
819+
$httpBackend.flush();
820+
expect(successHandler).not.toHaveBeenCalled();
821+
expect(failureHandler).toHaveBeenCalledWith(
822+
'Error in resource configuration. Expected response to contain an object but got an array');
823+
});
803824

804825
it('should transform request/response', function() {
805826
var Person = $resource('/Person/:id', {}, {

0 commit comments

Comments
 (0)