@@ -151,6 +151,36 @@ func TestJson_Structuring(t *testing.T) {
151151 require .Equal (t , "string" , s .Properties .Get ("first_name" ).Type .String ())
152152 },
153153 },
154+ {
155+ name : "$ref using in referenced file" ,
156+ test : func (t * testing.T ) {
157+ reader := & dynamictest.Reader {
158+ Data : map [string ]* dynamic.Config {
159+ "/bar.json" : {
160+ Info : dynamictest .NewConfigInfo (dynamictest .WithUrl ("/bar.json" )),
161+ Raw : []byte (`{
162+ "$defs": { "items": { "type": "integer" } },
163+ "type": "array",
164+ "items": { "$ref": "#/$defs/items" }
165+ }` ),
166+ },
167+ },
168+ }
169+
170+ foo := & dynamic.Config {
171+ Info : dynamictest .NewConfigInfo (dynamictest .WithUrl ("/foo.json" )),
172+ Data : & schema.Schema {
173+ Ref : "/bar.json" ,
174+ },
175+ }
176+
177+ err := foo .Data .(* schema.Schema ).Parse (foo , reader )
178+ require .NoError (t , err )
179+
180+ require .NoError (t , err )
181+ require .Equal (t , "integer" , foo .Data .(* schema.Schema ).Items .Type .String ())
182+ },
183+ },
154184 {
155185 name : "recursion" ,
156186 test : func (t * testing.T ) {
@@ -224,6 +254,40 @@ $ref: '#/$defs/a'
224254 require .Equal (t , "string" , person .Data .(* schema.Schema ).Items .Type .String ())
225255 },
226256 },
257+ {
258+ name : "parsing twice with $refs" ,
259+ test : func (t * testing.T ) {
260+ reader := & dynamictest.Reader {
261+ Data : map [string ]* dynamic.Config {
262+ "https://example.com/schemas/foo" : {
263+ Info : dynamictest .NewConfigInfo (dynamictest .WithUrl ("https://example.com/schemas/foo" )),
264+ Raw : []byte (`{
265+ "$defs": { "items": { "type": "integer" } },
266+ "type": "array",
267+ "items": { "$ref": "#/$defs/items" },
268+ }` ),
269+ },
270+ },
271+ }
272+
273+ person := & dynamic.Config {
274+ Info : dynamictest .NewConfigInfo (dynamictest .WithUrl ("https://example.com/schemas/bar" )),
275+ Data : & schema.Schema {
276+ Ref : "https://example.com/schemas/foo" ,
277+ },
278+ }
279+
280+ // 1. parsing
281+ err := person .Data .(* schema.Schema ).Parse (person , reader )
282+ require .NoError (t , err )
283+
284+ // 2. parsing
285+ err = person .Data .(* schema.Schema ).Parse (person , reader )
286+
287+ require .NoError (t , err )
288+ require .Equal (t , "integer" , person .Data .(* schema.Schema ).Items .Type .String ())
289+ },
290+ },
227291 }
228292
229293 t .Parallel ()
0 commit comments