@@ -2675,3 +2675,81 @@ func TestSchemaDynamicValue_Hash_IsB(t *testing.T) {
26752675 assert .False (t , value .IsA ())
26762676 assert .True (t , value .IsB ())
26772677}
2678+
2679+ // TestSchema_Id tests that the $id field is correctly extracted and included in the hash
2680+ func TestSchema_Id (t * testing.T ) {
2681+ yml := `type: object
2682+ $id: "https://example.com/schemas/pet.json"
2683+ description: A pet schema`
2684+
2685+ var idxNode yaml.Node
2686+ _ = yaml .Unmarshal ([]byte (yml ), & idxNode )
2687+
2688+ var sch Schema
2689+ err := low .BuildModel (idxNode .Content [0 ], & sch )
2690+ assert .NoError (t , err )
2691+
2692+ err = sch .Build (context .Background (), idxNode .Content [0 ], nil )
2693+ assert .NoError (t , err )
2694+
2695+ assert .Equal (t , "https://example.com/schemas/pet.json" , sch .Id .Value )
2696+ assert .NotNil (t , sch .Id .KeyNode )
2697+ assert .NotNil (t , sch .Id .ValueNode )
2698+ }
2699+
2700+ // TestSchema_Id_Hash tests that $id is included in the schema hash
2701+ func TestSchema_Id_Hash (t * testing.T ) {
2702+ yml1 := `type: object
2703+ $id: "https://example.com/schemas/a.json"
2704+ description: Schema A`
2705+
2706+ yml2 := `type: object
2707+ $id: "https://example.com/schemas/b.json"
2708+ description: Schema A`
2709+
2710+ yml3 := `type: object
2711+ description: Schema A`
2712+
2713+ var node1 , node2 , node3 yaml.Node
2714+ _ = yaml .Unmarshal ([]byte (yml1 ), & node1 )
2715+ _ = yaml .Unmarshal ([]byte (yml2 ), & node2 )
2716+ _ = yaml .Unmarshal ([]byte (yml3 ), & node3 )
2717+
2718+ var sch1 , sch2 , sch3 Schema
2719+ _ = low .BuildModel (node1 .Content [0 ], & sch1 )
2720+ _ = sch1 .Build (context .Background (), node1 .Content [0 ], nil )
2721+
2722+ _ = low .BuildModel (node2 .Content [0 ], & sch2 )
2723+ _ = sch2 .Build (context .Background (), node2 .Content [0 ], nil )
2724+
2725+ _ = low .BuildModel (node3 .Content [0 ], & sch3 )
2726+ _ = sch3 .Build (context .Background (), node3 .Content [0 ], nil )
2727+
2728+ hash1 := sch1 .Hash ()
2729+ hash2 := sch2 .Hash ()
2730+ hash3 := sch3 .Hash ()
2731+
2732+ // Different $id values should produce different hashes
2733+ assert .NotEqual (t , hash1 , hash2 )
2734+ // Schema without $id should differ from schema with $id
2735+ assert .NotEqual (t , hash1 , hash3 )
2736+ assert .NotEqual (t , hash2 , hash3 )
2737+ }
2738+
2739+ // TestSchema_Id_Empty tests that empty $id is not set
2740+ func TestSchema_Id_Empty (t * testing.T ) {
2741+ yml := `type: object
2742+ description: A schema without $id`
2743+
2744+ var idxNode yaml.Node
2745+ _ = yaml .Unmarshal ([]byte (yml ), & idxNode )
2746+
2747+ var sch Schema
2748+ err := low .BuildModel (idxNode .Content [0 ], & sch )
2749+ assert .NoError (t , err )
2750+
2751+ err = sch .Build (context .Background (), idxNode .Content [0 ], nil )
2752+ assert .NoError (t , err )
2753+
2754+ assert .True (t , sch .Id .IsEmpty ())
2755+ }
0 commit comments