Skip to content

Commit

Permalink
Release/0.21.4 (#4025)
Browse files Browse the repository at this point in the history
* fix json transform when data is pre-stringified (#4020)

* [Updating] incorrect JSON syntax in README.md

* [Releasing] v0.21.4

Co-authored-by: Guillaume FORTAINE <guillaume+github@fortaine.com>
  • Loading branch information
jasonsaayman and gfortaine authored Sep 6, 2021
1 parent 90205f8 commit 4091b07
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 11 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

### 0.21.4 (September 6, 2021)

Fixes and Functionality:
- Fixing JSON transform when data is stringified. Providing backward compatability and complying to the JSON RFC standard ([#4020](https://github.com/axios/axios/pull/4020))

Huge thanks to everyone who contributed to this release via code (authors listed below) or via reviews and triaging on GitHub:

- [Jay](mailto:jasonsaayman@gmail.com)
- [Guillaume Fortaine](https://github.com/gfortaine)
- [Yusuke Kawasaki](https://github.com/kawanet)
- [Dmitriy Mozgovoy](https://github.com/DigitalBrainJS)

### 0.21.3 (September 4, 2021)

Fixes and Functionality:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ These are the available config options for making requests. Only the `url` is re
silentJSONParsing: true, // default value for the current Axios version

// try to parse the response string as JSON even if `responseType` is not 'json'
forcedJSONParsing: true;
forcedJSONParsing: true,

// throw ETIMEDOUT error instead of generic ECONNABORTED on request timeouts
clarifyTimeoutError: false,
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "axios",
"main": "./dist/axios.js",
"version": "0.21.3",
"version": "0.21.4",
"homepage": "https://axios-http.com",
"authors": [
"Matt Zabriskie"
Expand Down
21 changes: 18 additions & 3 deletions dist/axios.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/axios.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/axios.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/axios.min.map

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ function getDefaultAdapter() {
return adapter;
}

function stringifySafely(rawValue, parser, encoder) {
if (utils.isString(rawValue)) {
try {
(parser || JSON.parse)(rawValue);
return utils.trim(rawValue);
} catch (e) {
if (e.name !== 'SyntaxError') {
throw e;
}
}
}

return (encoder || JSON.stringify)(rawValue);
}

var defaults = {

transitional: {
Expand Down Expand Up @@ -58,7 +73,7 @@ var defaults = {
}
if (utils.isObject(data) || (headers && headers['Content-Type'] === 'application/json')) {
setContentTypeIfUnset(headers, 'application/json');
return JSON.stringify(data);
return stringifySafely(data);
}
return data;
}],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "axios",
"version": "0.21.3",
"version": "0.21.4",
"description": "Promise based HTTP client for the browser and node.js",
"main": "index.js",
"types": "index.d.ts",
Expand Down
13 changes: 13 additions & 0 deletions test/specs/defaults.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ describe('defaults', function () {
expect(defaults.transformRequest[0]({foo: 'bar'})).toEqual('{"foo":"bar"}');
});

it("should also transform request json when 'Content-Type' is 'application/json'", function () {
var headers = {
'Content-Type': 'application/json',
};
expect(defaults.transformRequest[0](JSON.stringify({ foo: 'bar' }), headers)).toEqual('{"foo":"bar"}');
expect(defaults.transformRequest[0]([42, 43], headers)).toEqual('[42,43]');
expect(defaults.transformRequest[0]('foo', headers)).toEqual('"foo"');
expect(defaults.transformRequest[0](42, headers)).toEqual('42');
expect(defaults.transformRequest[0](true, headers)).toEqual('true');
expect(defaults.transformRequest[0](false, headers)).toEqual('false');
expect(defaults.transformRequest[0](null, headers)).toEqual('null');
});

it('should do nothing to request string', function () {
expect(defaults.transformRequest[0]('foo=bar')).toEqual('foo=bar');
});
Expand Down

0 comments on commit 4091b07

Please sign in to comment.