diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js index 827886a3cbd9..3ea7e7eec910 100644 --- a/src/ngResource/resource.js +++ b/src/ngResource/resource.js @@ -472,6 +472,10 @@ angular.module('ngResource', ['ng']). promise = value.$promise; if (data) { + if ( angular.isArray(data) != !!action.isArray ) { + return $q.reject("Error in resource configuration. Expected response to contain an " + + (action.isArray?'array':'object') + " but got an " + (angular.isArray(data)?'array':'object')); + } if (action.isArray) { value.length = 0; forEach(data, function(item) { diff --git a/test/ngResource/resourceSpec.js b/test/ngResource/resourceSpec.js index 6a709fb7bf08..6227b0819a79 100644 --- a/test/ngResource/resourceSpec.js +++ b/test/ngResource/resourceSpec.js @@ -800,6 +800,27 @@ describe("resource", function() { }); }); + it('should fail if action expects an object but response is an array', function() { + var successHandler = jasmine.createSpy('successHandler'); + var failureHandler = jasmine.createSpy('failureHandler'); + $httpBackend.expect('GET', '/Customer/123').respond({id: 'abc'}); + $resource('/Customer/123').query().$promise.then(successHandler, failureHandler); + $httpBackend.flush(); + expect(successHandler).not.toHaveBeenCalled(); + expect(failureHandler).toHaveBeenCalledWith( + 'Error in resource configuration. Expected response to contain an array but got an object'); + }); + + it('should fail if action expects an array but response is an object', function() { + var successHandler = jasmine.createSpy('successHandler'); + var failureHandler = jasmine.createSpy('failureHandler'); + $httpBackend.expect('GET', '/Customer/123').respond([1,2,3]); + $resource('/Customer/123').get().$promise.then(successHandler, failureHandler); + $httpBackend.flush(); + expect(successHandler).not.toHaveBeenCalled(); + expect(failureHandler).toHaveBeenCalledWith( + 'Error in resource configuration. Expected response to contain an object but got an array'); + }); it('should transform request/response', function() { var Person = $resource('/Person/:id', {}, {