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>) { 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;
}
if (typeof value === "string" && value.length === 0) { // Ignore empty strings.
return false; if (typeof value === "string" && value.length === 0) {
} continue;
}
if (Array.isArray(value) && value.length === 0) { // Ignore empty arrays.
return false; if (Array.isArray(value) && value.length === 0) {
} 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();
} }

View file

@ -42,6 +42,6 @@ describe("Authorization", () => {
"idToken", "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", "clients",
"first broker login", "first broker login",
"docker auth", "docker auth",
"http challenge",
]); ]);
}); });

View file

@ -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",
); );
}); });
}); });