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

Commit 16dfcb6

Browse files
caitprodyhaddad
authored andcommitted
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 #6314 Closes #7741
1 parent 65a44dd commit 16dfcb6

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/ngResource/resource.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,14 @@ angular.module('ngResource', ['ng']).
580580
if (action.isArray) {
581581
value.length = 0;
582582
forEach(data, function (item) {
583-
value.push(new Resource(item));
583+
if (typeof item === "object") {
584+
value.push(new Resource(item));
585+
} else {
586+
// Valid JSON values may be string literals, and these should not be converted
587+
// into objects. These items will not have access to the Resource prototype
588+
// methods, but unfortunately there
589+
value.push(item);
590+
}
584591
});
585592
} else {
586593
shallowClearAndCopy(data, value);

test/ngResource/resourceSpec.js

+21
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,27 @@ describe("resource", function() {
11131113
$httpBackend.flush();
11141114
expect(user).toEqualData([ {id: 1, name: 'user1'} ]);
11151115
});
1116+
1117+
it('should not convert string literals in array into Resource objects', function() {
1118+
$httpBackend.expect('GET', '/names.json').respond(["mary", "jane"]);
1119+
var strings = $resource('/names.json').query();
1120+
$httpBackend.flush();
1121+
expect(strings).toEqualData(["mary", "jane"]);
1122+
});
1123+
1124+
it('should not convert number literals in array into Resource objects', function() {
1125+
$httpBackend.expect('GET', '/names.json').respond([213, 456]);
1126+
var numbers = $resource('/names.json').query();
1127+
$httpBackend.flush();
1128+
expect(numbers).toEqualData([213, 456]);
1129+
});
1130+
1131+
it('should not convert boolean literals in array into Resource objects', function() {
1132+
$httpBackend.expect('GET', '/names.json').respond([true, false]);
1133+
var bools = $resource('/names.json').query();
1134+
$httpBackend.flush();
1135+
expect(bools).toEqualData([true, false]);
1136+
});
11161137
});
11171138

11181139
describe('get', function(){

0 commit comments

Comments
 (0)