Skip to content

Commit 7d19335

Browse files
RudrxxxRudransh Guptajasonsaayman
authored
fix: silentJSONParsing=false should throw on invalid JSON (#7253) (#7257)
Co-authored-by: Rudransh Gupta <rudranshgupta@Rudranshs-MacBook-Pro.local> Co-authored-by: Jay <jasonsaayman@gmail.com>
1 parent ec9d94e commit 7d19335

2 files changed

Lines changed: 71 additions & 2 deletions

File tree

‎lib/defaults/index.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ const defaults = {
108108

109109
if (data && utils.isString(data) && ((forcedJSONParsing && !this.responseType) || JSONRequested)) {
110110
const silentJSONParsing = transitional && transitional.silentJSONParsing;
111-
const strictJSONParsing = !silentJSONParsing && JSONRequested;
111+
const strictJSONParsing = !silentJSONParsing;
112112

113113
try {
114114
return JSON.parse(data, this.parseReviver);
@@ -158,4 +158,4 @@ utils.forEach(['delete', 'get', 'head', 'post', 'put', 'patch'], (method) => {
158158
defaults.headers[method] = {};
159159
});
160160

161-
export default defaults;
161+
export default defaults;

‎test/regression_silent_json.js‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
import assert from 'assert';
3+
import axios from '../index.js';
4+
5+
describe('Regression: Silent JSON Parsing', function () {
6+
7+
beforeEach(function () {
8+
// Mock adapter to return invalid JSON
9+
axios.defaults.adapter = async (config) => {
10+
return {
11+
data: '{ invalid json }',
12+
status: 200,
13+
statusText: 'OK',
14+
headers: {},
15+
config
16+
};
17+
};
18+
});
19+
20+
afterEach(function () {
21+
delete axios.defaults.adapter;
22+
});
23+
24+
it('should throw SyntaxError when silentJSONParsing is false', async function () {
25+
try {
26+
await axios.get('/test', {
27+
transitional: {
28+
silentJSONParsing: false
29+
}
30+
});
31+
assert.fail('Should have thrown error');
32+
} catch (err) {
33+
assert.ok(err.name === 'SyntaxError' || err.code === 'ERR_BAD_RESPONSE', 'Error should be SyntaxError or ERR_BAD_RESPONSE');
34+
}
35+
});
36+
37+
it('should return raw string when silentJSONParsing is true (default)', async function () {
38+
const response = await axios.get('/test', {
39+
transitional: {
40+
silentJSONParsing: true
41+
}
42+
});
43+
assert.strictEqual(response.data, '{ invalid json }');
44+
});
45+
46+
it('should throw SyntaxError when responseType is json (legacy behavior)', async function () {
47+
try {
48+
await axios.get('/test', {
49+
responseType: 'json',
50+
transitional: {
51+
silentJSONParsing: false
52+
}
53+
});
54+
assert.fail('Should have thrown error');
55+
} catch (err) {
56+
assert.ok(err.name === 'SyntaxError' || err.code === 'ERR_BAD_RESPONSE', 'Error should be SyntaxError or ERR_BAD_RESPONSE');
57+
}
58+
});
59+
60+
it('should swallow error when silentJSONParsing is true and responseType is json (legacy behavior)', async function () {
61+
const response = await axios.get('/test', {
62+
responseType: 'json',
63+
transitional: {
64+
silentJSONParsing: true
65+
}
66+
});
67+
assert.strictEqual(response.data, '{ invalid json }');
68+
});
69+
});

0 commit comments

Comments
 (0)