mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-06 12:37:49 +00:00
Extracting index building code to be run optionally
This is to allow the correct operation of local/remote lookups to be correctly indexed (after the resolver has run). Addresses an issue in vacuum https://github.com/daveshanley/vacuum/issues/294 Signed-off-by: Dave Shanley <dave@quobix.com>
This commit is contained in:
@@ -24,6 +24,10 @@ type DocumentConfiguration struct {
|
|||||||
|
|
||||||
// AllowRemoteReferences will allow the index to lookup remote references. This is disabled by default.
|
// AllowRemoteReferences will allow the index to lookup remote references. This is disabled by default.
|
||||||
AllowRemoteReferences bool
|
AllowRemoteReferences bool
|
||||||
|
|
||||||
|
// AvoidIndexBuild will avoid building the index. This is disabled by default, only use if you are sure you don't need it.
|
||||||
|
// This is useful for developers building out models that should be indexed later on.
|
||||||
|
AvoidIndexBuild bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewOpenDocumentConfiguration() *DocumentConfiguration {
|
func NewOpenDocumentConfiguration() *DocumentConfiguration {
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
|
|||||||
BasePath: cwd,
|
BasePath: cwd,
|
||||||
AllowFileLookup: config.AllowFileReferences,
|
AllowFileLookup: config.AllowFileReferences,
|
||||||
AllowRemoteLookup: config.AllowRemoteReferences,
|
AllowRemoteLookup: config.AllowRemoteReferences,
|
||||||
|
AvoidBuildIndex: config.AvoidIndexBuild,
|
||||||
})
|
})
|
||||||
doc.Index = idx
|
doc.Index = idx
|
||||||
|
|
||||||
|
|||||||
@@ -76,6 +76,13 @@ type SpecIndexConfig struct {
|
|||||||
// a breakglass to be used to prevent loops, checking the tree before recursing down.
|
// a breakglass to be used to prevent loops, checking the tree before recursing down.
|
||||||
ParentIndex *SpecIndex
|
ParentIndex *SpecIndex
|
||||||
|
|
||||||
|
// If set to true, the index will not be built out, which means only the foundational elements will be
|
||||||
|
// parsed and added to the index. This is useful to avoid building out an index if the specification is
|
||||||
|
// broken up into references and you want it fully resolved.
|
||||||
|
//
|
||||||
|
// Use the `BuildIndex()` method on the index to build it out once resolved/ready.
|
||||||
|
AvoidBuildIndex bool
|
||||||
|
|
||||||
// private fields
|
// private fields
|
||||||
seenRemoteSources *syncmap.Map
|
seenRemoteSources *syncmap.Map
|
||||||
remoteLock *sync.Mutex
|
remoteLock *sync.Mutex
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ func NewSpecIndexWithConfig(rootNode *yaml.Node, config *SpecIndexConfig) *SpecI
|
|||||||
return index
|
return index
|
||||||
}
|
}
|
||||||
boostrapIndexCollections(rootNode, index)
|
boostrapIndexCollections(rootNode, index)
|
||||||
return createNewIndex(rootNode, index)
|
return createNewIndex(rootNode, index, config.AvoidBuildIndex)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSpecIndex will create a new index of an OpenAPI or Swagger spec. It's not resolved or converted into anything
|
// NewSpecIndex will create a new index of an OpenAPI or Swagger spec. It's not resolved or converted into anything
|
||||||
@@ -55,10 +55,10 @@ func NewSpecIndex(rootNode *yaml.Node) *SpecIndex {
|
|||||||
index := new(SpecIndex)
|
index := new(SpecIndex)
|
||||||
index.config = CreateOpenAPIIndexConfig()
|
index.config = CreateOpenAPIIndexConfig()
|
||||||
boostrapIndexCollections(rootNode, index)
|
boostrapIndexCollections(rootNode, index)
|
||||||
return createNewIndex(rootNode, index)
|
return createNewIndex(rootNode, index, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func createNewIndex(rootNode *yaml.Node, index *SpecIndex) *SpecIndex {
|
func createNewIndex(rootNode *yaml.Node, index *SpecIndex, avoidBuildOut bool) *SpecIndex {
|
||||||
// there is no node! return an empty index.
|
// there is no node! return an empty index.
|
||||||
if rootNode == nil {
|
if rootNode == nil {
|
||||||
return index
|
return index
|
||||||
@@ -82,6 +82,23 @@ func createNewIndex(rootNode *yaml.Node, index *SpecIndex) *SpecIndex {
|
|||||||
index.ExtractExternalDocuments(index.root)
|
index.ExtractExternalDocuments(index.root)
|
||||||
index.GetPathCount()
|
index.GetPathCount()
|
||||||
|
|
||||||
|
// build out the index.
|
||||||
|
if !avoidBuildOut {
|
||||||
|
index.BuildIndex()
|
||||||
|
}
|
||||||
|
|
||||||
|
// do a copy!
|
||||||
|
index.config.seenRemoteSources.Range(func(k, v any) bool {
|
||||||
|
index.seenRemoteSources[k.(string)] = v.(*yaml.Node)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
return index
|
||||||
|
}
|
||||||
|
|
||||||
|
// BuildIndex will run all of the count operations required to build up maps of everything. It's what makes the index
|
||||||
|
// useful for looking up things, the count operations are all run in parallel and then the final calculations are run
|
||||||
|
// the index is ready.
|
||||||
|
func (index *SpecIndex) BuildIndex() {
|
||||||
countFuncs := []func() int{
|
countFuncs := []func() int{
|
||||||
index.GetOperationCount,
|
index.GetOperationCount,
|
||||||
index.GetComponentSchemaCount,
|
index.GetComponentSchemaCount,
|
||||||
@@ -111,13 +128,6 @@ func createNewIndex(rootNode *yaml.Node, index *SpecIndex) *SpecIndex {
|
|||||||
index.GetInlineDuplicateParamCount()
|
index.GetInlineDuplicateParamCount()
|
||||||
index.GetAllDescriptionsCount()
|
index.GetAllDescriptionsCount()
|
||||||
index.GetTotalTagsCount()
|
index.GetTotalTagsCount()
|
||||||
|
|
||||||
// do a copy!
|
|
||||||
index.config.seenRemoteSources.Range(func(k, v any) bool {
|
|
||||||
index.seenRemoteSources[k.(string)] = v.(*yaml.Node)
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
return index
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRootNode returns document root node.
|
// GetRootNode returns document root node.
|
||||||
|
|||||||
Reference in New Issue
Block a user