Skip to content

Commit b9f291a

Browse files
Fix: Incorrect serialisation of maps and sets in typescript-axios without changing other object types
1 parent fe6da71 commit b9f291a

16 files changed

Lines changed: 656 additions & 16 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
@@ -90,10 +90,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any,
9090
? configuration.isJsonMime(requestOptions.headers['Content-Type'])
9191
: nonString;
9292
return needsSerialization
93-
? JSON.stringify(value !== undefined ? value : {})
93+
? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {})
9494
: (value || "");
9595
}
9696

97+
function convertMapsAndSetsToPlain(value: any): any {
98+
if (typeof Set === "undefined") return value;
99+
if (typeof Map === "undefined") return value;
100+
if (typeof value !== "object" || !value) {
101+
return value;
102+
}
103+
if (value instanceof Set) {
104+
return Array.from(value).map(item => convertMapsAndSetsToPlain(item));
105+
}
106+
if (value instanceof Map) {
107+
const entries: Array<[string, any]> = [];
108+
value.forEach((value: any, key: any) => {
109+
entries.push([key, convertMapsAndSetsToPlain(value)])
110+
});
111+
return objectFromEntries(entries);
112+
}
113+
if (Array.isArray(value)) {
114+
return value.map(it => convertMapsAndSetsToPlain(it));
115+
}
116+
const proto = typeof Reflect !== "undefined"
117+
? Reflect.getPrototypeOf(value)
118+
: value.__proto__;
119+
if (proto === Object.prototype || proto === null) {
120+
return objectFromEntries(objectEntries(value)
121+
.map(([k, v]) => [k, convertMapsAndSetsToPlain(v)]));
122+
}
123+
return value;
124+
}
125+
126+
function objectEntries(object: Record<string, any>): Array<[string, any]> {
127+
return Object.keys(object).map(key => [key, object[key]]);
128+
}
129+
130+
function objectFromEntries(entries: any): Record<string, any> {
131+
return [...entries].reduce((object, [key, val]) => {
132+
object[key] = val;
133+
return object;
134+
}, {});
135+
}
136+
97137
export const toPathString = function (url: URL) {
98138
return url.pathname + url.search + url.hash
99139
}

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any,
9797
? configuration.isJsonMime(requestOptions.headers['Content-Type'])
9898
: nonString;
9999
return needsSerialization
100-
? JSON.stringify(value !== undefined ? value : {})
100+
? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {})
101101
: (value || "");
102102
}
103103

104+
function convertMapsAndSetsToPlain(value: any): any {
105+
if (typeof Set === "undefined") return value;
106+
if (typeof Map === "undefined") return value;
107+
if (typeof value !== "object" || !value) {
108+
return value;
109+
}
110+
if (value instanceof Set) {
111+
return Array.from(value).map(item => convertMapsAndSetsToPlain(item));
112+
}
113+
if (value instanceof Map) {
114+
const entries: Array<[string, any]> = [];
115+
value.forEach((value: any, key: any) => {
116+
entries.push([key, convertMapsAndSetsToPlain(value)])
117+
});
118+
return objectFromEntries(entries);
119+
}
120+
if (Array.isArray(value)) {
121+
return value.map(it => convertMapsAndSetsToPlain(it));
122+
}
123+
const proto = typeof Reflect !== "undefined"
124+
? Reflect.getPrototypeOf(value)
125+
: value.__proto__;
126+
if (proto === Object.prototype || proto === null) {
127+
return objectFromEntries(objectEntries(value)
128+
.map(([k, v]) => [k, convertMapsAndSetsToPlain(v)]));
129+
}
130+
return value;
131+
}
132+
133+
function objectEntries(object: Record<string, any>): Array<[string, any]> {
134+
return Object.keys(object).map(key => [key, object[key]]);
135+
}
136+
137+
function objectFromEntries(entries: any): Record<string, any> {
138+
return [...entries].reduce((object, [key, val]) => {
139+
object[key] = val;
140+
return object;
141+
}, {});
142+
}
143+
104144
export const toPathString = function (url: URL) {
105145
return url.pathname + url.search + url.hash
106146
}

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
@@ -97,10 +97,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any,
9797
? configuration.isJsonMime(requestOptions.headers['Content-Type'])
9898
: nonString;
9999
return needsSerialization
100-
? JSON.stringify(value !== undefined ? value : {})
100+
? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {})
101101
: (value || "");
102102
}
103103

