Skip to content

Commit 94a0358

Browse files
committed
[Javascript] Fixing the handling of non primitive types in paramToString (#7171)
1 parent 6f0bef6 commit 94a0358

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

modules/openapi-generator/src/main/resources/Javascript/ApiClient.mustache

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,28 @@
119119
if (param instanceof Date) {
120120
return param.toJSON();
121121
}
122+
if (this.canBeJsonified(param)) {
123+
return JSON.stringify(param);
124+
}
122125
return param.toString();
123126
};
124127

128+
{{#emitJSDoc}} /**
129+
* Returns a boolean indicating if the parameter could be JSON.stringified
130+
* @param param The actual parameter
131+
* @returns {Boolean} Flag indicating if <code>param</code> can be JSON.stringified
132+
*/
133+
{{/emitJSDoc}} exports.prototype.canBeJsonified = function(str) {
134+
if (typeof str !== 'string' && typeof str !== 'object') return false;
135+
try {
136+
const type = str.toString();
137+
return type === '[object Object]'
138+
|| type === '[object Array]';
139+
} catch (err) {
140+
return false;
141+
}
142+
};
143+
125144
{{#emitJSDoc}}
126145
/**
127146
* Builds full URL by appending the given path to the base URL and replacing path parameter place-holders with parameter values.

modules/openapi-generator/src/main/resources/Javascript/es6/ApiClient.mustache

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,29 @@ class ApiClient {
108108
if (param instanceof Date) {
109109
return param.toJSON();
110110
}
111+
if (ApiClient.canBeJsonified(param)) {
112+
return JSON.stringify(param);
113+
}
111114

112115
return param.toString();
113116
}
114117

118+
{{#emitJSDoc}}/**
119+
* Returns a boolean indicating if the parameter could be JSON.stringified
120+
* @param param The actual parameter
121+
* @returns {Boolean} Flag indicating if <code>param</code> can be JSON.stringified
122+
*/{{/emitJSDoc}}
123+
static canBeJsonified(str) {
124+
if (typeof str !== 'string' && typeof str !== 'object') return false;
125+
try {
126+
const type = str.toString();
127+
return type === '[object Object]'
128+
|| type === '[object Array]';
129+
} catch (err) {
130+
return false;
131+
}
132+
};
133+
115134
{{#emitJSDoc}}
116135
/**
117136
* Builds full URL by appending the given path to the base URL and replacing path parameter place-holders with parameter values.

samples/client/petstore/javascript-es6/src/ApiClient.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,29 @@ class ApiClient {
109109
if (param instanceof Date) {
110110
return param.toJSON();
111111
}
112+
if (ApiClient.canBeJsonified(param)) {
113+
return JSON.stringify(param);
114+
}
112115

113116
return param.toString();
114117
}
115118

119+
/**
120+
* Returns a boolean indicating if the parameter could be JSON.stringified
121+
* @param param The actual parameter
122+
* @returns {Boolean} Flag indicating if <code>param</code> can be JSON.stringified
123+
*/
124+
static canBeJsonified(str) {
125+
if (typeof str !== 'string' && typeof str !== 'object') return false;
126+
try {
127+
const type = str.toString();
128+
return type === '[object Object]'
129+
|| type === '[object Array]';
130+
} catch (err) {
131+
return false;
132+
}
133+
};
134+
116135
/**
117136
* Builds full URL by appending the given path to the base URL and replacing path parameter place-holders with parameter values.
118137
* NOTE: query parameters are not handled here.

samples/client/petstore/javascript-promise-es6/src/ApiClient.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,29 @@ class ApiClient {
109109
if (param instanceof Date) {
110110
return param.toJSON();
111111
}
112+
if (ApiClient.canBeJsonified(param)) {
113+
return JSON.stringify(param);
114+
}
112115

113116
return param.toString();
114117
}
115118

119+
/**
120+
* Returns a boolean indicating if the parameter could be JSON.stringified
121+
* @param param The actual parameter
122+
* @returns {Boolean} Flag indicating if <code>param</code> can be JSON.stringified
123+
*/
124+
static canBeJsonified(str) {
125+
if (typeof str !== 'string' && typeof str !== 'object') return false;
126+
try {
127+
const type = str.toString();
128+
return type === '[object Object]'
129+
|| type === '[object Array]';
130+
} catch (err) {
131+
return false;
132+
}
133+
};
134+
116135
/**
117136
* Builds full URL by appending the given path to the base URL and replacing path parameter place-holders with parameter values.
118137
* NOTE: query parameters are not handled here.

0 commit comments

Comments
 (0)