Skip to content

Commit

Permalink
[Javascript] Fixing the handling of non primitive types in paramToStr…
Browse files Browse the repository at this point in the history
…ing (#7171)
  • Loading branch information
tray2100 committed Aug 10, 2020
1 parent 6f0bef6 commit 94a0358
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,28 @@
if (param instanceof Date) {
return param.toJSON();
}
if (this.canBeJsonified(param)) {
return JSON.stringify(param);
}
return param.toString();
};

{{#emitJSDoc}} /**
* Returns a boolean indicating if the parameter could be JSON.stringified
* @param param The actual parameter
* @returns {Boolean} Flag indicating if <code>param</code> can be JSON.stringified
*/
{{/emitJSDoc}} exports.prototype.canBeJsonified = function(str) {
if (typeof str !== 'string' && typeof str !== 'object') return false;
try {
const type = str.toString();
return type === '[object Object]'
|| type === '[object Array]';
} catch (err) {
return false;
}
};

{{#emitJSDoc}}
/**
* Builds full URL by appending the given path to the base URL and replacing path parameter place-holders with parameter values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,29 @@ class ApiClient {
if (param instanceof Date) {
return param.toJSON();
}
if (ApiClient.canBeJsonified(param)) {
return JSON.stringify(param);
}

return param.toString();
}

{{#emitJSDoc}}/**
* Returns a boolean indicating if the parameter could be JSON.stringified
* @param param The actual parameter
* @returns {Boolean} Flag indicating if <code>param</code> can be JSON.stringified
*/{{/emitJSDoc}}
static canBeJsonified(str) {
if (typeof str !== 'string' && typeof str !== 'object') return false;
try {
const type = str.toString();
return type === '[object Object]'
|| type === '[object Array]';
} catch (err) {
return false;
}
};

{{#emitJSDoc}}
/**
* Builds full URL by appending the given path to the base URL and replacing path parameter place-holders with parameter values.
Expand Down
19 changes: 19 additions & 0 deletions samples/client/petstore/javascript-es6/src/ApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,29 @@ class ApiClient {
if (param instanceof Date) {
return param.toJSON();
}
if (ApiClient.canBeJsonified(param)) {
return JSON.stringify(param);
}

return param.toString();
}

/**
* Returns a boolean indicating if the parameter could be JSON.stringified
* @param param The actual parameter
* @returns {Boolean} Flag indicating if <code>param</code> can be JSON.stringified
*/
static canBeJsonified(str) {
if (typeof str !== 'string' && typeof str !== 'object') return false;
try {
const type = str.toString();
return type === '[object Object]'
|| type === '[object Array]';
} catch (err) {
return false;
}
};

/**
* Builds full URL by appending the given path to the base URL and replacing path parameter place-holders with parameter values.
* NOTE: query parameters are not handled here.
Expand Down
19 changes: 19 additions & 0 deletions samples/client/petstore/javascript-promise-es6/src/ApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,29 @@ class ApiClient {
if (param instanceof Date) {
return param.toJSON();
}
if (ApiClient.canBeJsonified(param)) {
return JSON.stringify(param);
}

return param.toString();
}

/**
* Returns a boolean indicating if the parameter could be JSON.stringified
* @param param The actual parameter
* @returns {Boolean} Flag indicating if <code>param</code> can be JSON.stringified
*/
static canBeJsonified(str) {
if (typeof str !== 'string' && typeof str !== 'object') return false;
try {
const type = str.toString();
return type === '[object Object]'
|| type === '[object Array]';
} catch (err) {
return false;
}
};

/**
* Builds full URL by appending the given path to the base URL and replacing path parameter place-holders with parameter values.
* NOTE: query parameters are not handled here.
Expand Down

0 comments on commit 94a0358

Please sign in to comment.