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

fix(ngResource): don't convert literal values into Resource objects when isArray is true #7741

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,14 @@ angular.module('ngResource', ['ng']).
if (action.isArray) {
value.length = 0;
forEach(data, function (item) {
value.push(new Resource(item));
if (typeof item === "object") {
value.push(new Resource(item));
} else {
// Valid JSON values may be string literals, and these should not be converted
// into objects. These items will not have access to the Resource prototype
// methods, but unfortunately there
value.push(item);
}
});
} else {
shallowClearAndCopy(data, value);
Expand Down
21 changes: 21 additions & 0 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,27 @@ describe("resource", function() {
$httpBackend.flush();
expect(user).toEqualData([ {id: 1, name: 'user1'} ]);
});

it('should not convert string literals in array into Resource objects', function() {
$httpBackend.expect('GET', '/names.json').respond(["mary", "jane"]);
var strings = $resource('/names.json').query();
$httpBackend.flush();
expect(strings).toEqualData(["mary", "jane"]);
});

it('should not convert number literals in array into Resource objects', function() {
$httpBackend.expect('GET', '/names.json').respond([213, 456]);
var numbers = $resource('/names.json').query();
$httpBackend.flush();
expect(numbers).toEqualData([213, 456]);
});

it('should not convert boolean literals in array into Resource objects', function() {
$httpBackend.expect('GET', '/names.json').respond([true, false]);
var bools = $resource('/names.json').query();
$httpBackend.flush();
expect(bools).toEqualData([true, false]);
});
});

describe('get', function(){
Expand Down