@@ -32,6 +32,10 @@ import (
3232 "github.com/stretchr/testify/assert"
3333)
3434
35+ var (
36+ rex = regexp .MustCompile (`"\$ref":\s*"(.+)"` )
37+ )
38+
3539func jsonDoc (path string ) (json.RawMessage , error ) {
3640 data , err := swag .LoadFromFileOrHTTP (path )
3741 if err != nil {
@@ -286,6 +290,39 @@ func TestExportedResponseExpansion(t *testing.T) {
286290 // assert.Equal(t, expected, resp)
287291}
288292
293+ func TestExpandResponseAndParamWithRoot (t * testing.T ) {
294+ specDoc , err := jsonDoc ("fixtures/bugs/1614/gitea.json" )
295+ if ! assert .NoError (t , err ) {
296+ t .FailNow ()
297+ return
298+ }
299+ var spec Swagger
300+ _ = json .Unmarshal (specDoc , & spec )
301+
302+ // check responses with $ref
303+ resp := spec .Paths .Paths ["/admin/users" ].Post .Responses .StatusCodeResponses [201 ]
304+ err = ExpandResponseWithRoot (& resp , spec , nil )
305+ assert .NoError (t , err )
306+ jazon , _ := json .MarshalIndent (resp , "" , " " )
307+ m := rex .FindAllStringSubmatch (string (jazon ), - 1 )
308+ assert .Nil (t , m )
309+
310+ resp = spec .Paths .Paths ["/admin/users" ].Post .Responses .StatusCodeResponses [403 ]
311+ err = ExpandResponseWithRoot (& resp , spec , nil )
312+ assert .NoError (t , err )
313+ jazon , _ = json .MarshalIndent (resp , "" , " " )
314+ m = rex .FindAllStringSubmatch (string (jazon ), - 1 )
315+ assert .Nil (t , m )
316+
317+ // check param with $ref
318+ param := spec .Paths .Paths ["/admin/users" ].Post .Parameters [0 ]
319+ err = ExpandParameterWithRoot (& param , spec , nil )
320+ assert .NoError (t , err )
321+ jazon , _ = json .MarshalIndent (param , "" , " " )
322+ m = rex .FindAllStringSubmatch (string (jazon ), - 1 )
323+ assert .Nil (t , m )
324+ }
325+
289326func TestIssue3 (t * testing.T ) {
290327 spec := new (Swagger )
291328 specDoc , err := jsonDoc ("fixtures/expansion/overflow.json" )
@@ -428,7 +465,6 @@ func Test_MoreCircular(t *testing.T) {
428465
429466 fixturePath := "fixtures/more_circulars/spec.json"
430467 jazon := expandThisOrDieTrying (t , fixturePath )
431- rex := regexp .MustCompile (`"\$ref":\s*"(.+)"` )
432468 m := rex .FindAllStringSubmatch (jazon , - 1 )
433469 if assert .NotNil (t , m ) {
434470 for _ , matched := range m {
@@ -440,7 +476,6 @@ func Test_MoreCircular(t *testing.T) {
440476
441477 fixturePath = "fixtures/more_circulars/spec2.json"
442478 jazon = expandThisOrDieTrying (t , fixturePath )
443- rex = regexp .MustCompile (`"\$ref":\s*"(.+)"` )
444479 m = rex .FindAllStringSubmatch (jazon , - 1 )
445480 if assert .NotNil (t , m ) {
446481 for _ , matched := range m {
@@ -452,7 +487,6 @@ func Test_MoreCircular(t *testing.T) {
452487
453488 fixturePath = "fixtures/more_circulars/spec3.json"
454489 jazon = expandThisOrDieTrying (t , fixturePath )
455- rex = regexp .MustCompile (`"\$ref":\s*"(.+)"` )
456490 m = rex .FindAllStringSubmatch (jazon , - 1 )
457491 if assert .NotNil (t , m ) {
458492 for _ , matched := range m {
@@ -464,7 +498,6 @@ func Test_MoreCircular(t *testing.T) {
464498
465499 fixturePath = "fixtures/more_circulars/spec4.json"
466500 jazon = expandThisOrDieTrying (t , fixturePath )
467- rex = regexp .MustCompile (`"\$ref":\s*"(.+)"` )
468501 m = rex .FindAllStringSubmatch (jazon , - 1 )
469502 if assert .NotNil (t , m ) {
470503 for _ , matched := range m {
@@ -481,7 +514,6 @@ func Test_Issue957(t *testing.T) {
481514 if assert .NotEmpty (t , jazon ) {
482515 assert .NotContainsf (t , jazon , "fixture-957.json#/" ,
483516 "expected %s to be expanded with stripped circular $ref" , fixturePath )
484- rex := regexp .MustCompile (`"\$ref":\s*"(.+)"` )
485517 m := rex .FindAllStringSubmatch (jazon , - 1 )
486518 if assert .NotNil (t , m ) {
487519 for _ , matched := range m {
@@ -499,7 +531,6 @@ func Test_Bitbucket(t *testing.T) {
499531
500532 fixturePath := "fixtures/more_circulars/bitbucket.json"
501533 jazon := expandThisOrDieTrying (t , fixturePath )
502- rex := regexp .MustCompile (`"\$ref":\s*"(.+)"` )
503534 m := rex .FindAllStringSubmatch (jazon , - 1 )
504535 if assert .NotNil (t , m ) {
505536 for _ , matched := range m {
@@ -514,7 +545,6 @@ func Test_ExpandJSONSchemaDraft4(t *testing.T) {
514545 fixturePath := filepath .Join ("schemas" , "jsonschema-draft-04.json" )
515546 jazon := expandThisSchemaOrDieTrying (t , fixturePath )
516547 // assert all $ref maches "$ref": "http://json-schema.org/draft-04/something"
517- rex := regexp .MustCompile (`"\$ref":\s*"(.+)"` )
518548 m := rex .FindAllStringSubmatch (jazon , - 1 )
519549 if assert .NotNil (t , m ) {
520550 for _ , matched := range m {
@@ -529,7 +559,6 @@ func Test_ExpandSwaggerSchema(t *testing.T) {
529559 fixturePath := filepath .Join ("schemas" , "v2" , "schema.json" )
530560 jazon := expandThisSchemaOrDieTrying (t , fixturePath )
531561 // assert all $ref maches "$ref": "#/definitions/something"
532- rex := regexp .MustCompile (`"\$ref":\s*"(.+)"` )
533562 m := rex .FindAllStringSubmatch (jazon , - 1 )
534563 if assert .NotNil (t , m ) {
535564 for _ , matched := range m {
@@ -1412,6 +1441,77 @@ func TestResolveForTransitiveRefs(t *testing.T) {
14121441 assert .NoError (t , err )
14131442}
14141443
1444+ const (
1445+ withoutSchemaID = "removed"
1446+ withSchemaID = "schema"
1447+ )
1448+
1449+ func TestExpandSchemaWithRoot (t * testing.T ) {
1450+ root := new (Swagger )
1451+ _ = json .Unmarshal (PetStoreJSONMessage , root )
1452+
1453+ // 1. remove ID from root definition
1454+ origPet := root .Definitions ["Pet" ]
1455+ newPet := origPet
1456+ newPet .ID = ""
1457+ root .Definitions ["Pet" ] = newPet
1458+ expandRootWithID (t , root , withoutSchemaID )
1459+
1460+ // 2. put back ID in Pet definition
1461+ // nested $ref should fail
1462+ //Debug = true
1463+ root .Definitions ["Pet" ] = origPet
1464+ expandRootWithID (t , root , withSchemaID )
1465+ }
1466+
1467+ func expandRootWithID (t * testing.T , root * Swagger , testcase string ) {
1468+ t .Logf ("case: expanding $ref to schema without ID, with nested $ref with %s ID" , testcase )
1469+ sch := & Schema {
1470+ SchemaProps : SchemaProps {
1471+ Ref : MustCreateRef ("#/definitions/newPet" ),
1472+ },
1473+ }
1474+ err := ExpandSchema (sch , root , nil )
1475+ if testcase == withSchemaID {
1476+ assert .Errorf (t , err , "expected %s NOT to expand properly because of the ID in the parent schema" , sch .Ref .String ())
1477+ } else {
1478+ assert .NoErrorf (t , err , "expected %s to expand properly" , sch .Ref .String ())
1479+ }
1480+ if Debug {
1481+ bbb , _ := json .MarshalIndent (sch , "" , " " )
1482+ t .Log (string (bbb ))
1483+ }
1484+
1485+ t .Log ("case: expanding $ref to schema without nested $ref" )
1486+ sch = & Schema {
1487+ SchemaProps : SchemaProps {
1488+ Ref : MustCreateRef ("#/definitions/Category" ),
1489+ },
1490+ }
1491+ err = ExpandSchema (sch , root , nil )
1492+ assert .NoErrorf (t , err , "expected %s to expand properly" , sch .Ref .String ())
1493+ if Debug {
1494+ bbb , _ := json .MarshalIndent (sch , "" , " " )
1495+ t .Log (string (bbb ))
1496+ }
1497+ t .Logf ("case: expanding $ref to schema with %s ID and nested $ref" , testcase )
1498+ sch = & Schema {
1499+ SchemaProps : SchemaProps {
1500+ Ref : MustCreateRef ("#/definitions/Pet" ),
1501+ },
1502+ }
1503+ err = ExpandSchema (sch , root , nil )
1504+ if testcase == withSchemaID {
1505+ assert .Errorf (t , err , "expected %s NOT to expand properly because of the ID in the parent schema" , sch .Ref .String ())
1506+ } else {
1507+ assert .NoErrorf (t , err , "expected %s to expand properly" , sch .Ref .String ())
1508+ }
1509+ if Debug {
1510+ bbb , _ := json .MarshalIndent (sch , "" , " " )
1511+ t .Log (string (bbb ))
1512+ }
1513+ }
1514+
14151515// PetStoreJSONMessage json raw message for Petstore20
14161516var PetStoreJSONMessage = json .RawMessage ([]byte (PetStore20 ))
14171517
0 commit comments