Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1cae1d1

Browse files
caitprodyhaddad
authored andcommittedJun 13, 2014
fix(ngResource): don't convert literal values into Resource objects when isArray is true
Previously non-object literals would be thrown out of Resource responses with isArray===true, or otherwise converted into Objects (in the case of string literals). The reason for this is because shallowClearAndCopy iterates over keys, and copies keys into the destination. Iterating over String keys results in integer keys, with a single-character value. Not converting non-objects to Resources means that you lose the ability to perform Resource operations on them. However, they become usable as strings, numbers, or booleans, which is important. In the future, it would be useful to make these useful as Resources while still retaining their primitive value usefulness. Closes angular#6314 Closes angular#7741
1 parent 81b7e5a commit 1cae1d1

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed
 

‎src/ngResource/resource.js

+28-7
Original file line numberDiff line numberDiff line change
@@ -526,13 +526,34 @@ angular.module('ngResource', ['ng']).
526526
var data = response.data,
527527
promise = value.$promise;
528528

529-
if (data) {
530-
// Need to convert action.isArray to boolean in case it is undefined
531-
// jshint -W018
532-
if (angular.isArray(data) !== (!!action.isArray)) {
533-
throw $resourceMinErr('badcfg', 'Error in resource configuration. Expected ' +
534-
'response to contain an {0} but got an {1}',
535-
action.isArray?'array':'object', angular.isArray(data)?'array':'object');
529+
if (data) {
530+
// Need to convert action.isArray to boolean in case it is undefined
531+
// jshint -W018
532+
if (angular.isArray(data) !== (!!action.isArray)) {
533+
throw $resourceMinErr('badcfg',
534+
'Error in resource configuration. Expected ' +
535+
'response to contain an {0} but got an {1}',
536+
action.isArray ? 'array' : 'object',
537+
angular.isArray(data) ? 'array' : 'object');
538+
}
539+
// jshint +W018
540+
if (action.isArray) {
541+
value.length = 0;
542+
forEach(data, function (item) {
543+
if (typeof item === "object") {
544+
value.push(new Resource(item));
545+
} else {
546+
// Valid JSON values may be string literals, and these should not be converted
547+
// into objects. These items will not have access to the Resource prototype
548+
// methods, but unfortunately there
549+
value.push(item);
550+
}
551+
});
552+
} else {
553+
shallowClearAndCopy(data, value);
554+
value.$promise = promise;
555+
}
556+
536557
}
537558
// jshint +W018
538559
if (action.isArray) {

‎test/ngResource/resourceSpec.js

+21
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,27 @@ describe("resource", function() {
10441044
$httpBackend.flush();
10451045
expect(user).toEqualData([ {id: 1, name: 'user1'} ]);
10461046
});
1047+
1048+
it('should not convert string literals in array into Resource objects', function() {
1049+
$httpBackend.expect('GET', '/names.json').respond(["mary", "jane"]);
1050+
var strings = $resource('/names.json').query();
1051+
$httpBackend.flush();
1052+
expect(strings).toEqualData(["mary", "jane"]);
1053+
});
1054+
1055+
it('should not convert number literals in array into Resource objects', function() {
1056+
$httpBackend.expect('GET', '/names.json').respond([213, 456]);
1057+
var numbers = $resource('/names.json').query();
1058+
$httpBackend.flush();
1059+
expect(numbers).toEqualData([213, 456]);
1060+
});
1061+
1062+
it('should not convert boolean literals in array into Resource objects', function() {
1063+
$httpBackend.expect('GET', '/names.json').respond([true, false]);
1064+
var bools = $resource('/names.json').query();
1065+
$httpBackend.flush();
1066+
expect(bools).toEqualData([true, false]);
1067+
});
10471068
});
10481069

10491070
describe('get', function(){

0 commit comments

Comments
 (0)
Please sign in to comment.