104+
function convertMapsAndSetsToPlain(value: any): any {
105+
if (typeof Set === "undefined") return value;
106+
if (typeof Map === "undefined") return value;
107+
if (typeof value !== "object" || !value) {
108+
return value;
109+
}
110+
if (value instanceof Set) {
111+
return Array.from(value).map(item => convertMapsAndSetsToPlain(item));
112+
}
113+
if (value instanceof Map) {
114+
const entries: Array<[string, any]> = [];
115+
value.forEach((value: any, key: any) => {
116+
entries.push([key, convertMapsAndSetsToPlain(value)])
117+
});
118+
return objectFromEntries(entries);
119+
}
120+
if (Array.isArray(value)) {
121+
return value.map(it => convertMapsAndSetsToPlain(it));
122+
}
123+
const proto = typeof Reflect !== "undefined"
124+
? Reflect.getPrototypeOf(value)
125+
: value.__proto__;
126+
if (proto === Object.prototype || proto === null) {
127+
return objectFromEntries(objectEntries(value)
128+
.map(([k, v]) => [k, convertMapsAndSetsToPlain(v)]));
129+
}
130+
return value;
131+
}
132+
133+
function objectEntries(object: Record<string, any>): Array<[string, any]> {
134+
return Object.keys(object).map(key => [key, object[key]]);
135+
}
136+
137+
function objectFromEntries(entries: any): Record<string, any> {
138+
return [...entries].reduce((object, [key, val]) => {
139+
object[key] = val;
140+
return object;
141+
}, {});
142+
}
143+
104144
export const toPathString = function (url: URL) {
105145
return url.pathname + url.search + url.hash
106146
}

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any,
9797
? configuration.isJsonMime(requestOptions.headers['Content-Type'])
9898
: nonString;
9999
return needsSerialization
100-
? JSON.stringify(value !== undefined ? value : {})
100+
? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {})
101101
: (value || "");
102102
}
103103

104+
function convertMapsAndSetsToPlain(value: any): any {
105+
if (typeof Set === "undefined") return value;
106+
if (typeof Map === "undefined") return value;
107+
if (typeof value !== "object" || !value) {
108+
return value;
109+
}
110+
if (value instanceof Set) {
111+
return Array.from(value).map(item => convertMapsAndSetsToPlain(item));
112+
}
113+
if (value instanceof Map) {
114+
const entries: Array<[string, any]> = [];
115+
value.forEach((value: any, key: any) => {
116+
entries.push([key, convertMapsAndSetsToPlain(value)])
117+
});
118+
return objectFromEntries(entries);
119+
}
120+
if (Array.isArray(value)) {
121+
return value.map(it => convertMapsAndSetsToPlain(it));
122+
}
123+
const proto = typeof Reflect !== "undefined"
124+
? Reflect.getPrototypeOf(value)
125+
: value.__proto__;
126+
if (proto === Object.prototype || proto === null) {
127+
return objectFromEntries(objectEntries(value)
128+
.map(([k, v]) => [k, convertMapsAndSetsToPlain(v)]));
129+
}
130+
return value;
131+
}
132+
133+
function objectEntries(object: Record<string, any>): Array<[string, any]> {
134+
return Object.keys(object).map(key => [key, object[key]]);
135+
}
136+
137+
function objectFromEntries(entries: any): Record<string, any> {
138+
return [...entries].reduce((object, [key, val]) => {
139+
object[key] = val;
140+
return object;
141+
}, {});
142+
}
143+
104144
export const toPathString = function (url: URL) {
105145
return url.pathname + url.search + url.hash
106146
}

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any,
9797
? configuration.isJsonMime(requestOptions.headers['Content-Type'])
9898
: nonString;
9999
return needsSerialization
100-
? JSON.stringify(value !== undefined ? value : {})
100+
? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {})
101101
: (value || "");
102102
}
103103

