Properly handle array query arguments in Admin Client (#24483)

Closes #20135
This commit is contained in:
Jon Koops 2023-11-02 17:08:42 +01:00 committed by GitHub
parent bd80f208fa
commit a3a2f78dbd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 18 deletions

View file

@ -1,21 +1,29 @@
export function stringifyQueryParams(params: Record<string, unknown>) {
return new URLSearchParams(
Object.entries(params).filter((param): param is [string, string] => {
const [, value] = param;
const searchParams = new URLSearchParams();
if (typeof value === "undefined" || value === null) {
return false;
}
for (const [key, value] of Object.entries(params)) {
// Ignore undefined and null values.
if (value === undefined || value === null) {
continue;
}
if (typeof value === "string" && value.length === 0) {
return false;
}
// Ignore empty strings.
if (typeof value === "string" && value.length === 0) {
continue;
}
if (Array.isArray(value) && value.length === 0) {
return false;
}
// Ignore empty arrays.
if (Array.isArray(value) && value.length === 0) {
continue;
}
return true;
}),
).toString();
// Append each entry of an array as a separate parameter, or the value itself otherwise.
if (Array.isArray(value)) {
value.forEach((item) => searchParams.append(key, item.toString()));
} else {
searchParams.append(key, value.toString());
}
}
return searchParams.toString();
}

View file

@ -42,6 +42,6 @@ describe("Authorization", () => {
"idToken",
);
expect(data.scope).to.equal("openid profile email");
expect(data.scope).to.equal("openid email profile");
});
});

View file

@ -176,7 +176,6 @@ describe("Authentication management", () => {
"clients",
"first broker login",
"docker auth",
"http challenge",
]);
});

View file

@ -23,9 +23,10 @@ describe("stringifyQueryParams", () => {
numZero: 0,
numNegative: -1,
str: "Hello World!",
arr: ["foo", "bar"],
}),
).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",
);
});
});