diff --git a/modules/openapi-generator/src/main/resources/typescript-axios/common.mustache b/modules/openapi-generator/src/main/resources/typescript-axios/common.mustache index d778f29e6d3b..59627ddaa531 100755 --- a/modules/openapi-generator/src/main/resources/typescript-axios/common.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-axios/common.mustache @@ -90,10 +90,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any, ? configuration.isJsonMime(requestOptions.headers['Content-Type']) : nonString; return needsSerialization - ? JSON.stringify(value !== undefined ? value : {}) + ? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {}) : (value || ""); } +function convertMapsAndSetsToPlain(value: any): any { + if (typeof Set === "undefined") return value; + if (typeof Map === "undefined") return value; + if (typeof value !== "object" || !value) { + return value; + } + if (value instanceof Set) { + return Array.from(value).map(item => convertMapsAndSetsToPlain(item)); + } + if (value instanceof Map) { + const entries: Array<[string, any]> = []; + value.forEach((value: any, key: any) => { + entries.push([key, convertMapsAndSetsToPlain(value)]) + }); + return objectFromEntries(entries); + } + if (Array.isArray(value)) { + return value.map(it => convertMapsAndSetsToPlain(it)); + } + const proto = typeof Reflect !== "undefined" + ? Reflect.getPrototyReflect.getPrototypeOf(value) + : value.__proto__; + if (proto === Object.prototype || proto === null) { + return objectFromEntries(objectEntries(value) + .map(([k, v]) => [k, convertMapsAndSetsToPlain(v)])); + } + return value; +} + +function objectEntries(object: Record): Array<[string, any]> { + return Object.keys(object).map(key => [key, object[key]]); +} + +function objectFromEntries(entries: any): Record { + return [...entries].reduce((object, [key, val]) => { + object[key] = val; + return object; + }, {}); +} + export const toPathString = function (url: URL) { return url.pathname + url.search + url.hash } diff --git a/samples/client/echo_api/typescript-axios/build/common.ts b/samples/client/echo_api/typescript-axios/build/common.ts index 41e36d497493..8042a77103e2 100644 --- a/samples/client/echo_api/typescript-axios/build/common.ts +++ b/samples/client/echo_api/typescript-axios/build/common.ts @@ -97,10 +97,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any, ? configuration.isJsonMime(requestOptions.headers['Content-Type']) : nonString; return needsSerialization - ? JSON.stringify(value !== undefined ? value : {}) + ? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {}) : (value || ""); } +function convertMapsAndSetsToPlain(value: any): any { + if (typeof Set === "undefined") return value; + if (typeof Map === "undefined") return value; + if (typeof value !== "object" || !value) { + return value; + } + if (value instanceof Set) { + return Array.from(value).map(item => convertMapsAndSetsToPlain(item)); + } + if (value instanceof Map) { + const entries: Array<[string, any]> = []; + value.forEach((value: any, key: any) => { + entries.push([key, convertMapsAndSetsToPlain(value)]) + }); + return objectFromEntries(entries); + } + if (Array.isArray(value)) { + return value.map(it => convertMapsAndSetsToPlain(it)); + } + const proto = typeof Reflect !== "undefined" + ? Reflect.getPrototyReflect.getPrototypeOf(value) + : value.__proto__; + if (proto === Object.prototype || proto === null) { + return objectFromEntries(objectEntries(value) + .map(([k, v]) => [k, convertMapsAndSetsToPlain(v)])); + } + return value; +} + +function objectEntries(object: Record): Array<[string, any]> { + return Object.keys(object).map(key => [key, object[key]]); +} + +function objectFromEntries(entries: any): Record { + return [...entries].reduce((object, [key, val]) => { + object[key] = val; + return object; + }, {}); +} + export const toPathString = function (url: URL) { return url.pathname + url.search + url.hash } diff --git a/samples/client/others/typescript-axios/with-separate-models-and-api-inheritance/common.ts b/samples/client/others/typescript-axios/with-separate-models-and-api-inheritance/common.ts index e6ac882c1dac..bfee2823f17f 100644 --- a/samples/client/others/typescript-axios/with-separate-models-and-api-inheritance/common.ts +++ b/samples/client/others/typescript-axios/with-separate-models-and-api-inheritance/common.ts @@ -97,10 +97,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any, ? configuration.isJsonMime(requestOptions.headers['Content-Type']) : nonString; return needsSerialization - ? JSON.stringify(value !== undefined ? value : {}) + ? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {}) : (value || ""); } +function convertMapsAndSetsToPlain(value: any): any { + if (typeof Set === "undefined") return value; + if (typeof Map === "undefined") return value; + if (typeof value !== "object" || !value) { + return value; + } + if (value instanceof Set) { + return Array.from(value).map(item => convertMapsAndSetsToPlain(item)); + } + if (value instanceof Map) { + const entries: Array<[string, any]> = []; + value.forEach((value: any, key: any) => { + entries.push([key, convertMapsAndSetsToPlain(value)]) + }); + return objectFromEntries(entries); + } + if (Array.isArray(value)) { + return value.map(it => convertMapsAndSetsToPlain(it)); + } + const proto = typeof Reflect !== "undefined" + ? Reflect.getPrototyReflect.getPrototypeOf(value) + : value.__proto__; + if (proto === Object.prototype || proto === null) { + return objectFromEntries(objectEntries(value) + .map(([k, v]) => [k, convertMapsAndSetsToPlain(v)])); + } + return value; +} + +function objectEntries(object: Record): Array<[string, any]> { + return Object.keys(object).map(key => [key, object[key]]); +} + +function objectFromEntries(entries: any): Record { + return [...entries].reduce((object, [key, val]) => { + object[key] = val; + return object; + }, {}); +} + export const toPathString = function (url: URL) { return url.pathname + url.search + url.hash } diff --git a/samples/client/petstore/typescript-axios/builds/composed-schemas/common.ts b/samples/client/petstore/typescript-axios/builds/composed-schemas/common.ts index ff80920e8850..96e4298fa386 100644 --- a/samples/client/petstore/typescript-axios/builds/composed-schemas/common.ts +++ b/samples/client/petstore/typescript-axios/builds/composed-schemas/common.ts @@ -97,10 +97,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any, ? configuration.isJsonMime(requestOptions.headers['Content-Type']) : nonString; return needsSerialization - ? JSON.stringify(value !== undefined ? value : {}) + ? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {}) : (value || ""); } +function convertMapsAndSetsToPlain(value: any): any { + if (typeof Set === "undefined") return value; + if (typeof Map === "undefined") return value; + if (typeof value !== "object" || !value) { + return value; + } + if (value instanceof Set) { + return Array.from(value).map(item => convertMapsAndSetsToPlain(item)); + } + if (value instanceof Map) { + const entries: Array<[string, any]> = []; + value.forEach((value: any, key: any) => { + entries.push([key, convertMapsAndSetsToPlain(value)]) + }); + return objectFromEntries(entries); + } + if (Array.isArray(value)) { + return value.map(it => convertMapsAndSetsToPlain(it)); + } + const proto = typeof Reflect !== "undefined" + ? Reflect.getPrototyReflect.getPrototypeOf(value) + : value.__proto__; + if (proto === Object.prototype || proto === null) { + return objectFromEntries(objectEntries(value) + .map(([k, v]) => [k, convertMapsAndSetsToPlain(v)])); + } + return value; +} + +function objectEntries(object: Record): Array<[string, any]> { + return Object.keys(object).map(key => [key, object[key]]); +} + +function objectFromEntries(entries: any): Record { + return [...entries].reduce((object, [key, val]) => { + object[key] = val; + return object; + }, {}); +} + export const toPathString = function (url: URL) { return url.pathname + url.search + url.hash } diff --git a/samples/client/petstore/typescript-axios/builds/default/common.ts b/samples/client/petstore/typescript-axios/builds/default/common.ts index 48a99b2fb1e0..6d09d5aaa1a9 100644 --- a/samples/client/petstore/typescript-axios/builds/default/common.ts +++ b/samples/client/petstore/typescript-axios/builds/default/common.ts @@ -97,10 +97,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any, ? configuration.isJsonMime(requestOptions.headers['Content-Type']) : nonString; return needsSerialization - ? JSON.stringify(value !== undefined ? value : {}) + ? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {}) : (value || ""); } +function convertMapsAndSetsToPlain(value: any): any { + if (typeof Set === "undefined") return value; + if (typeof Map === "undefined") return value; + if (typeof value !== "object" || !value) { + return value; + } + if (value instanceof Set) { + return Array.from(value).map(item => convertMapsAndSetsToPlain(item)); + } + if (value instanceof Map) { + const entries: Array<[string, any]> = []; + value.forEach((value: any, key: any) => { + entries.push([key, convertMapsAndSetsToPlain(value)]) + }); + return objectFromEntries(entries); + } + if (Array.isArray(value)) { + return value.map(it => convertMapsAndSetsToPlain(it)); + } + const proto = typeof Reflect !== "undefined" + ? Reflect.getPrototyReflect.getPrototypeOf(value) + : value.__proto__; + if (proto === Object.prototype || proto === null) { + return objectFromEntries(objectEntries(value) + .map(([k, v]) => [k, convertMapsAndSetsToPlain(v)])); + } + return value; +} + +function objectEntries(object: Record): Array<[string, any]> { + return Object.keys(object).map(key => [key, object[key]]); +} + +function objectFromEntries(entries: any): Record { + return [...entries].reduce((object, [key, val]) => { + object[key] = val; + return object; + }, {}); +} + export const toPathString = function (url: URL) { return url.pathname + url.search + url.hash } diff --git a/samples/client/petstore/typescript-axios/builds/es6-target/common.ts b/samples/client/petstore/typescript-axios/builds/es6-target/common.ts index 48a99b2fb1e0..6d09d5aaa1a9 100644 --- a/samples/client/petstore/typescript-axios/builds/es6-target/common.ts +++ b/samples/client/petstore/typescript-axios/builds/es6-target/common.ts @@ -97,10 +97,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any, ? configuration.isJsonMime(requestOptions.headers['Content-Type']) : nonString; return needsSerialization - ? JSON.stringify(value !== undefined ? value : {}) + ? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {}) : (value || ""); } +function convertMapsAndSetsToPlain(value: any): any { + if (typeof Set === "undefined") return value; + if (typeof Map === "undefined") return value; + if (typeof value !== "object" || !value) { + return value; + } + if (value instanceof Set) { + return Array.from(value).map(item => convertMapsAndSetsToPlain(item)); + } + if (value instanceof Map) { + const entries: Array<[string, any]> = []; + value.forEach((value: any, key: any) => { + entries.push([key, convertMapsAndSetsToPlain(value)]) + }); + return objectFromEntries(entries); + } + if (Array.isArray(value)) { + return value.map(it => convertMapsAndSetsToPlain(it)); + } + const proto = typeof Reflect !== "undefined" + ? Reflect.getPrototyReflect.getPrototypeOf(value) + : value.__proto__; + if (proto === Object.prototype || proto === null) { + return objectFromEntries(objectEntries(value) + .map(([k, v]) => [k, convertMapsAndSetsToPlain(v)])); + } + return value; +} + +function objectEntries(object: Record): Array<[string, any]> { + return Object.keys(object).map(key => [key, object[key]]); +} + +function objectFromEntries(entries: any): Record { + return [...entries].reduce((object, [key, val]) => { + object[key] = val; + return object; + }, {}); +} + export const toPathString = function (url: URL) { return url.pathname + url.search + url.hash } diff --git a/samples/client/petstore/typescript-axios/builds/test-petstore/common.ts b/samples/client/petstore/typescript-axios/builds/test-petstore/common.ts index 0a5b63b231ff..597d494f615a 100644 --- a/samples/client/petstore/typescript-axios/builds/test-petstore/common.ts +++ b/samples/client/petstore/typescript-axios/builds/test-petstore/common.ts @@ -97,10 +97,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any, ? configuration.isJsonMime(requestOptions.headers['Content-Type']) : nonString; return needsSerialization - ? JSON.stringify(value !== undefined ? value : {}) + ? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {}) : (value || ""); } +function convertMapsAndSetsToPlain(value: any): any { + if (typeof Set === "undefined") return value; + if (typeof Map === "undefined") return value; + if (typeof value !== "object" || !value) { + return value; + } + if (value instanceof Set) { + return Array.from(value).map(item => convertMapsAndSetsToPlain(item)); + } + if (value instanceof Map) { + const entries: Array<[string, any]> = []; + value.forEach((value: any, key: any) => { + entries.push([key, convertMapsAndSetsToPlain(value)]) + }); + return objectFromEntries(entries); + } + if (Array.isArray(value)) { + return value.map(it => convertMapsAndSetsToPlain(it)); + } + const proto = typeof Reflect !== "undefined" + ? Reflect.getPrototyReflect.getPrototypeOf(value) + : value.__proto__; + if (proto === Object.prototype || proto === null) { + return objectFromEntries(objectEntries(value) + .map(([k, v]) => [k, convertMapsAndSetsToPlain(v)])); + } + return value; +} + +function objectEntries(object: Record): Array<[string, any]> { + return Object.keys(object).map(key => [key, object[key]]); +} + +function objectFromEntries(entries: any): Record { + return [...entries].reduce((object, [key, val]) => { + object[key] = val; + return object; + }, {}); +} + export const toPathString = function (url: URL) { return url.pathname + url.search + url.hash } diff --git a/samples/client/petstore/typescript-axios/builds/with-complex-headers/common.ts b/samples/client/petstore/typescript-axios/builds/with-complex-headers/common.ts index 48a99b2fb1e0..6d09d5aaa1a9 100644 --- a/samples/client/petstore/typescript-axios/builds/with-complex-headers/common.ts +++ b/samples/client/petstore/typescript-axios/builds/with-complex-headers/common.ts @@ -97,10 +97,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any, ? configuration.isJsonMime(requestOptions.headers['Content-Type']) : nonString; return needsSerialization - ? JSON.stringify(value !== undefined ? value : {}) + ? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {}) : (value || ""); } +function convertMapsAndSetsToPlain(value: any): any { + if (typeof Set === "undefined") return value; + if (typeof Map === "undefined") return value; + if (typeof value !== "object" || !value) { + return value; + } + if (value instanceof Set) { + return Array.from(value).map(item => convertMapsAndSetsToPlain(item)); + } + if (value instanceof Map) { + const entries: Array<[string, any]> = []; + value.forEach((value: any, key: any) => { + entries.push([key, convertMapsAndSetsToPlain(value)]) + }); + return objectFromEntries(entries); + } + if (Array.isArray(value)) { + return value.map(it => convertMapsAndSetsToPlain(it)); + } + const proto = typeof Reflect !== "undefined" + ? Reflect.getPrototyReflect.getPrototypeOf(value) + : value.__proto__; + if (proto === Object.prototype || proto === null) { + return objectFromEntries(objectEntries(value) + .map(([k, v]) => [k, convertMapsAndSetsToPlain(v)])); + } + return value; +} + +function objectEntries(object: Record): Array<[string, any]> { + return Object.keys(object).map(key => [key, object[key]]); +} + +function objectFromEntries(entries: any): Record { + return [...entries].reduce((object, [key, val]) => { + object[key] = val; + return object; + }, {}); +} + export const toPathString = function (url: URL) { return url.pathname + url.search + url.hash } diff --git a/samples/client/petstore/typescript-axios/builds/with-fake-endpoints-models-for-testing-with-http-signature/common.ts b/samples/client/petstore/typescript-axios/builds/with-fake-endpoints-models-for-testing-with-http-signature/common.ts index 0a5b63b231ff..597d494f615a 100644 --- a/samples/client/petstore/typescript-axios/builds/with-fake-endpoints-models-for-testing-with-http-signature/common.ts +++ b/samples/client/petstore/typescript-axios/builds/with-fake-endpoints-models-for-testing-with-http-signature/common.ts @@ -97,10 +97,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any, ? configuration.isJsonMime(requestOptions.headers['Content-Type']) : nonString; return needsSerialization - ? JSON.stringify(value !== undefined ? value : {}) + ? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {}) : (value || ""); } +function convertMapsAndSetsToPlain(value: any): any { + if (typeof Set === "undefined") return value; + if (typeof Map === "undefined") return value; + if (typeof value !== "object" || !value) { + return value; + } + if (value instanceof Set) { + return Array.from(value).map(item => convertMapsAndSetsToPlain(item)); + } + if (value instanceof Map) { + const entries: Array<[string, any]> = []; + value.forEach((value: any, key: any) => { + entries.push([key, convertMapsAndSetsToPlain(value)]) + }); + return objectFromEntries(entries); + } + if (Array.isArray(value)) { + return value.map(it => convertMapsAndSetsToPlain(it)); + } + const proto = typeof Reflect !== "undefined" + ? Reflect.getPrototyReflect.getPrototypeOf(value) + : value.__proto__; + if (proto === Object.prototype || proto === null) { + return objectFromEntries(objectEntries(value) + .map(([k, v]) => [k, convertMapsAndSetsToPlain(v)])); + } + return value; +} + +function objectEntries(object: Record): Array<[string, any]> { + return Object.keys(object).map(key => [key, object[key]]); +} + +function objectFromEntries(entries: any): Record { + return [...entries].reduce((object, [key, val]) => { + object[key] = val; + return object; + }, {}); +} + export const toPathString = function (url: URL) { return url.pathname + url.search + url.hash } diff --git a/samples/client/petstore/typescript-axios/builds/with-interfaces-and-with-single-request-param/common.ts b/samples/client/petstore/typescript-axios/builds/with-interfaces-and-with-single-request-param/common.ts index 48a99b2fb1e0..6d09d5aaa1a9 100644 --- a/samples/client/petstore/typescript-axios/builds/with-interfaces-and-with-single-request-param/common.ts +++ b/samples/client/petstore/typescript-axios/builds/with-interfaces-and-with-single-request-param/common.ts @@ -97,10 +97,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any, ? configuration.isJsonMime(requestOptions.headers['Content-Type']) : nonString; return needsSerialization - ? JSON.stringify(value !== undefined ? value : {}) + ? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {}) : (value || ""); } +function convertMapsAndSetsToPlain(value: any): any { + if (typeof Set === "undefined") return value; + if (typeof Map === "undefined") return value; + if (typeof value !== "object" || !value) { + return value; + } + if (value instanceof Set) { + return Array.from(value).map(item => convertMapsAndSetsToPlain(item)); + } + if (value instanceof Map) { + const entries: Array<[string, any]> = []; + value.forEach((value: any, key: any) => { + entries.push([key, convertMapsAndSetsToPlain(value)]) + }); + return objectFromEntries(entries); + } + if (Array.isArray(value)) { + return value.map(it => convertMapsAndSetsToPlain(it)); + } + const proto = typeof Reflect !== "undefined" + ? Reflect.getPrototyReflect.getPrototypeOf(value) + : value.__proto__; + if (proto === Object.prototype || proto === null) { + return objectFromEntries(objectEntries(value) + .map(([k, v]) => [k, convertMapsAndSetsToPlain(v)])); + } + return value; +} + +function objectEntries(object: Record): Array<[string, any]> { + return Object.keys(object).map(key => [key, object[key]]); +} + +function objectFromEntries(entries: any): Record { + return [...entries].reduce((object, [key, val]) => { + object[key] = val; + return object; + }, {}); +} + export const toPathString = function (url: URL) { return url.pathname + url.search + url.hash } diff --git a/samples/client/petstore/typescript-axios/builds/with-interfaces/common.ts b/samples/client/petstore/typescript-axios/builds/with-interfaces/common.ts index 48a99b2fb1e0..6d09d5aaa1a9 100644 --- a/samples/client/petstore/typescript-axios/builds/with-interfaces/common.ts +++ b/samples/client/petstore/typescript-axios/builds/with-interfaces/common.ts @@ -97,10 +97,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any, ? configuration.isJsonMime(requestOptions.headers['Content-Type']) : nonString; return needsSerialization - ? JSON.stringify(value !== undefined ? value : {}) + ? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {}) : (value || ""); } +function convertMapsAndSetsToPlain(value: any): any { + if (typeof Set === "undefined") return value; + if (typeof Map === "undefined") return value; + if (typeof value !== "object" || !value) { + return value; + } + if (value instanceof Set) { + return Array.from(value).map(item => convertMapsAndSetsToPlain(item)); + } + if (value instanceof Map) { + const entries: Array<[string, any]> = []; + value.forEach((value: any, key: any) => { + entries.push([key, convertMapsAndSetsToPlain(value)]) + }); + return objectFromEntries(entries); + } + if (Array.isArray(value)) { + return value.map(it => convertMapsAndSetsToPlain(it)); + } + const proto = typeof Reflect !== "undefined" + ? Reflect.getPrototyReflect.getPrototypeOf(value) + : value.__proto__; + if (proto === Object.prototype || proto === null) { + return objectFromEntries(objectEntries(value) + .map(([k, v]) => [k, convertMapsAndSetsToPlain(v)])); + } + return value; +} + +function objectEntries(object: Record): Array<[string, any]> { + return Object.keys(object).map(key => [key, object[key]]); +} + +function objectFromEntries(entries: any): Record { + return [...entries].reduce((object, [key, val]) => { + object[key] = val; + return object; + }, {}); +} + export const toPathString = function (url: URL) { return url.pathname + url.search + url.hash } diff --git a/samples/client/petstore/typescript-axios/builds/with-node-imports/common.ts b/samples/client/petstore/typescript-axios/builds/with-node-imports/common.ts index 69c609709819..07a0e4dc4759 100644 --- a/samples/client/petstore/typescript-axios/builds/with-node-imports/common.ts +++ b/samples/client/petstore/typescript-axios/builds/with-node-imports/common.ts @@ -98,10 +98,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any, ? configuration.isJsonMime(requestOptions.headers['Content-Type']) : nonString; return needsSerialization - ? JSON.stringify(value !== undefined ? value : {}) + ? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {}) : (value || ""); } +function convertMapsAndSetsToPlain(value: any): any { + if (typeof Set === "undefined") return value; + if (typeof Map === "undefined") return value; + if (typeof value !== "object" || !value) { + return value; + } + if (value instanceof Set) { + return Array.from(value).map(item => convertMapsAndSetsToPlain(item)); + } + if (value instanceof Map) { + const entries: Array<[string, any]> = []; + value.forEach((value: any, key: any) => { + entries.push([key, convertMapsAndSetsToPlain(value)]) + }); + return objectFromEntries(entries); + } + if (Array.isArray(value)) { + return value.map(it => convertMapsAndSetsToPlain(it)); + } + const proto = typeof Reflect !== "undefined" + ? Reflect.getPrototyReflect.getPrototypeOf(value) + : value.__proto__; + if (proto === Object.prototype || proto === null) { + return objectFromEntries(objectEntries(value) + .map(([k, v]) => [k, convertMapsAndSetsToPlain(v)])); + } + return value; +} + +function objectEntries(object: Record): Array<[string, any]> { + return Object.keys(object).map(key => [key, object[key]]); +} + +function objectFromEntries(entries: any): Record { + return [...entries].reduce((object, [key, val]) => { + object[key] = val; + return object; + }, {}); +} + export const toPathString = function (url: URL) { return url.pathname + url.search + url.hash } diff --git a/samples/client/petstore/typescript-axios/builds/with-npm-version-and-separate-models-and-api/common.ts b/samples/client/petstore/typescript-axios/builds/with-npm-version-and-separate-models-and-api/common.ts index 48a99b2fb1e0..6d09d5aaa1a9 100644 --- a/samples/client/petstore/typescript-axios/builds/with-npm-version-and-separate-models-and-api/common.ts +++ b/samples/client/petstore/typescript-axios/builds/with-npm-version-and-separate-models-and-api/common.ts @@ -97,10 +97,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any, ? configuration.isJsonMime(requestOptions.headers['Content-Type']) : nonString; return needsSerialization - ? JSON.stringify(value !== undefined ? value : {}) + ? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {}) : (value || ""); } +function convertMapsAndSetsToPlain(value: any): any { + if (typeof Set === "undefined") return value; + if (typeof Map === "undefined") return value; + if (typeof value !== "object" || !value) { + return value; + } + if (value instanceof Set) { + return Array.from(value).map(item => convertMapsAndSetsToPlain(item)); + } + if (value instanceof Map) { + const entries: Array<[string, any]> = []; + value.forEach((value: any, key: any) => { + entries.push([key, convertMapsAndSetsToPlain(value)]) + }); + return objectFromEntries(entries); + } + if (Array.isArray(value)) { + return value.map(it => convertMapsAndSetsToPlain(it)); + } + const proto = typeof Reflect !== "undefined" + ? Reflect.getPrototyReflect.getPrototypeOf(value) + : value.__proto__; + if (proto === Object.prototype || proto === null) { + return objectFromEntries(objectEntries(value) + .map(([k, v]) => [k, convertMapsAndSetsToPlain(v)])); + } + return value; +} + +function objectEntries(object: Record): Array<[string, any]> { + return Object.keys(object).map(key => [key, object[key]]); +} + +function objectFromEntries(entries: any): Record { + return [...entries].reduce((object, [key, val]) => { + object[key] = val; + return object; + }, {}); +} + export const toPathString = function (url: URL) { return url.pathname + url.search + url.hash } diff --git a/samples/client/petstore/typescript-axios/builds/with-npm-version/common.ts b/samples/client/petstore/typescript-axios/builds/with-npm-version/common.ts index 48a99b2fb1e0..6d09d5aaa1a9 100644 --- a/samples/client/petstore/typescript-axios/builds/with-npm-version/common.ts +++ b/samples/client/petstore/typescript-axios/builds/with-npm-version/common.ts @@ -97,10 +97,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any, ? configuration.isJsonMime(requestOptions.headers['Content-Type']) : nonString; return needsSerialization - ? JSON.stringify(value !== undefined ? value : {}) + ? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {}) : (value || ""); } +function convertMapsAndSetsToPlain(value: any): any { + if (typeof Set === "undefined") return value; + if (typeof Map === "undefined") return value; + if (typeof value !== "object" || !value) { + return value; + } + if (value instanceof Set) { + return Array.from(value).map(item => convertMapsAndSetsToPlain(item)); + } + if (value instanceof Map) { + const entries: Array<[string, any]> = []; + value.forEach((value: any, key: any) => { + entries.push([key, convertMapsAndSetsToPlain(value)]) + }); + return objectFromEntries(entries); + } + if (Array.isArray(value)) { + return value.map(it => convertMapsAndSetsToPlain(it)); + } + const proto = typeof Reflect !== "undefined" + ? Reflect.getPrototyReflect.getPrototypeOf(value) + : value.__proto__; + if (proto === Object.prototype || proto === null) { + return objectFromEntries(objectEntries(value) + .map(([k, v]) => [k, convertMapsAndSetsToPlain(v)])); + } + return value; +} + +function objectEntries(object: Record): Array<[string, any]> { + return Object.keys(object).map(key => [key, object[key]]); +} + +function objectFromEntries(entries: any): Record { + return [...entries].reduce((object, [key, val]) => { + object[key] = val; + return object; + }, {}); +} + export const toPathString = function (url: URL) { return url.pathname + url.search + url.hash } diff --git a/samples/client/petstore/typescript-axios/builds/with-single-request-parameters/common.ts b/samples/client/petstore/typescript-axios/builds/with-single-request-parameters/common.ts index 48a99b2fb1e0..6d09d5aaa1a9 100644 --- a/samples/client/petstore/typescript-axios/builds/with-single-request-parameters/common.ts +++ b/samples/client/petstore/typescript-axios/builds/with-single-request-parameters/common.ts @@ -97,10 +97,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any, ? configuration.isJsonMime(requestOptions.headers['Content-Type']) : nonString; return needsSerialization - ? JSON.stringify(value !== undefined ? value : {}) + ? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {}) : (value || ""); } +function convertMapsAndSetsToPlain(value: any): any { + if (typeof Set === "undefined") return value; + if (typeof Map === "undefined") return value; + if (typeof value !== "object" || !value) { + return value; + } + if (value instanceof Set) { + return Array.from(value).map(item => convertMapsAndSetsToPlain(item)); + } + if (value instanceof Map) { + const entries: Array<[string, any]> = []; + value.forEach((value: any, key: any) => { + entries.push([key, convertMapsAndSetsToPlain(value)]) + }); + return objectFromEntries(entries); + } + if (Array.isArray(value)) { + return value.map(it => convertMapsAndSetsToPlain(it)); + } + const proto = typeof Reflect !== "undefined" + ? Reflect.getPrototyReflect.getPrototypeOf(value) + : value.__proto__; + if (proto === Object.prototype || proto === null) { + return objectFromEntries(objectEntries(value) + .map(([k, v]) => [k, convertMapsAndSetsToPlain(v)])); + } + return value; +} + +function objectEntries(object: Record): Array<[string, any]> { + return Object.keys(object).map(key => [key, object[key]]); +} + +function objectFromEntries(entries: any): Record { + return [...entries].reduce((object, [key, val]) => { + object[key] = val; + return object; + }, {}); +} + export const toPathString = function (url: URL) { return url.pathname + url.search + url.hash } diff --git a/samples/client/petstore/typescript-axios/builds/with-string-enums/common.ts b/samples/client/petstore/typescript-axios/builds/with-string-enums/common.ts index 48a99b2fb1e0..6d09d5aaa1a9 100644 --- a/samples/client/petstore/typescript-axios/builds/with-string-enums/common.ts +++ b/samples/client/petstore/typescript-axios/builds/with-string-enums/common.ts @@ -97,10 +97,50 @@ export const serializeDataIfNeeded = function (value: any, requestOptions: any, ? configuration.isJsonMime(requestOptions.headers['Content-Type']) : nonString; return needsSerialization - ? JSON.stringify(value !== undefined ? value : {}) + ? JSON.stringify(value !== undefined ? convertMapsAndSetsToPlain(value) : {}) : (value || ""); } +function convertMapsAndSetsToPlain(value: any): any { + if (typeof Set === "undefined") return value; + if (typeof Map === "undefined") return value; + if (typeof value !== "object" || !value) { + return value; + } + if (value instanceof Set) { + return Array.from(value).map(item => convertMapsAndSetsToPlain(item)); + } + if (value instanceof Map) { + const entries: Array<[string, any]> = []; + value.forEach((value: any, key: any) => { + entries.push([key, convertMapsAndSetsToPlain(value)]) + }); + return objectFromEntries(entries); + } + if (Array.isArray(value)) { + return value.map(it => convertMapsAndSetsToPlain(it)); + } + const proto = typeof Reflect !== "undefined" + ? Reflect.getPrototyReflect.getPrototypeOf(value) + : value.__proto__; + if (proto === Object.prototype || proto === null) { + return objectFromEntries(objectEntries(value) + .map(([k, v]) => [k, convertMapsAndSetsToPlain(v)])); + } + return value; +} + +function objectEntries(object: Record): Array<[string, any]> { + return Object.keys(object).map(key => [key, object[key]]); +} + +function objectFromEntries(entries: any): Record { + return [...entries].reduce((object, [key, val]) => { + object[key] = val; + return object; + }, {}); +} + export const toPathString = function (url: URL) { return url.pathname + url.search + url.hash }