Fixed other references to "root.yaml"

This commit is contained in:
Calvin Lobo
2024-08-28 14:32:37 -04:00
committed by quobix
parent 07ed25a870
commit c8186468ae
4 changed files with 28 additions and 7 deletions

View File

@@ -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)
//

View File

@@ -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)
}
}

View File

@@ -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)

View File

@@ -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