Skip to content

Commit 412993a

Browse files
committed
fix($http): don't parse templates as JSON
The default behaviour causes issues with a wide variety of custom interpolation markers. Even if the JSON test regexps were strengthened to closer match the standard JSON format, it would still limit the possibilities of different custom interpolation markers. BREAKING CHANGE: $http calls using either the $templateCache, or with custom Accept headers not including `application/json` will no longer parse JSON. Closes angular#5756
1 parent 34fee06 commit 412993a

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/ng/http.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ function $HttpProvider() {
133133
var responseInterceptorFactories = this.responseInterceptors = [];
134134

135135
this.$get = ['$httpBackend', '$browser', '$cacheFactory', '$rootScope', '$q', '$injector',
136-
function($httpBackend, $browser, $cacheFactory, $rootScope, $q, $injector) {
136+
'$templateCache',
137+
function($httpBackend, $browser, $cacheFactory, $rootScope, $q, $injector, $templateCache) {
137138

138139
var defaultCache = $cacheFactory('$http');
139140

@@ -678,8 +679,16 @@ function $HttpProvider() {
678679
transformRequest: defaults.transformRequest,
679680
transformResponse: defaults.transformResponse
680681
};
682+
681683
var headers = mergeHeaders(requestConfig);
682684

685+
if (requestConfig.cache === $templateCache || (typeof headers['Accept'] === 'string' &&
686+
headers['Accept'].indexOf('application/json') < 0)) {
687+
config.transformResponse = [function(data) {
688+
return data;
689+
}];
690+
}
691+
683692
extend(config, requestConfig);
684693
config.headers = headers;
685694
config.method = uppercase(config.method);

test/ng/httpSpec.js

+18
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,24 @@ describe('$http', function() {
10621062
expect(callback).toHaveBeenCalledOnce();
10631063
expect(callback.mostRecentCall.args[0]).toEqual('{{some}}');
10641064
});
1065+
1066+
it('should not deserialize tpl beginning with custom interpolation markers', inject(function($templateCache) {
1067+
$httpBackend.expect('GET', '/url').respond('{[foo.bar.baz]}');
1068+
$http.get('/url', { cache: $templateCache }).success(callback);
1069+
$httpBackend.flush();
1070+
1071+
expect(callback).toHaveBeenCalledOnce();
1072+
expect(callback.mostRecentCall.args[0]).toEqual('{[foo.bar.baz]}');
1073+
1074+
callback.reset();
1075+
1076+
$httpBackend.expect('GET', '/url2').respond('[{foo.bar.baz}]');
1077+
$http.get('/url2', { headers: { Accept: 'text/plain' } }).success(callback);
1078+
$httpBackend.flush();
1079+
1080+
expect(callback).toHaveBeenCalledOnce();
1081+
expect(callback.mostRecentCall.args[0]).toEqual('[{foo.bar.baz}]');
1082+
}));
10651083
});
10661084

10671085

0 commit comments

Comments
 (0)