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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

{{/bodyParam}}

return {
Expand Down Expand Up @@ -302,4 +302,4 @@ export class {{classname}} extends BaseAPI {

{{/operation}}
}
{{/operations}}
{{/operations}}
14 changes: 7 additions & 7 deletions samples/client/petstore/typescript-axios/builds/default/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"Pet" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -534,7 +534,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"Pet" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -1127,7 +1127,7 @@ export const StoreApiAxiosParamCreator = function (configuration?: Configuration
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"Order" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -1342,7 +1342,7 @@ export const UserApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"User" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -1379,7 +1379,7 @@ export const UserApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"Array&lt;User&gt;" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -1416,7 +1416,7 @@ export const UserApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"Array&lt;User&gt;" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -1601,7 +1601,7 @@ export const UserApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"User" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"Pet" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -534,7 +534,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"Pet" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -1127,7 +1127,7 @@ export const StoreApiAxiosParamCreator = function (configuration?: Configuration
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"Order" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -1342,7 +1342,7 @@ export const UserApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"User" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -1379,7 +1379,7 @@ export const UserApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"Array&lt;User&gt;" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -1416,7 +1416,7 @@ export const UserApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"Array&lt;User&gt;" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -1601,7 +1601,7 @@ export const UserApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"User" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"Pet" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -534,7 +534,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"Pet" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -1220,7 +1220,7 @@ export const StoreApiAxiosParamCreator = function (configuration?: Configuration
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"Order" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -1482,7 +1482,7 @@ export const UserApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"User" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -1519,7 +1519,7 @@ export const UserApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"Array&lt;User&gt;" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -1556,7 +1556,7 @@ export const UserApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"Array&lt;User&gt;" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -1741,7 +1741,7 @@ export const UserApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"User" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"Pet" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -293,7 +293,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"Pet" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export const StoreApiAxiosParamCreator = function (configuration?: Configuration
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"Order" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const UserApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"User" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -92,7 +92,7 @@ export const UserApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"Array&lt;User&gt;" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -129,7 +129,7 @@ export const UserApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"Array&lt;User&gt;" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down Expand Up @@ -314,7 +314,7 @@ export const UserApiAxiosParamCreator = function (configuration?: Configuration)
delete localVarUrlObj.search;
localVarRequestOptions.headers = {...localVarHeaderParameter, ...options.headers};
const needsSerialization = (<any>"User" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body || {}) : (body || "");
localVarRequestOptions.data = needsSerialization ? JSON.stringify(body !== undefined ? body : {}) : (body || "");

return {
url: url.format(localVarUrlObj),
Expand Down
Loading