-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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
[typescript-axios] Fix JSON serialization of falsy request bodies #2446
Conversation
👍 Thanks for opening this issue! The team will review the labels and make any necessary changes. |
|
@@ -175,7 +175,7 @@ export const {{classname}}AxiosParamCreator = function (configuration?: Configur | |||
{{/hasFormParams}} | |||
{{#bodyParam}} | |||
const needsSerialization = (<any>"{{dataType}}" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json'; | |||
localVarRequestOptions.data = needsSerialization ? JSON.stringify({{paramName}} || {}) : ({{paramName}} || ""); | |||
localVarRequestOptions.data = needsSerialization ? JSON.stringify({{paramName}} !== undefined ? {{paramName}} : {}) : ({{paramName}} || ""); |
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.
is it correct to send an empty object when the body is undefined?
what if body is null?
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.
null
is a valid JSON value
For {}
when it's undefined
, I have no idea so I kept the current behavior
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.
But you're right, an undefined
body param should either throw an error or give an empty body 🤔
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.
I think it is ok to treat undefined
similar to null
and send an empty 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.
the more I think about it, the more I consider the const needsSerialization = (<any>"{{dataType}}" !== "string")
part the origin of the problem, since it does not check for any other type like boolean, etc.
so the undefined
check is a good way to send whatever is passed as body as long as it is defined
cc @TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @nicokoenig (2018/09) @topce (2018/10) |
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.
LGTM
@@ -175,7 +175,7 @@ export const {{classname}}AxiosParamCreator = function (configuration?: Configur | |||
{{/hasFormParams}} | |||
{{#bodyParam}} | |||
const needsSerialization = (<any>"{{dataType}}" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json'; | |||
localVarRequestOptions.data = needsSerialization ? JSON.stringify({{paramName}} || {}) : ({{paramName}} || ""); | |||
localVarRequestOptions.data = needsSerialization ? JSON.stringify({{paramName}} !== undefined ? {{paramName}} : {}) : ({{paramName}} || ""); |
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.
I think it is ok to treat undefined
similar to null
and send an empty 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.
LGTM
Not sure how to get the bot to remove the WIP label |
@wing328 can you help remove the WIP label? |
PR checklist
./bin/
to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.sh
,./bin/openapi3/{LANG}-petstore.sh
if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in.\bin\windows\
.master
,. Default:3.4.x
,4.0.x
master
.Description of the PR
Fix #2445