Skip to content
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

Merged
merged 2 commits into from
Mar 22, 2019
Merged

[typescript-axios] Fix JSON serialization of falsy request bodies #2446

merged 2 commits into from
Mar 22, 2019

Conversation

hectorj
Copy link
Contributor

@hectorj hectorj commented Mar 19, 2019

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./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\.
  • Filed the PR against the correct branch: master, 3.4.x, 4.0.x. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming language.

Description of the PR

Fix #2445

@auto-labeler auto-labeler bot added Client: TypeScript WIP Work in Progress labels Mar 19, 2019
@auto-labeler
Copy link

auto-labeler bot commented Mar 19, 2019

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@hectorj
Copy link
Contributor Author

hectorj commented Mar 19, 2019

(WIP because I need to do update the Petstore sample) Edit: done

@hectorj hectorj changed the title WIP: [typescript-axios] Fix JSON serialization of falsy request bodies [typescript-axios] Fix JSON serialization of falsy request bodies Mar 19, 2019
@@ -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}} || "");
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

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 🤔

Copy link
Member

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.

Copy link
Member

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

@macjohnny
Copy link
Member

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)

Copy link
Member

@macjohnny macjohnny left a 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}} || "");
Copy link
Member

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.

Copy link
Member

@macjohnny macjohnny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@hectorj
Copy link
Contributor Author

hectorj commented Mar 20, 2019

Not sure how to get the bot to remove the WIP label

@macjohnny
Copy link
Member

@wing328 can you help remove the WIP label?

@wing328 wing328 added Issue: Bug and removed WIP Work in Progress labels Mar 22, 2019
@wing328 wing328 merged commit d839af5 into OpenAPITools:master Mar 22, 2019
@wing328 wing328 added this to the 4.0.0 milestone Mar 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants