Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions client/src/utils/__tests__/schemaUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,38 @@ describe("generateDefaultValue", () => {
expect(generateDefaultValue({ type: "array" })).toBe(undefined);
});

test("generates undefined for optional object", () => {
expect(generateDefaultValue({ type: "object" })).toBe(undefined);
test("generates empty object for optional root object", () => {
expect(generateDefaultValue({ type: "object" })).toEqual({});
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to add tests for these cases as well?


  test("generates undefined for nested optional object", () => {
    // When called WITH propertyName and parentSchema, and the property is NOT required,
    // nested optional objects should return undefined
    const parentSchema = {
      type: "object" as const,
      required: ["otherField"],
      properties: {
        optionalObject: { type: "object" as const },
        otherField: { type: "string" as const },
      },
    };
    expect(
      generateDefaultValue({ type: "object" }, "optionalObject", parentSchema),
    ).toBe(undefined);
  });

  test("generates empty object for root-level object with all optional properties", () => {
    // Root-level schema with properties but no required array
    // This is the exact scenario from PR #926 - elicitation with all optional fields
    const schema: JsonSchemaType = {
      type: "object",
      properties: {
        optionalField1: { type: "string" },
        optionalField2: { type: "number" },
      },
      // No required array - all fields are optional
    };
    expect(generateDefaultValue(schema)).toEqual({});
  });


test("generates undefined for nested optional object", () => {
// When called WITH propertyName and parentSchema, and the property is NOT required,
// nested optional objects should return undefined
const parentSchema = {
type: "object" as const,
required: ["otherField"],
properties: {
optionalObject: { type: "object" as const },
otherField: { type: "string" as const },
},
};
expect(
generateDefaultValue({ type: "object" }, "optionalObject", parentSchema),
).toBe(undefined);
});

test("generates empty object for root-level object with all optional properties", () => {
// Root-level schema with properties but no required array
// This is the exact scenario from PR #926 - elicitation with all optional fields
const schema: JsonSchemaType = {
type: "object",
properties: {
optionalField1: { type: "string" },
optionalField2: { type: "number" },
},
// No required array - all fields are optional
};
expect(generateDefaultValue(schema)).toEqual({});
});

test("generates default null for unknown types", () => {
Expand Down
11 changes: 9 additions & 2 deletions client/src/utils/schemaUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export function generateDefaultValue(
propertyName && parentSchema
? isPropertyRequired(propertyName, parentSchema)
: false;
const isRootSchema = propertyName === undefined && parentSchema === undefined;

switch (schema.type) {
case "string":
Expand All @@ -112,7 +113,9 @@ export function generateDefaultValue(
case "array":
return isRequired ? [] : undefined;
case "object": {
if (!schema.properties) return isRequired ? {} : undefined;
if (!schema.properties) {
return isRequired || isRootSchema ? {} : undefined;
}

const obj: JsonObject = {};
// Only include properties that are required according to the schema's required array
Expand All @@ -124,7 +127,11 @@ export function generateDefaultValue(
}
}
});
return isRequired ? obj : Object.keys(obj).length > 0 ? obj : undefined;

if (Object.keys(obj).length === 0) {
return isRequired || isRootSchema ? {} : undefined;
}
return obj;
}
case "null":
return null;
Expand Down