Skip to content

Commit

Permalink
Fix: Incorrect serialisation of maps and sets in typescript-axios (Op…
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianhelvikspond authored Mar 19, 2024
1 parent 5f49a04 commit 8152052
Show file tree
Hide file tree
Showing 15 changed files with 615 additions and 15 deletions.
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

0 comments on commit 8152052

Please sign in to comment.