@@ -213,7 +213,19 @@ func expandSchema(target Schema, parentRefs []string, resolver *schemaLoader, ba
213
213
}
214
214
215
215
if target .Ref .String () != "" {
216
- return expandSchemaRef (target , parentRefs , resolver , basePath )
216
+ if ! resolver .options .SkipSchemas {
217
+ return expandSchemaRef (target , parentRefs , resolver , basePath )
218
+ }
219
+
220
+ // when "expand" with SkipSchema, we just rebase the existing $ref without replacing
221
+ // the full schema.
222
+ rebasedRef , err := NewRef (normalizeURI (target .Ref .String (), basePath ))
223
+ if err != nil {
224
+ return nil , err
225
+ }
226
+ target .Ref = denormalizeRef (& rebasedRef , resolver .context .basePath , resolver .context .rootID )
227
+
228
+ return & target , nil
217
229
}
218
230
219
231
for k := range target .Definitions {
@@ -525,21 +537,25 @@ func getRefAndSchema(input interface{}) (*Ref, *Schema, error) {
525
537
}
526
538
527
539
func expandParameterOrResponse (input interface {}, resolver * schemaLoader , basePath string ) error {
528
- ref , _ , err := getRefAndSchema (input )
540
+ ref , sch , err := getRefAndSchema (input )
529
541
if err != nil {
530
542
return err
531
543
}
532
544
533
- if ref == nil {
545
+ if ref == nil && sch == nil { // nothing to do
534
546
return nil
535
547
}
536
548
537
549
parentRefs := make ([]string , 0 , 10 )
538
- if err = resolver .deref (input , parentRefs , basePath ); resolver .shouldStopOnError (err ) {
539
- return err
550
+ if ref != nil {
551
+ // dereference this $ref
552
+ if err = resolver .deref (input , parentRefs , basePath ); resolver .shouldStopOnError (err ) {
553
+ return err
554
+ }
555
+
556
+ ref , sch , _ = getRefAndSchema (input )
540
557
}
541
558
542
- ref , sch , _ := getRefAndSchema (input )
543
559
if ref .String () != "" {
544
560
transitiveResolver := resolver .transitiveResolver (basePath , * ref )
545
561
basePath = resolver .updateBasePath (transitiveResolver , basePath )
@@ -551,6 +567,7 @@ func expandParameterOrResponse(input interface{}, resolver *schemaLoader, basePa
551
567
if ref != nil {
552
568
* ref = Ref {}
553
569
}
570
+
554
571
return nil
555
572
}
556
573
@@ -560,38 +577,29 @@ func expandParameterOrResponse(input interface{}, resolver *schemaLoader, basePa
560
577
return ern
561
578
}
562
579
563
- switch {
564
- case resolver .isCircular (& rebasedRef , basePath , parentRefs ... ):
580
+ if resolver .isCircular (& rebasedRef , basePath , parentRefs ... ) {
565
581
// this is a circular $ref: stop expansion
566
582
if ! resolver .options .AbsoluteCircularRef {
567
583
sch .Ref = denormalizeRef (& rebasedRef , resolver .context .basePath , resolver .context .rootID )
568
584
} else {
569
585
sch .Ref = rebasedRef
570
586
}
571
- case ! resolver .options .SkipSchemas :
572
- // schema expanded to a $ref in another root
573
- sch .Ref = rebasedRef
574
- debugLog ("rebased to: %s" , sch .Ref .String ())
575
- default :
576
- // skip schema expansion but rebase $ref to schema
577
- sch .Ref = denormalizeRef (& rebasedRef , resolver .context .basePath , resolver .context .rootID )
578
587
}
579
588
}
580
589
590
+ // $ref expansion or rebasing is performed by expandSchema below
581
591
if ref != nil {
582
592
* ref = Ref {}
583
593
}
584
594
585
595
// expand schema
586
- if ! resolver .options .SkipSchemas {
587
- s , err := expandSchema (* sch , parentRefs , resolver , basePath )
588
- if resolver .shouldStopOnError (err ) {
589
- return err
590
- }
591
- if s == nil {
592
- // guard for when continuing on error
593
- return nil
594
- }
596
+ // yes, we do it even if options.SkipSchema is true: we have to go down that rabbit hole and rebase nested $ref)
597
+ s , err := expandSchema (* sch , parentRefs , resolver , basePath )
598
+ if resolver .shouldStopOnError (err ) {
599
+ return err
600
+ }
601
+
602
+ if s != nil { // guard for when continuing on error
595
603
* sch = * s
596
604
}
597
605
0 commit comments