Properly handle array query arguments in Admin Client (#24483)
Closes #20135
This commit is contained in:
parent
bd80f208fa
commit
a3a2f78dbd
4 changed files with 26 additions and 18 deletions
|
@ -1,21 +1,29 @@
|
||||||
export function stringifyQueryParams(params: Record<string, unknown>) {
|
export function stringifyQueryParams(params: Record<string, unknown>) {
|
||||||
return new URLSearchParams(
|
const searchParams = new URLSearchParams();
|
||||||
Object.entries(params).filter((param): param is [string, string] => {
|
|
||||||
const [, value] = param;
|
|
||||||
|
|
||||||
if (typeof value === "undefined" || value === null) {
|
for (const [key, value] of Object.entries(params)) {
|
||||||
return false;
|
// Ignore undefined and null values.
|
||||||
|
if (value === undefined || value === null) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ignore empty strings.
|
||||||
if (typeof value === "string" && value.length === 0) {
|
if (typeof value === "string" && value.length === 0) {
|
||||||
return false;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ignore empty arrays.
|
||||||
if (Array.isArray(value) && value.length === 0) {
|
if (Array.isArray(value) && value.length === 0) {
|
||||||
return false;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
// Append each entry of an array as a separate parameter, or the value itself otherwise.
|
||||||
}),
|
if (Array.isArray(value)) {
|
||||||
).toString();
|
value.forEach((item) => searchParams.append(key, item.toString()));
|
||||||
|
} else {
|
||||||
|
searchParams.append(key, value.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return searchParams.toString();
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,6 @@ describe("Authorization", () => {
|
||||||
"idToken",
|
"idToken",
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(data.scope).to.equal("openid profile email");
|
expect(data.scope).to.equal("openid email profile");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -176,7 +176,6 @@ describe("Authentication management", () => {
|
||||||
"clients",
|
"clients",
|
||||||
"first broker login",
|
"first broker login",
|
||||||
"docker auth",
|
"docker auth",
|
||||||
"http challenge",
|
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -23,9 +23,10 @@ describe("stringifyQueryParams", () => {
|
||||||
numZero: 0,
|
numZero: 0,
|
||||||
numNegative: -1,
|
numNegative: -1,
|
||||||
str: "Hello World!",
|
str: "Hello World!",
|
||||||
|
arr: ["foo", "bar"],
|
||||||
}),
|
}),
|
||||||
).to.equal(
|
).to.equal(
|
||||||
"boolTrue=true&boolFalse=false&numPositive=1&numZero=0&numNegative=-1&str=Hello+World%21",
|
"boolTrue=true&boolFalse=false&numPositive=1&numZero=0&numNegative=-1&str=Hello+World%21&arr=foo&arr=bar",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue