Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Always serialise Map and Set to object and array in typescript-axios #17790

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,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));
}
return objectFromEntries(objectEntries(value)
.map(([k, v]) => [k, convertMapsAndSetsToPlain(v)]));
}

/**
* Ponyfill for Object.entries
*/
function objectEntries(object: Record<string, any>): Array<[string, any]> {
return Object.keys(object).map(key => [key, object[key]]);
}

/**
* Ponyfill for Object.fromEntries
*/
function objectFromEntries(entries: any): Record<string, any> {
return [...entries].reduce((object, [key, val]) => {
object[key] = val;
return object;
}, {});
}

/**
*
* @export
Expand Down
42 changes: 41 additions & 1 deletion samples/client/echo_api/typescript-axios/build/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,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));
}
return objectFromEntries(objectEntries(value)
.map(([k, v]) => [k, convertMapsAndSetsToPlain(v)]));
}

/**
* Ponyfill for Object.entries
*/
function objectEntries(object: Record<string, any>): Array<[string, any]> {
return Object.keys(object).map(key => [key, object[key]]);
}

/**
* Ponyfill for Object.fromEntries
*/
function objectFromEntries(entries: any): Record<string, any> {
return [...entries].reduce((object, [key, val]) => {
object[key] = val;
return object;
}, {});
}

/**
*
* @export
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,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));
}
return objectFromEntries(objectEntries(value)
.map(([k, v]) => [k, convertMapsAndSetsToPlain(v)]));
}

/**
* Ponyfill for Object.entries
*/
function objectEntries(object: Record<string, any>): Array<[string, any]> {
return Object.keys(object).map(key => [key, object[key]]);
}

/**
* Ponyfill for Object.fromEntries
*/
function objectFromEntries(entries: any): Record<string, any> {
return [...entries].reduce((object, [key, val]) => {
object[key] = val;
return object;
}, {});
}

/**
*
* @export
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,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));
}
return objectFromEntries(objectEntries(value)
.map(([k, v]) => [k, convertMapsAndSetsToPlain(v)]));
}

/**
* Ponyfill for Object.entries
*/
function objectEntries(object: Record<string, any>): Array<[string, any]> {
return Object.keys(object).map(key => [key, object[key]]);
}

/**
* Ponyfill for Object.fromEntries
*/
function objectFromEntries(entries: any): Record<string, any> {
return [...entries].reduce((object, [key, val]) => {
object[key] = val;
return object;
}, {});
}

/**
*
* @export
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,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));
}
return objectFromEntries(objectEntries(value)
.map(([k, v]) => [k, convertMapsAndSetsToPlain(v)]));
}

/**
* Ponyfill for Object.entries
*/
function objectEntries(object: Record<string, any>): Array<[string, any]> {
return Object.keys(object).map(key => [key, object[key]]);
}

/**
* Ponyfill for Object.fromEntries
*/
function objectFromEntries(entries: any): Record<string, any> {
return [...entries].reduce((object, [key, val]) => {
object[key] = val;
return object;
}, {});
}

/**
*
* @export
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,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));
}
return objectFromEntries(objectEntries(value)
.map(([k, v]) => [k, convertMapsAndSetsToPlain(v)]));
}

/**
* Ponyfill for Object.entries
*/
function objectEntries(object: Record<string, any>): Array<[string, any]> {
return Object.keys(object).map(key => [key, object[key]]);
}

/**
* Ponyfill for Object.fromEntries
*/
function objectFromEntries(entries: any): Record<string, any> {
return [...entries].reduce((object, [key, val]) => {
object[key] = val;
return object;
}, {});
}

/**
*
* @export
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,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));
}
return objectFromEntries(objectEntries(value)
.map(([k, v]) => [k, convertMapsAndSetsToPlain(v)]));
}

/**
* Ponyfill for Object.entries
*/
function objectEntries(object: Record<string, any>): Array<[string, any]> {
return Object.keys(object).map(key => [key, object[key]]);
}

/**
* Ponyfill for Object.fromEntries
*/
function objectFromEntries(entries: any): Record<string, any> {
return [...entries].reduce((object, [key, val]) => {
object[key] = val;
return object;
}, {});
}

/**
*
* @export
Expand Down
Loading
Loading