-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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(#2767): Fix serilization issues of bigint in json body #2773
Conversation
<pre className="line request"> | ||
<span className="arrow">{'>'}</span> data {requestData} | ||
<span className="arrow">{'>'}</span> data <pre>{request.data}</pre> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This causes a horizontal overflow, if the text is too long
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch. Fixed
return this.req.data; | ||
} | ||
|
||
setBody(data) { | ||
if (typeof data === 'object') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably also check if the Object is a "plain" object with data.constructor === Object
(or something else). Setting a FormData object as the body does not work now, but worked previously.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes sense. Will have this updated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I have updated the logic to also check if content type is json (based on headers).
This should cover the use case of people setting body as FormData object.
If someone has set the Content-Type as json in the header (or it gets auto added if they have chosen JSON as body in UI), and then they try to pass a non-plain object while invoking setBody()
- they are essentially shooting themselves in the foot and we don't need to gracefully handle that.
The reason why are supporting the raw
option is to support use cases where people want to still have the content type as json, but want to pass the json strings themselves instead of passing the json object
@Its-treason lmk if you differ or have further suggestions
setBody(data, options = {}) {
if (options.raw) {
this.req.data = data;
this.body = data;
return;
}
const isJson = hasJSONContentType(this.req.headers);
if (isJson && this.__isObject(data)) {
this.body = data;
this.req.data = this.__safeStringifyJSON(data);
return;
}
this.req.data = data;
this.body = data;
}
* @param {boolean} options.raw - If true, return the raw body without parsing it | ||
* @returns | ||
*/ | ||
getBody(options = {}) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about req.body
that is set inside the constructor? I think this should also be JSON parsed like req.getBody
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯 good catch (this could have resulted in breakages for folks who were accessing req.body
in scripts)
Have updated the code with a comment
constructor(req) {
// existing code
/**
* We automatically parse the JSON body if the content type is JSON
* This is to make it easier for the user to access the body directly
*
* It must be noted that the request data is always a string and is what gets sent over the network
* If the user wants to access the raw data, they can use getBody({raw: true}) method
*/
const isJson = hasJSONContentType(this.req.headers);
if (isJson) {
this.body = this.__safeParseJSON(req.data);
}
}
@Its-treason I have addressed all your comments and have also made some updates. Let me know if you have further comments @lohxt1 will take over from here and add further changes on this PR to add a toggle in
|
Another option that I am thinking to solve problems for folks who don’t want us to to automatically parse response as json (where the response loses fidelity in some cases) is to provide an api that can disable this.
They can use this then at request level scripting, or apply it to folder level or collection level scripts as they see fit. We can consider providing a UI toggle much later if there is demand for it. |
* disable response json parse flag * fix: pr review comments
The PR is GTG from my side. Folks who don’t want us to to automatically parse response as json (where the response text loses fidelity in some cases) can use the new api method They can use this at request level scripting, or apply it to folder level or collection level scripts as they see fit. @Its-treason @jwetzell let me know if you have any further comments. I plan to have this merged tomorrow. |
* disable response json parse flag * fix: pr review comments * update bru req function name
hello, fix seems not working in 1.27.0 eg |
Fixes: #2767