Allow to override basePath for file lookup

This commit is contained in:
Dmitry
2023-03-16 06:17:59 +01:00
committed by Dave Shanley
parent 9004d57197
commit 634d675eef
2 changed files with 194 additions and 184 deletions

View File

@@ -15,6 +15,10 @@ type DocumentConfiguration struct {
// Schema must be set to "http/https". // Schema must be set to "http/https".
BaseURL *url.URL BaseURL *url.URL
// If resolving locally, the BasePath will be the root from which relative references will be resolved from.
// It usually location of the root specification.
BasePath string // set the Base Path for resolving relative references if the spec is exploded.
// AllowFileReferences will allow the index to locate relative file references. This is disabled by default. // AllowFileReferences will allow the index to locate relative file references. This is disabled by default.
AllowFileReferences bool AllowFileReferences bool

View File

@@ -2,14 +2,15 @@ package v3
import ( import (
"errors" "errors"
"os"
"sync"
"github.com/pb33f/libopenapi/datamodel" "github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/datamodel/low/base" "github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/pb33f/libopenapi/index" "github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/resolver" "github.com/pb33f/libopenapi/resolver"
"github.com/pb33f/libopenapi/utils" "github.com/pb33f/libopenapi/utils"
"os"
"sync"
) )
// CreateDocument will create a new Document instance from the provided SpecInfo. // CreateDocument will create a new Document instance from the provided SpecInfo.
@@ -38,9 +39,13 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
version = low.NodeReference[string]{Value: versionNode.Value, KeyNode: labelNode, ValueNode: versionNode} version = low.NodeReference[string]{Value: versionNode.Value, KeyNode: labelNode, ValueNode: versionNode}
doc := Document{Version: version} doc := Document{Version: version}
// get current working directory // get current working directory as a basePath
cwd, _ := os.Getwd() cwd, _ := os.Getwd()
// If basePath is provided override it
if config.BasePath != "" {
cwd = config.BasePath
}
// build an index // build an index
idx := index.NewSpecIndexWithConfig(info.RootNode, &index.SpecIndexConfig{ idx := index.NewSpecIndexWithConfig(info.RootNode, &index.SpecIndexConfig{
BaseURL: config.BaseURL, BaseURL: config.BaseURL,
@@ -72,14 +77,15 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
_, dialectLabel, dialectNode := utils.FindKeyNodeFull(JSONSchemaDialectLabel, info.RootNode.Content) _, dialectLabel, dialectNode := utils.FindKeyNodeFull(JSONSchemaDialectLabel, info.RootNode.Content)
if dialectNode != nil { if dialectNode != nil {
doc.JsonSchemaDialect = low.NodeReference[string]{ doc.JsonSchemaDialect = low.NodeReference[string]{
Value: dialectNode.Value, KeyNode: dialectLabel, ValueNode: dialectNode} Value: dialectNode.Value, KeyNode: dialectLabel, ValueNode: dialectNode,
}
} }
var runExtraction = func(info *datamodel.SpecInfo, doc *Document, idx *index.SpecIndex, runExtraction := func(info *datamodel.SpecInfo, doc *Document, idx *index.SpecIndex,
runFunc func(i *datamodel.SpecInfo, d *Document, idx *index.SpecIndex) error, runFunc func(i *datamodel.SpecInfo, d *Document, idx *index.SpecIndex) error,
ers *[]error, ers *[]error,
wg *sync.WaitGroup) { wg *sync.WaitGroup,
) {
if er := runFunc(info, doc, idx); er != nil { if er := runFunc(info, doc, idx); er != nil {
*ers = append(*ers, er) *ers = append(*ers, er)
} }