@@ -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" , ( ) => {
0 commit comments