Skip to content

Commit 5778a3a

Browse files
authored
Merge pull request #926 from bolinfest/sapling-pr-archive-bolinfest
fix: allow empty elicitation form data when all fields are optional
2 parents bce61b2 + 4b20f15 commit 5778a3a

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,38 @@ 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({});
48+
});
49+
50+
test("generates undefined for nested optional object", () => {
51+
// When called WITH propertyName and parentSchema, and the property is NOT required,
52+
// nested optional objects should return undefined
53+
const parentSchema = {
54+
type: "object" as const,
55+
required: ["otherField"],
56+
properties: {
57+
optionalObject: { type: "object" as const },
58+
otherField: { type: "string" as const },
59+
},
60+
};
61+
expect(
62+
generateDefaultValue({ type: "object" }, "optionalObject", parentSchema),
63+
).toBe(undefined);
64+
});
65+
66+
test("generates empty object for root-level object with all optional properties", () => {
67+
// Root-level schema with properties but no required array
68+
// This is the exact scenario from PR #926 - elicitation with all optional fields
69+
const schema: JsonSchemaType = {
70+
type: "object",
71+
properties: {
72+
optionalField1: { type: "string" },
73+
optionalField2: { type: "number" },
74+
},
75+
// No required array - all fields are optional
76+
};
77+
expect(generateDefaultValue(schema)).toEqual({});
4878
});
4979

5080
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
// Include required properties OR optional properties that declare a default
@@ -126,7 +129,11 @@ export function generateDefaultValue(
126129
}
127130
}
128131
});
129-
return isRequired ? obj : Object.keys(obj).length > 0 ? obj : undefined;
132+
133+
if (Object.keys(obj).length === 0) {
134+
return isRequired || isRootSchema ? {} : undefined;
135+
}
136+
return obj;
130137
}
131138
case "null":
132139
return null;

0 commit comments

Comments
 (0)