Skip to content

Commit

Permalink
Fixed other references to "root.yaml"
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin Lobo committed Aug 28, 2024
1 parent e4a49c0 commit f55c151
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
12 changes: 12 additions & 0 deletions index/index_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log/slog"
"net/http"
"net/url"
"path/filepath"
"sync"

"github.com/pb33f/libopenapi/datamodel"
Expand Down Expand Up @@ -154,6 +155,17 @@ type SpecIndexConfig struct {
uri []string
}

// SetTheoreticalRoot sets the spec file paths to a theoretical file path
func (s *SpecIndexConfig) SetTheoreticalRoot() {
s.SpecFilePath = filepath.Join(s.BasePath, theoreticalRoot)

basePath := s.BasePath
if !filepath.IsAbs(basePath) {
basePath, _ = filepath.Abs(basePath)
}
s.SpecAbsolutePath = filepath.Join(basePath, theoreticalRoot)
}

// CreateOpenAPIIndexConfig is a helper function to create a new SpecIndexConfig with the AllowRemoteLookup and
// AllowFileLookup set to true. This is the default behaviour of the index in previous versions of libopenapi. (pre 0.6.0)
//
Expand Down
10 changes: 4 additions & 6 deletions index/rolodex.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func (r *Rolodex) IndexTheRolodex() error {
// indexed and built every supporting file, we can build the root index (our entry point)
if r.rootNode != nil {

// if there is a base path, then we need to set the root spec config to point to a theoretical root.yaml
// if there is a base path but no SpecFilePath, then we need to set the root spec config to point to a theoretical root.yaml
// which does not exist, but is used to formulate the absolute path to root references correctly.
if r.indexConfig.BasePath != "" && r.indexConfig.BaseURL == nil {

Expand All @@ -313,13 +313,11 @@ func (r *Rolodex) IndexTheRolodex() error {
}

if len(r.localFS) > 0 || len(r.remoteFS) > 0 {
// For specs that are not read from a filesystem (either from remote URL or []byte), we need to
// assign a theoretical root file. Having a root file is necessary when mapping references.
rootFile := "root.yaml"
if r.indexConfig.SpecFilePath != "" {
rootFile = filepath.Base(r.indexConfig.SpecFilePath)
r.indexConfig.SpecAbsolutePath = filepath.Join(basePath, filepath.Base(r.indexConfig.SpecFilePath))
} else {
r.indexConfig.SetTheoreticalRoot()
}
r.indexConfig.SpecAbsolutePath = filepath.Join(basePath, rootFile)
}
}

Expand Down
2 changes: 1 addition & 1 deletion index/search_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (index *SpecIndex) SearchIndexForReferenceByReferenceWithContext(ctx contex
if strings.Contains(roloLookup, "#") {
roloLookup = strings.Split(roloLookup, "#")[0]
}
if filepath.Base(roloLookup) == "root.yaml" {
if filepath.Base(roloLookup) == index.GetSpecFileName() {
return nil, index, ctx
}
rFile, err := index.rolodex.Open(roloLookup)
Expand Down
11 changes: 11 additions & 0 deletions index/spec_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (
"gopkg.in/yaml.v3"
)

const (
theoreticalRoot = "root.yaml"
)

// NewSpecIndexWithConfig will create a new index of an OpenAPI or Swagger spec. It uses the same logic as NewSpecIndex
// except it sets a base URL for resolving relative references, except it also allows for granular control over
// how the index is set up.
Expand Down Expand Up @@ -152,6 +156,13 @@ func (index *SpecIndex) GetRolodex() *Rolodex {
return index.rolodex
}

func (index *SpecIndex) GetSpecFileName() string {
if index == nil || index.rolodex == nil || index.rolodex.indexConfig == nil {
return theoreticalRoot
}
return index.rolodex.indexConfig.SpecFilePath
}

// GetGlobalTagsNode returns document root tags node.
func (index *SpecIndex) GetGlobalTagsNode() *yaml.Node {
return index.tagsNode
Expand Down

0 comments on commit f55c151

Please sign in to comment.