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

Commit f0904cf

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 81b7e5a commit f0904cf

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

src/ngResource/resource.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -522,23 +522,32 @@ angular.module('ngResource', ['ng']).
522522
extend({}, extractParams(data, action.params || {}), params),
523523
action.url);
524524

525-
var promise = $http(httpConfig).then(function(response) {
525+
var promise = $http(httpConfig).then(function (response) {
526526
var data = response.data,
527-
promise = value.$promise;
527+
promise = value.$promise;
528528

529529
if (data) {
530530
// Need to convert action.isArray to boolean in case it is undefined
531531
// jshint -W018
532532
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');
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');
536538
}
537539
// jshint +W018
538540
if (action.isArray) {
539541
value.length = 0;
540-
forEach(data, function(item) {
541-
value.push(new Resource(item));
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+
}
542551
});
543552
} else {
544553
shallowClearAndCopy(data, value);

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)