-
-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix json transform when data is pre-stringified #4020
fix json transform when data is pre-stringified #4020
Conversation
e19af1f
to
1ec6f38
Compare
It looks like I'm a little late with my fix :) |
LGTM |
32e9c6d
to
fea1383
Compare
stringifySafely LGTM, so it looked like relevant to include it in this fix as well 👍 |
0.21.1 if (utils.isObject(data)) {
setContentTypeIfUnset(headers, 'application/json;charset=utf-8');
return JSON.stringify(data);
}
return data; commit f6ae669 if (utils.isObject(data) || (headers && headers['Content-Type'] === 'application/json')) {
setContentTypeIfUnset(headers, 'application/json');
return stringifySafely(data);
}
return data; Why not keep a better backward compatibility: var isJSON = (headers && headers['Content-Type'] === 'application/json');
if (utils.isObject(data) || (isJSON && !utils.isString(data))) {
setContentTypeIfUnset(headers, 'application/json');
return stringifySafely(data);
}
return data; |
@kawanet How do you handle #2613 (comment) with strings ? |
@slaout looks just want to support a data which is a naked number. README.md doesn't mention JavaScript string nor number serialized.
I rarely think there are any use-cases to send a naked string to be serialized (wrapped) as a JSON by axios's default behavior. |
@kawanet, I agree that the readme, may not contain this behaviour however people could easily be lead to believe that we handle JSON as per the RFC. I therefore think the best course of action is to keep backward compatibility at the best levels while including compatibility with the RFC. @gfortaine, can we include all the cases from #2613 and as below so that we don't break anything in the future:
|
f6ae669
to
5f45be2
Compare
@DigitalBrainJS is this inline with #4021, can I merge this and close your pull request? |
8f688bf
to
ff8e76e
Compare
@jasonsaayman all the cases => done (see tests) ✅ |
Thank you for updating axios! I welcome 0.21.4 anyway!🍻 Behavior Summary: (1) sending a valid JSON axios.post(url, {"foo": "bar"}, options) (2) sending a valid JSON axios.post(url, '{"foo": "bar"}', options) (3) sending a valid JSON axios.post(url, 123, options) (4) sending a JSON wrapped string axios.post(url, "some text", options) (5) sending a JSON wrapped string axios.post(url, '{"foo":"bar"', options) // broken JSON missing trailing `}` (7) sending a axios.post(url, Buffer.from('{"foo": "bar"}'), options) |
Ok great, @gfortaine I will need to make some updates since the pull is coming from master and there are breaking changes already merged to master so I will have to change it to |
ff8e76e
to
82b24e1
Compare
@jasonsaayman rebased to be able to merge it safely in release/0.21.4 |
82b24e1
to
9807d1e
Compare
when are we planning for 0.21.4 release? |
@gfortaine thanks looks great to me 👍 I will release tonight |
@jasonsaayman Definitely yes. But it would be nice if someone could fix the syntax issue of the config object in the Readme as my PR contains these changes. |
@DigitalBrainJS I will include that in the release :) |
* 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>
…be passed directly as payload for encoding to JSON #2613, #61, #907 (#3688) * Draft * Added support for primitive types to be converted to JSON if the request Content-Type is 'application/json'; Added throwing SyntaxError if JSON parsing failed and responseType is json; Added transitional option object; Added options validator to assert transitional options; Added transitional option `silentJSONParsing= true` for backward compatibility; Updated README.md; Updated typings; * Fixed isOlderVersion helper; Fixed typo; Added validator.spec.js; * Added forcedJSONParsing transitional option #2791 * `transformData` is now called in the default configuration context if the function context is not specified (for tests compatibility); * Added `transitional.clarifyTimeoutError` to throw ETIMEDOUT error instead of generic ECONNABORTED on request timeouts; Added support of onloadend handler if available instead of onreadystatechange; Added xhr timeout test; Fixed potential bug of xhr adapter with proper handling timeouts&errors (FakeXMLHTTPRequest failed to handle timeouts);
This breaks a lot of stuff upstream, escaping is done too aggressively. |
@alexandrughinea Why? Is using the right ContentType for requests so aggressive? Is there any reason to set ContentType to JSON to actually send non JSON data? |
@DigitalBrainJS That't not the problem. Please better highlight breaking changes in release notes. 👍 |
It was not a problem that the commit 5ad6994 had a breaking change. |
We will mend this, I am working harder now to determine what will bump what in the versioning. Next release will be a 0.22.0 and then I would like to cut a version 1. However there is some larger questions that really need to be answered before that v1 is cut to have a solid vision and steering for axios. That being said I strive to be more vigilant! |
Care to elaborate @alexandrughinea? |
* fix json transform when data is pre-stringified (axios#4020) * [Updating] incorrect JSON syntax in README.md * [Releasing] v0.21.4 Co-authored-by: Guillaume FORTAINE <guillaume+github@fortaine.com>
Just spent my Sunday afternoon on this nasty bug introduced by PR #3688 (to fix #2613). It should be OK for people :
using interceptors with JSON data. Indeed, the fact is that when we retry the request with the request interceptor, because the data is already stringified the second time config.data has been changed in response interceptor #1386, we fall into this bug (the payload is stringified 2 times instead of only one) cc @ddolcimascolo @rdsedmundo : Error response interceptor receives JSON stringified payload on error.config.data #3986
using pre-stringified data cc @kawanet : 0.21.2 has a breaking change with pre-stringified JSON request body #4018
cc @jasonsaayman @chinesedfan @DigitalBrainJS