From 94a03585690db343e58151ffd97c2948b055d587 Mon Sep 17 00:00:00 2001 From: Troy Patterson Date: Sun, 9 Aug 2020 16:47:34 -0700 Subject: [PATCH] [Javascript] Fixing the handling of non primitive types in paramToString (#7171) --- .../resources/Javascript/ApiClient.mustache | 19 +++++++++++++++++++ .../Javascript/es6/ApiClient.mustache | 19 +++++++++++++++++++ .../petstore/javascript-es6/src/ApiClient.js | 19 +++++++++++++++++++ .../javascript-promise-es6/src/ApiClient.js | 19 +++++++++++++++++++ 4 files changed, 76 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/Javascript/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Javascript/ApiClient.mustache index d618e612b33c..b402e62d1a5f 100644 --- a/modules/openapi-generator/src/main/resources/Javascript/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Javascript/ApiClient.mustache @@ -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 param 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. diff --git a/modules/openapi-generator/src/main/resources/Javascript/es6/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Javascript/es6/ApiClient.mustache index c660001f1fdc..fcb92377e19c 100644 --- a/modules/openapi-generator/src/main/resources/Javascript/es6/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Javascript/es6/ApiClient.mustache @@ -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 param 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. diff --git a/samples/client/petstore/javascript-es6/src/ApiClient.js b/samples/client/petstore/javascript-es6/src/ApiClient.js index 3d081bf1877a..6fba8796f928 100644 --- a/samples/client/petstore/javascript-es6/src/ApiClient.js +++ b/samples/client/petstore/javascript-es6/src/ApiClient.js @@ -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 param 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. diff --git a/samples/client/petstore/javascript-promise-es6/src/ApiClient.js b/samples/client/petstore/javascript-promise-es6/src/ApiClient.js index d3eb5cb7184f..1b38bc4b28eb 100644 --- a/samples/client/petstore/javascript-promise-es6/src/ApiClient.js +++ b/samples/client/petstore/javascript-promise-es6/src/ApiClient.js @@ -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 param 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.