104+
function convertMapsAndSetsToPlain(value: any): any {
105+
if (typeof Set === "undefined") return value;
106+
if (typeof Map === "undefined") return value;
107+
if (typeof value !== "object" || !value) {
108+
return value;
109+
}
110+
if (value instanceof Set) {
111+
return Array.from(value).map(item => convertMapsAndSetsToPlain(item));
112+
}
113+
if (value instanceof Map) {
114+
const entries: Array<[string, any]> = [];
115+
value.forEach((value: any, key: any) => {
116+
entries.push([key, convertMapsAndSetsToPlain(value)])
117+
});
118+
return objectFromEntries(entries);
119+
}
120+
if (Array.isArray(value)) {
121+
return value.map(it => convertMapsAndSetsToPlain(it));
122+
}
123+
const proto = typeof Reflect !== "undefined"
124+
? Reflect.getPrototypeOf(value)
125+
: value.__proto__;
126+
if (proto === Object.prototype || proto === null) {
127+
return objectFromEntries(objectEntries(value)
128+
.map(([k, v]) => [k, convertMapsAndSetsToPlain(v)]));
129+
}
130+
return value;
131+
}
132+
133+
function objectEntries(object: Record<string, any>): Array<[string, any]> {
134+
return Object.keys(object).map(key => [key, object[key]]);
135+
}
136+
137+
function objectFromEntries(entries: any): Record<string, any> {
138+
return [...entries].reduce((object, [key, val]) => {
139+
object[key] = val;
140+
return object;
141+
}, {});
142+
}
143+
104144
export const toPathString = function (url: URL) {
105145
return url.pathname + url.search + url.hash
106146
}

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any,
9797
? configuration.isJsonMime(requestOptions.headers['Content-Type'])
9898
: nonString;
9999
return needsSerialization
100-
? JSON.stringify(value !== undefined ? value : {})
100+
? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {})
101101
: (value || "");
102102
}
103103

104+
function convertMapsAndSetsToPlain(value: any): any {
105+
if (typeof Set === "undefined") return value;
106+
if (typeof Map === "undefined") return value;
107+
if (typeof value !== "object" || !value) {
108+
return value;
109+
}
110+
if (value instanceof Set) {
111+
return Array.from(value).map(item => convertMapsAndSetsToPlain(item));
112+
}
113+
if (value instanceof Map) {
114+
const entries: Array<[string, any]> = [];
115+
value.forEach((value: any, key: any) => {
116+
entries.push([key, convertMapsAndSetsToPlain(value)])
117+
});
118+
return objectFromEntries(entries);
119+
}
120+
if (Array.isArray(value)) {
121+
return value.map(it => convertMapsAndSetsToPlain(it));
122+
}
123+
const proto = typeof Reflect !== "undefined"
124+
? Reflect.getPrototypeOf(value)
125+
: value.__proto__;
126+
if (proto === Object.prototype || proto === null) {
127+
return objectFromEntries(objectEntries(value)
128+
.map(([k, v]) => [k, convertMapsAndSetsToPlain(v)]));
129+
}
130+
return value;
131+
}
132+
133+
function objectEntries(object: Record<string, any>): Array<[string, any]> {
134+
return Object.keys(object).map(key => [key, object[key]]);
135+
}
136+
137+
function objectFromEntries(entries: any): Record<string, any> {
138+
return [...entries].reduce((object, [key, val]) => {
139+
object[key] = val;
140+
return object;
141+
}, {});
142+
}
143+
104144
export const toPathString = function (url: URL) {
105145
return url.pathname + url.search + url.hash
106146
}

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any,
9797
? configuration.isJsonMime(requestOptions.headers['Content-Type'])
9898
: nonString;
9999
return needsSerialization
100-
? JSON.stringify(value !== undefined ? value : {})
100+
? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {})
101101
: (value || "");
102102
}
103103

104+
function convertMapsAndSetsToPlain(value: any): any {
105+
if (typeof Set === "undefined") return value;
106+
if (typeof Map === "undefined") return value;
107+
if (typeof value !== "object" || !value) {
108+
return value;
109+
}
110+
if (value instanceof Set) {
111+
return Array.from(value).map(item => convertMapsAndSetsToPlain(item));
112+
}
113+
if (value instanceof Map) {
114+
const entries: Array<[string, any]> = [];
115+
value.forEach((value: any, key: any) => {
116+
entries.push([key, convertMapsAndSetsToPlain(value)])
117+
});
118+
return objectFromEntries(entries);
119+
}
120+
if (Array.isArray(value)) {
121+
return value.map(it => convertMapsAndSetsToPlain(it));
122+
}
123+
const proto = typeof Reflect !== "undefined"
124+
? Reflect.getPrototypeOf(value)
125+
: value.__proto__;
126+
if (proto === Object.prototype || proto === null) {
127+
return objectFromEntries(objectEntries(value)
128+
.map(([k, v]) => [k, convertMapsAndSetsToPlain(v)]));
129+
}
130+
return value;
131+
}
132+
133+
function objectEntries(object: Record<string, any>): Array<[string, any]> {
134+
return Object.keys(object).map(key => [key, object[key]]);
135+
}
136+
137+
function objectFromEntries(entries: any): Record<string, any> {
138+
return [...entries].reduce((object, [key, val]) => {
139+
object[key] = val;
140+
return object;
141+
}, {});
142+
}
143+
104144
export const toPathString = function (url: URL) {
105145
return url.pathname + url.search + url.hash
106146
}

0 commit comments

Comments
 (0)