From a4230f5581b3f58b6ff531b6dbac377a4fd7942a Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 8 Dec 2025 14:19:20 +0200 Subject: [PATCH] =?UTF-8?q?Revert=20"fix:=20silentJSONParsing=3Dfalse=20sh?= =?UTF-8?q?ould=20throw=20on=20invalid=20JSON=20(#7253)=20(#7=E2=80=A6"=20?= =?UTF-8?q?(#7298)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 7d19335e43d6754a1a9a66e424f7f7da259895bf. --- lib/defaults/index.js | 4 +- test/regression_silent_json.js | 69 ---------------------------------- 2 files changed, 2 insertions(+), 71 deletions(-) delete mode 100644 test/regression_silent_json.js diff --git a/lib/defaults/index.js b/lib/defaults/index.js index b2288ae..cc14a8b 100644 --- a/lib/defaults/index.js +++ b/lib/defaults/index.js @@ -108,7 +108,7 @@ const defaults = { if (data && utils.isString(data) && ((forcedJSONParsing && !this.responseType) || JSONRequested)) { const silentJSONParsing = transitional && transitional.silentJSONParsing; - const strictJSONParsing = !silentJSONParsing; + const strictJSONParsing = !silentJSONParsing && JSONRequested; try { return JSON.parse(data, this.parseReviver); @@ -158,4 +158,4 @@ utils.forEach(['delete', 'get', 'head', 'post', 'put', 'patch'], (method) => { defaults.headers[method] = {}; }); -export default defaults; \ No newline at end of file +export default defaults; diff --git a/test/regression_silent_json.js b/test/regression_silent_json.js deleted file mode 100644 index 37ecb83..0000000 --- a/test/regression_silent_json.js +++ /dev/null @@ -1,69 +0,0 @@ - -import assert from 'assert'; -import axios from '../index.js'; - -describe('Regression: Silent JSON Parsing', function () { - - beforeEach(function () { - // Mock adapter to return invalid JSON - axios.defaults.adapter = async (config) => { - return { - data: '{ invalid json }', - status: 200, - statusText: 'OK', - headers: {}, - config - }; - }; - }); - - afterEach(function () { - delete axios.defaults.adapter; - }); - - it('should throw SyntaxError when silentJSONParsing is false', async function () { - try { - await axios.get('/test', { - transitional: { - silentJSONParsing: false - } - }); - assert.fail('Should have thrown error'); - } catch (err) { - assert.ok(err.name === 'SyntaxError' || err.code === 'ERR_BAD_RESPONSE', 'Error should be SyntaxError or ERR_BAD_RESPONSE'); - } - }); - - it('should return raw string when silentJSONParsing is true (default)', async function () { - const response = await axios.get('/test', { - transitional: { - silentJSONParsing: true - } - }); - assert.strictEqual(response.data, '{ invalid json }'); - }); - - it('should throw SyntaxError when responseType is json (legacy behavior)', async function () { - try { - await axios.get('/test', { - responseType: 'json', - transitional: { - silentJSONParsing: false - } - }); - assert.fail('Should have thrown error'); - } catch (err) { - assert.ok(err.name === 'SyntaxError' || err.code === 'ERR_BAD_RESPONSE', 'Error should be SyntaxError or ERR_BAD_RESPONSE'); - } - }); - - it('should swallow error when silentJSONParsing is true and responseType is json (legacy behavior)', async function () { - const response = await axios.get('/test', { - responseType: 'json', - transitional: { - silentJSONParsing: true - } - }); - assert.strictEqual(response.data, '{ invalid json }'); - }); -});