Skip to content

Commit 92e01dc

Browse files
committed
fix: allow empty elicitation form data when all fields are optional
1 parent dff91a8 commit 92e01dc

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

client/src/utils/__tests__/schemaUtils.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ describe("generateDefaultValue", () => {
4343
expect(generateDefaultValue({ type: "array" })).toBe(undefined);
4444
});
4545

46-
test("generates undefined for optional object", () => {
47-
expect(generateDefaultValue({ type: "object" })).toBe(undefined);
46+
test("generates empty object for optional root object", () => {
47+
expect(generateDefaultValue({ type: "object" })).toEqual({});
4848
});
4949

5050
test("generates default null for unknown types", () => {

client/src/utils/schemaUtils.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export function generateDefaultValue(
100100
propertyName && parentSchema
101101
? isPropertyRequired(propertyName, parentSchema)
102102
: false;
103+
const isRootSchema = propertyName === undefined && parentSchema === undefined;
103104

104105
switch (schema.type) {
105106
case "string":
@@ -112,7 +113,9 @@ export function generateDefaultValue(
112113
case "array":
113114
return isRequired ? [] : undefined;
114115
case "object": {
115-
if (!schema.properties) return isRequired ? {} : undefined;
116+
if (!schema.properties) {
117+
return isRequired || isRootSchema ? {} : undefined;
118+
}
116119

117120
const obj: JsonObject = {};
118121
// Only include properties that are required according to the schema's required array
@@ -124,7 +127,11 @@ export function generateDefaultValue(
124127
}
125128
}
126129
});
127-
return isRequired ? obj : Object.keys(obj).length > 0 ? obj : undefined;
130+
131+
if (Object.keys(obj).length === 0) {
132+
return isRequired || isRootSchema ? {} : undefined;
133+
}
134+
return obj;
128135
}
129136
case "null":
130137
return null;

0 commit comments

Comments
 (0)