[Javascript] Fixing the handling of non primitive types in paramToString (#7171) (#7172)

This commit is contained in:
Troy P
2020-08-10 01:58:55 -07:00
committed by GitHub
parent 256d498d0e
commit 780b55a518
4 changed files with 76 additions and 0 deletions

View File

@@ -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.