From c8186468aea7fd0974bcadcfbc4fab9a5d7817db Mon Sep 17 00:00:00 2001 From: Calvin Lobo Date: Wed, 28 Aug 2024 14:32:37 -0400 Subject: [PATCH] Fixed other references to "root.yaml" --- index/index_model.go | 12 ++++++++++++ index/rolodex.go | 10 ++++------ index/search_index.go | 2 +- index/spec_index.go | 11 +++++++++++ 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/index/index_model.go b/index/index_model.go index 53659e61..5dec6bb2 100644 --- a/index/index_model.go +++ b/index/index_model.go @@ -8,6 +8,7 @@ import ( "log/slog" "net/http" "net/url" + "path/filepath" "sync" "github.com/pb33f/libopenapi/datamodel" @@ -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) // diff --git a/index/rolodex.go b/index/rolodex.go index 020dbfba..f5f692c9 100644 --- a/index/rolodex.go +++ b/index/rolodex.go @@ -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 { @@ -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) } } diff --git a/index/search_index.go b/index/search_index.go index 25806bd0..0f46a533 100644 --- a/index/search_index.go +++ b/index/search_index.go @@ -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) diff --git a/index/spec_index.go b/index/spec_index.go index 7a74222f..56861b37 100644 --- a/index/spec_index.go +++ b/index/spec_index.go @@ -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. @@ -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