@@ -1055,3 +1055,162 @@ func TestJSONSchema_ParentLinks(t *testing.T) {
10551055 }, "SetTopLevelParent on nil schema should not panic" )
10561056 })
10571057}
1058+
1059+ // Test GetReferenceChain method
1060+ func TestJSONSchema_GetReferenceChain (t * testing.T ) {
1061+ t .Parallel ()
1062+
1063+ t .Run ("nil schema returns nil" , func (t * testing.T ) {
1064+ t .Parallel ()
1065+ var nilSchema * JSONSchema [Referenceable ]
1066+ assert .Nil (t , nilSchema .GetReferenceChain (), "nil schema GetReferenceChain should return nil" )
1067+ })
1068+
1069+ t .Run ("schema with nil parent returns nil" , func (t * testing.T ) {
1070+ t .Parallel ()
1071+ schema := createSimpleSchema ()
1072+ assert .Nil (t , schema .GetReferenceChain (), "schema with nil parent should return nil from GetReferenceChain" )
1073+ })
1074+
1075+ t .Run ("schema with non-reference parent returns empty chain" , func (t * testing.T ) {
1076+ t .Parallel ()
1077+ // Create parent that is NOT a reference (just a regular schema)
1078+ nonRefParent := createSimpleSchema ()
1079+
1080+ // Create child with parent set
1081+ childSchema := createSimpleSchema ()
1082+ childSchema .SetParent (nonRefParent )
1083+
1084+ // Chain should be empty (not nil) since parent exists but isn't a reference
1085+ chain := childSchema .GetReferenceChain ()
1086+ assert .Empty (t , chain , "schema with non-reference parent should return empty chain" )
1087+ })
1088+
1089+ t .Run ("schema with reference parent returns single-entry chain" , func (t * testing.T ) {
1090+ t .Parallel ()
1091+ // Create parent that IS a reference
1092+ refParent := createSchemaWithRef ("#/components/schemas/Parent" )
1093+
1094+ // Create child with parent set
1095+ childSchema := createSimpleSchema ()
1096+ childSchema .SetParent (refParent )
1097+
1098+ chain := childSchema .GetReferenceChain ()
1099+ require .Len (t , chain , 1 , "schema with reference parent should return single-entry chain" )
1100+ assert .Equal (t , "#/components/schemas/Parent" , string (chain [0 ].Reference ))
1101+ assert .Equal (t , refParent , chain [0 ].Schema )
1102+ })
1103+
1104+ t .Run ("schema with mixed parent chain filters non-references" , func (t * testing.T ) {
1105+ t .Parallel ()
1106+ // Create a chain: refGrandparent -> nonRefParent -> child
1107+ // Only refGrandparent should appear in the chain
1108+
1109+ refGrandparent := createSchemaWithRef ("#/components/schemas/Grandparent" )
1110+ nonRefParent := createSimpleSchema ()
1111+ childSchema := createSimpleSchema ()
1112+
1113+ // Set up the chain
1114+ nonRefParent .SetParent (refGrandparent )
1115+ childSchema .SetParent (nonRefParent )
1116+
1117+ chain := childSchema .GetReferenceChain ()
1118+ require .Len (t , chain , 1 , "chain should only include reference parents" )
1119+ assert .Equal (t , "#/components/schemas/Grandparent" , string (chain [0 ].Reference ))
1120+ })
1121+
1122+ t .Run ("schema with multiple reference ancestors returns full chain" , func (t * testing.T ) {
1123+ t .Parallel ()
1124+ // Create a chain: refGrandparent -> refParent -> child
1125+
1126+ refGrandparent := createSchemaWithRef ("#/components/schemas/Grandparent" )
1127+ refParent := createSchemaWithRef ("#/components/schemas/Parent" )
1128+ childSchema := createSimpleSchema ()
1129+
1130+ // Set up the chain
1131+ refParent .SetParent (refGrandparent )
1132+ childSchema .SetParent (refParent )
1133+
1134+ chain := childSchema .GetReferenceChain ()
1135+ require .Len (t , chain , 2 , "chain should include both reference ancestors" )
1136+ // Chain is outer -> inner order (grandparent first, parent last)
1137+ assert .Equal (t , "#/components/schemas/Grandparent" , string (chain [0 ].Reference ))
1138+ assert .Equal (t , "#/components/schemas/Parent" , string (chain [1 ].Reference ))
1139+ })
1140+ }
1141+
1142+ // Test GetImmediateReference method
1143+ func TestJSONSchema_GetImmediateReference (t * testing.T ) {
1144+ t .Parallel ()
1145+
1146+ t .Run ("nil schema returns nil" , func (t * testing.T ) {
1147+ t .Parallel ()
1148+ var nilSchema * JSONSchema [Referenceable ]
1149+ assert .Nil (t , nilSchema .GetImmediateReference (), "nil schema GetImmediateReference should return nil" )
1150+ })
1151+
1152+ t .Run ("schema with nil parent returns nil" , func (t * testing.T ) {
1153+ t .Parallel ()
1154+ schema := createSimpleSchema ()
1155+ assert .Nil (t , schema .GetImmediateReference (), "schema with nil parent should return nil" )
1156+ })
1157+
1158+ t .Run ("schema with non-reference parent returns nil" , func (t * testing.T ) {
1159+ t .Parallel ()
1160+ nonRefParent := createSimpleSchema ()
1161+ childSchema := createSimpleSchema ()
1162+ childSchema .SetParent (nonRefParent )
1163+
1164+ assert .Nil (t , childSchema .GetImmediateReference (), "schema with non-reference parent should return nil" )
1165+ })
1166+
1167+ t .Run ("schema with reference parent returns entry" , func (t * testing.T ) {
1168+ t .Parallel ()
1169+ refParent := createSchemaWithRef ("#/components/schemas/Parent" )
1170+ childSchema := createSimpleSchema ()
1171+ childSchema .SetParent (refParent )
1172+
1173+ entry := childSchema .GetImmediateReference ()
1174+ require .NotNil (t , entry , "should return entry for reference parent" )
1175+ assert .Equal (t , "#/components/schemas/Parent" , string (entry .Reference ))
1176+ assert .Equal (t , refParent , entry .Schema )
1177+ })
1178+ }
1179+
1180+ // Test GetTopLevelReference method
1181+ func TestJSONSchema_GetTopLevelReference (t * testing.T ) {
1182+ t .Parallel ()
1183+
1184+ t .Run ("nil schema returns nil" , func (t * testing.T ) {
1185+ t .Parallel ()
1186+ var nilSchema * JSONSchema [Referenceable ]
1187+ assert .Nil (t , nilSchema .GetTopLevelReference (), "nil schema GetTopLevelReference should return nil" )
1188+ })
1189+
1190+ t .Run ("schema with nil topLevelParent returns nil" , func (t * testing.T ) {
1191+ t .Parallel ()
1192+ schema := createSimpleSchema ()
1193+ assert .Nil (t , schema .GetTopLevelReference (), "schema with nil topLevelParent should return nil" )
1194+ })
1195+
1196+ t .Run ("schema with non-reference topLevelParent returns nil" , func (t * testing.T ) {
1197+ t .Parallel ()
1198+ nonRefTopLevel := createSimpleSchema ()
1199+ childSchema := createSimpleSchema ()
1200+ childSchema .SetTopLevelParent (nonRefTopLevel )
1201+
1202+ assert .Nil (t , childSchema .GetTopLevelReference (), "schema with non-reference topLevelParent should return nil" )
1203+ })
1204+
1205+ t .Run ("schema with reference topLevelParent returns entry" , func (t * testing.T ) {
1206+ t .Parallel ()
1207+ refTopLevel := createSchemaWithRef ("#/components/schemas/TopLevel" )
1208+ childSchema := createSimpleSchema ()
1209+ childSchema .SetTopLevelParent (refTopLevel )
1210+
1211+ entry := childSchema .GetTopLevelReference ()
1212+ require .NotNil (t , entry , "should return entry for reference topLevelParent" )
1213+ assert .Equal (t , "#/components/schemas/TopLevel" , string (entry .Reference ))
1214+ assert .Equal (t , refTopLevel , entry .Schema )
1215+ })
1216+ }
0 commit comments