@@ -213,7 +213,19 @@ func expandSchema(target Schema, parentRefs []string, resolver *schemaLoader, ba
213213 }
214214
215215 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
217229 }
218230
219231 for k := range target .Definitions {
@@ -525,21 +537,25 @@ func getRefAndSchema(input interface{}) (*Ref, *Schema, error) {
525537}
526538
527539func expandParameterOrResponse (input interface {}, resolver * schemaLoader , basePath string ) error {
528- ref , _ , err := getRefAndSchema (input )
540+ ref , sch , err := getRefAndSchema (input )
529541 if err != nil {
530542 return err
531543 }
532544
533- if ref == nil {
545+ if ref == nil && sch == nil { // nothing to do
534546 return nil
535547 }
536548
537549 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 )
540557 }
541558
542- ref , sch , _ := getRefAndSchema (input )
543559 if ref .String () != "" {
544560 transitiveResolver := resolver .transitiveResolver (basePath , * ref )
545561 basePath = resolver .updateBasePath (transitiveResolver , basePath )
@@ -551,6 +567,7 @@ func expandParameterOrResponse(input interface{}, resolver *schemaLoader, basePa
551567 if ref != nil {
552568 * ref = Ref {}
553569 }
570+
554571 return nil
555572 }
556573
@@ -560,38 +577,29 @@ func expandParameterOrResponse(input interface{}, resolver *schemaLoader, basePa
560577 return ern
561578 }
562579
563- switch {
564- case resolver .isCircular (& rebasedRef , basePath , parentRefs ... ):
580+ if resolver .isCircular (& rebasedRef , basePath , parentRefs ... ) {
565581 // this is a circular $ref: stop expansion
566582 if ! resolver .options .AbsoluteCircularRef {
567583 sch .Ref = denormalizeRef (& rebasedRef , resolver .context .basePath , resolver .context .rootID )
568584 } else {
569585 sch .Ref = rebasedRef
570586 }
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 )
578587 }
579588 }
580589
590+ // $ref expansion or rebasing is performed by expandSchema below
581591 if ref != nil {
582592 * ref = Ref {}
583593 }
584594
585595 // 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
595603 * sch = * s
596604 }
597605
0 commit comments