Skip to content

Commit 8152052

Browse files
Fix: Incorrect serialisation of maps and sets in typescript-axios (OpenAPITools#17790)
1 parent 5f49a04 commit 8152052

15 files changed

Lines changed: 615 additions & 15 deletions

File tree

modules/openapi-generator/src/main/resources/typescript-axios/common.mustache

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any,
118118
? configuration.isJsonMime(requestOptions.headers['Content-Type'])
119119
: nonString;
120120
return needsSerialization
121-
? JSON.stringify(value !== undefined ? value : {})
121+
? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {})
122122
: (value || "");
123123
}
124124

125+
function convertMapsAndSetsToPlain(value: any): any {
126+
if (typeof Set === "undefined") return value;
127+
if (typeof Map === "undefined") return value;
128+
if (typeof value !== "object" || !value) {
129+
return value;
130+
}
131+
if (value instanceof Set) {
132+
return Array.from(value).map(item => convertMapsAndSetsToPlain(item));
133+
}
134+
if (value instanceof Map) {
135+
const entries: Array<[string, any]> = [];
136+
value.forEach((value: any, key: any) => {
137+
entries.push([key, convertMapsAndSetsToPlain(value)])
138+
});
139+
return objectFromEntries(entries);
140+
}
141+
if (Array.isArray(value)) {
142+
return value.map(it => convertMapsAndSetsToPlain(it));
143+
}
144+
return objectFromEntries(objectEntries(value)
145+
.map(([k, v]) => [k, convertMapsAndSetsToPlain(v)]));
146+
}
147+
148+
/**
149+
* Ponyfill for Object.entries
150+
*/
151+
function objectEntries(object: Record<string, any>): Array<[string, any]> {
152+
return Object.keys(object).map(key => [key, object[key]]);
153+
}
154+
155+
/**
156+
* Ponyfill for Object.fromEntries
157+
*/
158+
function objectFromEntries(entries: any): Record<string, any> {
159+
return [...entries].reduce((object, [key, val]) => {
160+
object[key] = val;
161+
return object;
162+
}, {});
163+
}
164+
125165
/**
126166
*
127167
* @export

samples/client/echo_api/typescript-axios/build/common.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any,
126126
? configuration.isJsonMime(requestOptions.headers['Content-Type'])
127127
: nonString;
128128
return needsSerialization
129-
? JSON.stringify(value !== undefined ? value : {})
129+
? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {})
130130
: (value || "");
131131
}
132132

133+
function convertMapsAndSetsToPlain(value: any): any {
134+
if (typeof Set === "undefined") return value;
135+
if (typeof Map === "undefined") return value;
136+
if (typeof value !== "object" || !value) {
137+
return value;
138+
}
139+
if (value instanceof Set) {
140+
return Array.from(value).map(item => convertMapsAndSetsToPlain(item));
141+
}
142+
if (value instanceof Map) {
143+
const entries: Array<[string, any]> = [];
144+
value.forEach((value: any, key: any) => {
145+
entries.push([key, convertMapsAndSetsToPlain(value)])
146+
});
147+
return objectFromEntries(entries);
148+
}
149+
if (Array.isArray(value)) {
150+
return value.map(it => convertMapsAndSetsToPlain(it));
151+
}
152+
return objectFromEntries(objectEntries(value)
153+
.map(([k, v]) => [k, convertMapsAndSetsToPlain(v)]));
154+
}
155+
156+
/**
157+
* Ponyfill for Object.entries
158+
*/
159+
function objectEntries(object: Record<string, any>): Array<[string, any]> {
160+
return Object.keys(object).map(key => [key, object[key]]);
161+
}
162+
163+
/**
164+
* Ponyfill for Object.fromEntries
165+
*/
166+
function objectFromEntries(entries: any): Record<string, any> {
167+
return [...entries].reduce((object, [key, val]) => {
168+
object[key] = val;
169+
return object;
170+
}, {});
171+
}
172+
133173
/**
134174
*
135175
* @export

samples/client/others/typescript-axios/with-separate-models-and-api-inheritance/common.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any,
126126
? configuration.isJsonMime(requestOptions.headers['Content-Type'])
127127
: nonString;
128128
return needsSerialization
129-
? JSON.stringify(value !== undefined ? value : {})
129+
? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {})
130130
: (value || "");
131131
}
132132

133+
function convertMapsAndSetsToPlain(value: any): any {
134+
if (typeof Set === "undefined") return value;
135+
if (typeof Map === "undefined") return value;
136+
if (typeof value !== "object" || !value) {
137+
return value;
138+
}
139+
if (value instanceof Set) {
140+
return Array.from(value).map(item => convertMapsAndSetsToPlain(item));
141+
}
142+
if (value instanceof Map) {
143+
const entries: Array<[string, any]> = [];
144+
value.forEach((value: any, key: any) => {
145+
entries.push([key, convertMapsAndSetsToPlain(value)])
146+
});
147+
return objectFromEntries(entries);
148+
}
149+
if (Array.isArray(value)) {
150+
return value.map(it => convertMapsAndSetsToPlain(it));
151+
}
152+
return objectFromEntries(objectEntries(value)
153+
.map(([k, v]) => [k, convertMapsAndSetsToPlain(v)]));
154+
}
155+
156+
/**
157+
* Ponyfill for Object.entries
158+
*/
159+
function objectEntries(object: Record<string, any>): Array<[string, any]> {
160+
return Object.keys(object).map(key => [key, object[key]]);
161+
}
162+
163+
/**
164+
* Ponyfill for Object.fromEntries
165+
*/
166+
function objectFromEntries(entries: any): Record<string, any> {
167+
return [...entries].reduce((object, [key, val]) => {
168+
object[key] = val;
169+
return object;
170+
}, {});
171+
}
172+
133173
/**
134174
*
135175
* @export

samples/client/petstore/typescript-axios/builds/composed-schemas/common.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any,
126126
? configuration.isJsonMime(requestOptions.headers['Content-Type'])
127127
: nonString;
128128
return needsSerialization
129-
? JSON.stringify(value !== undefined ? value : {})
129+
? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {})
130130
: (value || "");
131131
}
132132

133+
function convertMapsAndSetsToPlain(value: any): any {
134+
if (typeof Set === "undefined") return value;
135+
if (typeof Map === "undefined") return value;
136+
if (typeof value !== "object" || !value) {
137+
return value;
138+
}
139+
if (value instanceof Set) {
140+
return Array.from(value).map(item => convertMapsAndSetsToPlain(item));
141+
}
142+
if (value instanceof Map) {
143+
const entries: Array<[string, any]> = [];
144+
value.forEach((value: any, key: any) => {
145+
entries.push([key, convertMapsAndSetsToPlain(value)])
146+
});
147+
return objectFromEntries(entries);
148+
}
149+
if (Array.isArray(value)) {
150+
return value.map(it => convertMapsAndSetsToPlain(it));
151+
}
152+
return objectFromEntries(objectEntries(value)
153+
.map(([k, v]) => [k, convertMapsAndSetsToPlain(v)]));
154+
}
155+
156+
/**
157+
* Ponyfill for Object.entries
158+
*/
159+
function objectEntries(object: Record<string, any>): Array<[string, any]> {
160+
return Object.keys(object).map(key => [key, object[key]]);
161+
}
162+
163+
/**
164+
* Ponyfill for Object.fromEntries
165+
*/
166+
function objectFromEntries(entries: any): Record<string, any> {
167+
return [...entries].reduce((object, [key, val]) => {
168+
object[key] = val;
169+
return object;
170+
}, {});
171+
}
172+
133173
/**
134174
*
135175
* @export

samples/client/petstore/typescript-axios/builds/default/common.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any,
126126
? configuration.isJsonMime(requestOptions.headers['Content-Type'])
127127
: nonString;
128128
return needsSerialization
129-
? JSON.stringify(value !== undefined ? value : {})
129+
? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {})
130130
: (value || "");
131131
}
132132

133+
function convertMapsAndSetsToPlain(value: any): any {
134+
if (typeof Set === "undefined") return value;
135+
if (typeof Map === "undefined") return value;
136+
if (typeof value !== "object" || !value) {
137+
return value;
138+
}
139+
if (value instanceof Set) {
140+
return Array.from(value).map(item => convertMapsAndSetsToPlain(item));
141+
}
142+
if (value instanceof Map) {
143+
const entries: Array<[string, any]> = [];
144+
value.forEach((value: any, key: any) => {
145+
entries.push([key, convertMapsAndSetsToPlain(value)])
146+
});
147+
return objectFromEntries(entries);
148+
}
149+
if (Array.isArray(value)) {
150+
return value.map(it => convertMapsAndSetsToPlain(it));
151+
}
152+
return objectFromEntries(objectEntries(value)
153+
.map(([k, v]) => [k, convertMapsAndSetsToPlain(v)]));
154+
}
155+
156+
/**
157+
* Ponyfill for Object.entries
158+
*/
159+
function objectEntries(object: Record<string, any>): Array<[string, any]> {
160+
return Object.keys(object).map(key => [key, object[key]]);
161+
}
162+
163+
/**
164+
* Ponyfill for Object.fromEntries
165+
*/
166+
function objectFromEntries(entries: any): Record<string, any> {
167+
return [...entries].reduce((object, [key, val]) => {
168+
object[key] = val;
169+
return object;
170+
}, {});
171+
}
172+
133173
/**
134174
*
135175
* @export

samples/client/petstore/typescript-axios/builds/es6-target/common.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any,
126126
? configuration.isJsonMime(requestOptions.headers['Content-Type'])
127127
: nonString;
128128
return needsSerialization
129-
? JSON.stringify(value !== undefined ? value : {})
129+
? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {})
130130
: (value || "");
131131
}
132132

133+
function convertMapsAndSetsToPlain(value: any): any {
134+
if (typeof Set === "undefined") return value;
135+
if (typeof Map === "undefined") return value;
136+
if (typeof value !== "object" || !value) {
137+
return value;
138+
}
139+
if (value instanceof Set) {
140+
return Array.from(value).map(item => convertMapsAndSetsToPlain(item));
141+
}
142+
if (value instanceof Map) {
143+
const entries: Array<[string, any]> = [];
144+
value.forEach((value: any, key: any) => {
145+
entries.push([key, convertMapsAndSetsToPlain(value)])
146+
});
147+
return objectFromEntries(entries);
148+
}
149+
if (Array.isArray(value)) {
150+
return value.map(it => convertMapsAndSetsToPlain(it));
151+
}
152+
return objectFromEntries(objectEntries(value)
153+
.map(([k, v]) => [k, convertMapsAndSetsToPlain(v)]));
154+
}
155+
156+
/**
157+
* Ponyfill for Object.entries
158+
*/
159+
function objectEntries(object: Record<string, any>): Array<[string, any]> {
160+
return Object.keys(object).map(key => [key, object[key]]);
161+
}
162+
163+
/**
164+
* Ponyfill for Object.fromEntries
165+
*/
166+
function objectFromEntries(entries: any): Record<string, any> {
167+
return [...entries].reduce((object, [key, val]) => {
168+
object[key] = val;
169+
return object;
170+
}, {});
171+
}
172+
133173
/**
134174
*
135175
* @export

samples/client/petstore/typescript-axios/builds/test-petstore/common.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any,
126126
? configuration.isJsonMime(requestOptions.headers['Content-Type'])
127127
: nonString;
128128
return needsSerialization
129-
? JSON.stringify(value !== undefined ? value : {})
129+
? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {})
130130
: (value || "");
131131
}
132132

133+
function convertMapsAndSetsToPlain(value: any): any {
134+
if (typeof Set === "undefined") return value;
135+
if (typeof Map === "undefined") return value;
136+
if (typeof value !== "object" || !value) {
137+
return value;
138+
}
139+
if (value instanceof Set) {
140+
return Array.from(value).map(item => convertMapsAndSetsToPlain(item));
141+
}
142+
if (value instanceof Map) {
143+
const entries: Array<[string, any]> = [];
144+
value.forEach((value: any, key: any) => {
145+
entries.push([key, convertMapsAndSetsToPlain(value)])
146+
});
147+
return objectFromEntries(entries);
148+
}
149+
if (Array.isArray(value)) {
150+
return value.map(it => convertMapsAndSetsToPlain(it));
151+
}
152+
return objectFromEntries(objectEntries(value)
153+
.map(([k, v]) => [k, convertMapsAndSetsToPlain(v)]));
154+
}
155+
156+
/**
157+
* Ponyfill for Object.entries
158+
*/
159+
function objectEntries(object: Record<string, any>): Array<[string, any]> {
160+
return Object.keys(object).map(key => [key, object[key]]);
161+
}
162+
163+
/**
164+
* Ponyfill for Object.fromEntries
165+
*/
166+
function objectFromEntries(entries: any): Record<string, any> {
167+
return [...entries].reduce((object, [key, val]) => {
168+
object[key] = val;
169+
return object;
170+
}, {});
171+
}
172+
133173
/**
134174
*
135175
* @export

0 commit comments

Comments
 (0)