Skip to content

Commit 27fbed4

Browse files
authored
Merge pull request #645 from IBM/fix-array-input-parsing
Fix: array input parsing
2 parents 2f269c9 + f040dc8 commit 27fbed4

File tree

1 file changed

+60
-44
lines changed

1 file changed

+60
-44
lines changed

mcpgateway/static/admin.js

Lines changed: 60 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3348,9 +3348,19 @@ async function testTool(toolId) {
33483348
input.className =
33493349
"mt-1 block w-full rounded-md border border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 dark:bg-gray-900 text-gray-700 dark:text-gray-300 dark:border-gray-700 dark:focus:border-indigo-400 dark:focus:ring-indigo-400";
33503350

3351-
if (prop.items?.type === "number") {
3351+
const itemTypes = Array.isArray(prop.items?.anyOf)
3352+
? prop.items.anyOf.map((t) => t.type)
3353+
: [prop.items?.type];
3354+
3355+
if (
3356+
itemTypes.includes("number") ||
3357+
itemTypes.includes("integer")
3358+
) {
33523359
input.type = "number";
3353-
} else if (prop.items?.type === "boolean") {
3360+
input.step = itemTypes.includes("integer")
3361+
? "1"
3362+
: "any";
3363+
} else if (itemTypes.includes("boolean")) {
33543364
input.type = "checkbox";
33553365
input.value = "true";
33563366
input.checked = value === true || value === "true";
@@ -3390,9 +3400,16 @@ async function testTool(toolId) {
33903400
});
33913401

33923402
if (Array.isArray(prop.default)) {
3393-
prop.default.forEach((val) => {
3394-
arrayContainer.appendChild(createArrayInput(val));
3395-
});
3403+
if (prop.default.length > 0) {
3404+
prop.default.forEach((val) => {
3405+
arrayContainer.appendChild(
3406+
createArrayInput(val),
3407+
);
3408+
});
3409+
} else {
3410+
// Create one empty input for empty default arrays
3411+
arrayContainer.appendChild(createArrayInput());
3412+
}
33963413
} else {
33973414
arrayContainer.appendChild(createArrayInput());
33983415
}
@@ -3535,49 +3552,48 @@ async function runToolTest() {
35353552
const inputValues = formData.getAll(key);
35363553
try {
35373554
// Convert values based on the items schema type
3538-
if (prop.items && prop.items.type) {
3539-
switch (prop.items.type) {
3540-
case "object":
3541-
value = inputValues.map((v) => {
3542-
try {
3543-
const parsed = JSON.parse(v);
3544-
if (
3545-
typeof parsed !== "object" ||
3546-
Array.isArray(parsed)
3547-
) {
3548-
throw new Error(
3549-
`Value must be an object, got ${typeof parsed}`,
3550-
);
3551-
}
3552-
return parsed;
3553-
} catch (e) {
3554-
console.error(
3555-
`Error parsing object for ${key}:`,
3556-
e,
3557-
);
3555+
if (prop.items) {
3556+
const itemType = Array.isArray(prop.items.anyOf)
3557+
? prop.items.anyOf.map((t) => t.type)
3558+
: [prop.items.type];
3559+
3560+
if (
3561+
itemType.includes("number") ||
3562+
itemType.includes("integer")
3563+
) {
3564+
value = inputValues.map((v) => {
3565+
const num = Number(v);
3566+
if (isNaN(num)) {
3567+
throw new Error(`Invalid number: ${v}`);
3568+
}
3569+
return num;
3570+
});
3571+
} else if (itemType.includes("boolean")) {
3572+
value = inputValues.map(
3573+
(v) => v === "true" || v === true,
3574+
);
3575+
} else if (itemType.includes("object")) {
3576+
value = inputValues.map((v) => {
3577+
try {
3578+
const parsed = JSON.parse(v);
3579+
if (
3580+
typeof parsed !== "object" ||
3581+
Array.isArray(parsed)
3582+
) {
35583583
throw new Error(
3559-
`Invalid object format for ${key}. Each item must be a valid JSON object.`,
3584+
"Value must be an object",
35603585
);
35613586
}
3562-
});
3563-
break;
3564-
case "number":
3565-
value = inputValues.map((v) =>
3566-
v === "" ? null : Number(v),
3567-
);
3568-
break;
3569-
case "boolean":
3570-
value = inputValues.map(
3571-
(v) => v === "true" || v === true,
3572-
);
3573-
break;
3574-
default:
3575-
// For other types (like strings), use raw values
3576-
value = inputValues;
3587+
return parsed;
3588+
} catch {
3589+
throw new Error(
3590+
`Invalid object format for ${key}`,
3591+
);
3592+
}
3593+
});
3594+
} else {
3595+
value = inputValues;
35773596
}
3578-
} else {
3579-
// If no items type specified, use raw values
3580-
value = inputValues;
35813597
}
35823598

35833599
// Handle empty values

0 commit comments

Comments
 (0)