Tuned some glitches with v3 doc creation.

all covered now

Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
quobix
2023-10-31 15:31:19 -04:00
parent b37b9a2fb9
commit 8f3f568e5f
7 changed files with 334 additions and 278 deletions

View File

@@ -4,9 +4,9 @@
package datamodel
import (
"github.com/pb33f/libopenapi/utils"
"io/fs"
"log/slog"
"net/http"
"net/url"
"os"
)
@@ -28,7 +28,7 @@ type DocumentConfiguration struct {
// will not be used, as there will be nothing to use it against.
//
// Resolves [#132]: https://github.com/pb33f/libopenapi/issues/132
RemoteURLHandler func(url string) (*http.Response, error)
RemoteURLHandler utils.RemoteURLHandler
// If resolving locally, the BasePath will be the root from which relative references will be resolved from.
// It's usually the location of the root specification.
@@ -103,13 +103,3 @@ func NewDocumentConfiguration() *DocumentConfiguration {
})),
}
}
// Deprecated: use NewDocumentConfiguration instead.
func NewOpenDocumentConfiguration() *DocumentConfiguration {
return NewDocumentConfiguration()
}
// Deprecated: use NewDocumentConfiguration instead.
func NewClosedDocumentConfiguration() *DocumentConfiguration {
return NewDocumentConfiguration()
}

View File

@@ -51,12 +51,8 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
// If basePath is provided, add a local filesystem to the rolodex.
if idxConfig.BasePath != "" {
var absError error
var cwd string
cwd, absError = filepath.Abs(config.BasePath)
if absError != nil {
return nil, absError
}
cwd, _ = filepath.Abs(config.BasePath)
// if a supplied local filesystem is provided, add it to the rolodex.
if config.LocalFS != nil {
rolodex.AddLocalFS(cwd, config.LocalFS)
@@ -68,42 +64,30 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
DirFS: os.DirFS(cwd),
FileFilters: config.FileFilter,
}
fileFS, err := index.NewLocalFSWithConfig(&localFSConf)
if err != nil {
return nil, err
}
idxConfig.AllowFileLookup = true
// add the filesystem to the rolodex
rolodex.AddLocalFS(cwd, fileFS)
}
}
// if base url is provided, add a remote filesystem to the rolodex.
if idxConfig.BaseURL != nil {
// if a supplied remote filesystem is provided, add it to the rolodex.
if config.RemoteFS != nil {
if config.BaseURL == nil {
return nil, errors.New("cannot use remote filesystem without a BaseURL")
}
rolodex.AddRemoteFS(config.BaseURL.String(), config.RemoteFS)
} else {
// create a remote filesystem
remoteFS, fsErr := index.NewRemoteFSWithConfig(idxConfig)
if fsErr != nil {
return nil, fsErr
}
if config.RemoteURLHandler != nil {
remoteFS.RemoteHandlerFunc = config.RemoteURLHandler
}
idxConfig.AllowRemoteLookup = true
// add to the rolodex
rolodex.AddRemoteFS(config.BaseURL.String(), remoteFS)
// create a remote filesystem
remoteFS, _ := index.NewRemoteFSWithConfig(idxConfig)
if config.RemoteURLHandler != nil {
remoteFS.RemoteHandlerFunc = config.RemoteURLHandler
}
idxConfig.AllowRemoteLookup = true
// add to the rolodex
rolodex.AddRemoteFS(config.BaseURL.String(), remoteFS)
}
// index the rolodex

View File

@@ -2,7 +2,10 @@ package v3
import (
"fmt"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/utils"
"net/http"
"net/url"
"os"
"testing"
@@ -20,7 +23,7 @@ func initTest() {
info, _ := datamodel.ExtractSpecInfo(data)
var err error
// deprecated function test.
doc, err = CreateDocument(info)
doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
if err != nil {
panic("broken something")
}
@@ -56,6 +59,123 @@ func TestCircularReferenceError(t *testing.T) {
assert.Len(t, utils.UnwrapErrors(err), 3)
}
func TestRolodexLocalFileSystem(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/first.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
cf := datamodel.NewDocumentConfiguration()
cf.BasePath = "../../../test_specs"
cf.FileFilter = []string{"first.yaml", "second.yaml", "third.yaml"}
lDoc, err := CreateDocumentFromConfig(info, cf)
assert.NotNil(t, lDoc)
assert.NoError(t, err)
}
func TestRolodexLocalFileSystem_ProvideNonRolodexFS(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/first.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
baseDir := "../../../test_specs"
cf := datamodel.NewDocumentConfiguration()
cf.BasePath = baseDir
cf.FileFilter = []string{"first.yaml", "second.yaml", "third.yaml"}
cf.LocalFS = os.DirFS(baseDir)
lDoc, err := CreateDocumentFromConfig(info, cf)
assert.NotNil(t, lDoc)
assert.Error(t, err)
}
func TestRolodexLocalFileSystem_ProvideRolodexFS(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/first.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
baseDir := "../../../test_specs"
cf := datamodel.NewDocumentConfiguration()
cf.BasePath = baseDir
cf.FileFilter = []string{"first.yaml", "second.yaml", "third.yaml"}
localFS, lErr := index.NewLocalFSWithConfig(&index.LocalFSConfig{
BaseDirectory: baseDir,
DirFS: os.DirFS(baseDir),
FileFilters: cf.FileFilter,
})
cf.LocalFS = localFS
assert.NoError(t, lErr)
lDoc, err := CreateDocumentFromConfig(info, cf)
assert.NotNil(t, lDoc)
assert.NoError(t, err)
}
func TestRolodexLocalFileSystem_BadPath(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/first.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
cf := datamodel.NewDocumentConfiguration()
cf.BasePath = "/NOWHERE"
cf.FileFilter = []string{"first.yaml", "second.yaml", "third.yaml"}
lDoc, err := CreateDocumentFromConfig(info, cf)
assert.Nil(t, lDoc)
assert.Error(t, err)
}
func TestRolodexRemoteFileSystem(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/first.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
cf := datamodel.NewDocumentConfiguration()
baseUrl := "https://raw.githubusercontent.com/pb33f/libopenapi/main/test_specs"
u, _ := url.Parse(baseUrl)
cf.BaseURL = u
lDoc, err := CreateDocumentFromConfig(info, cf)
assert.NotNil(t, lDoc)
assert.NoError(t, err)
}
func TestRolodexRemoteFileSystem_BadBase(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/first.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
cf := datamodel.NewDocumentConfiguration()
baseUrl := "https://no-no-this-will-not-work-it-just-will-not-get-the-job-done-mate.com"
u, _ := url.Parse(baseUrl)
cf.BaseURL = u
lDoc, err := CreateDocumentFromConfig(info, cf)
assert.NotNil(t, lDoc)
assert.Error(t, err)
}
func TestRolodexRemoteFileSystem_CustomRemote_NoBaseURL(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/first.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
cf := datamodel.NewDocumentConfiguration()
cf.RemoteFS, _ = index.NewRemoteFSWithConfig(&index.SpecIndexConfig{})
lDoc, err := CreateDocumentFromConfig(info, cf)
assert.NotNil(t, lDoc)
assert.Error(t, err)
}
func TestRolodexRemoteFileSystem_CustomHttpHandler(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/first.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
cf := datamodel.NewDocumentConfiguration()
cf.RemoteURLHandler = http.Get
baseUrl := "https://no-no-this-will-not-work-it-just-will-not-get-the-job-done-mate.com"
u, _ := url.Parse(baseUrl)
cf.BaseURL = u
pizza := func(url string) (resp *http.Response, err error) {
return nil, nil
}
cf.RemoteURLHandler = pizza
lDoc, err := CreateDocumentFromConfig(info, cf)
assert.NotNil(t, lDoc)
assert.Error(t, err)
}
func TestCircularReference_IgnoreArray(t *testing.T) {
spec := `openapi: 3.1.0
components:
@@ -76,8 +196,6 @@ components:
info, _ := datamodel.ExtractSpecInfo([]byte(spec))
circDoc, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
IgnoreArrayCircularReferences: true,
})
assert.NotNil(t, circDoc)
@@ -104,8 +222,6 @@ components:
info, _ := datamodel.ExtractSpecInfo([]byte(spec))
circDoc, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
IgnorePolymorphicCircularReferences: true,
})
assert.NotNil(t, circDoc)
@@ -117,10 +233,7 @@ func BenchmarkCreateDocument_Stripe(b *testing.B) {
info, _ := datamodel.ExtractSpecInfo(data)
for i := 0; i < b.N; i++ {
_, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
_, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
if err != nil {
panic("this should not error")
}
@@ -131,10 +244,7 @@ func BenchmarkCreateDocument_Petstore(b *testing.B) {
data, _ := os.ReadFile("../../../test_specs/petstorev3.json")
info, _ := datamodel.ExtractSpecInfo(data)
for i := 0; i < b.N; i++ {
_, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
_, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
if err != nil {
panic("this should not error")
}
@@ -144,10 +254,7 @@ func BenchmarkCreateDocument_Petstore(b *testing.B) {
func TestCreateDocumentStripe(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/stripe.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
d, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
d, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
assert.Len(t, utils.UnwrapErrors(err), 3)
assert.Equal(t, "3.0.0", d.Version.Value)
@@ -211,10 +318,7 @@ webhooks:
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err error
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
assert.Len(t, utils.UnwrapErrors(err), 1)
}
@@ -590,10 +694,7 @@ components:
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err error
doc, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
doc, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
assert.NoError(t, err)
ob := doc.Components.Value.FindSchema("bork").Value
@@ -609,10 +710,7 @@ webhooks:
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err error
doc, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
doc, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
assert.Equal(t, "flat map build failed: reference cannot be found: reference at line 4, column 5 is empty, it cannot be resolved",
err.Error())
}
@@ -626,10 +724,7 @@ components:
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err error
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
assert.Equal(t, "reference at line 5, column 7 is empty, it cannot be resolved", err.Error())
}
@@ -641,10 +736,7 @@ paths:
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err error
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
assert.Equal(t,
"path item build failed: cannot find reference: at line 4, col 10", err.Error())
}
@@ -656,10 +748,7 @@ tags:
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err error
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
assert.Equal(t,
"object extraction failed: reference at line 3, column 5 is empty, it cannot be resolved", err.Error())
}
@@ -671,10 +760,7 @@ security:
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err error
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
assert.Equal(t,
"array build failed: reference cannot be found: reference at line 3, column 3 is empty, it cannot be resolved",
err.Error())
@@ -687,10 +773,7 @@ externalDocs:
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err error
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
assert.Equal(t, "object extraction failed: reference at line 3, column 3 is empty, it cannot be resolved", err.Error())
}
@@ -702,10 +785,7 @@ func TestCreateDocument_YamlAnchor(t *testing.T) {
info, _ := datamodel.ExtractSpecInfo(anchorDocument)
// build low-level document model
document, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
document, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
if err != nil {
fmt.Printf("error: %s\n", err.Error())
@@ -755,10 +835,7 @@ func ExampleCreateDocument() {
info, _ := datamodel.ExtractSpecInfo(petstoreBytes)
// build low-level document model
document, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
document, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
if err != nil {
fmt.Printf("error: %s\n", err.Error())

View File

@@ -283,7 +283,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
if index.config.BaseURL != nil {
u := *index.config.BaseURL
abs, _ := filepath.Abs(filepath.Join(u.Path, uri[0]))
abs := filepath.Join(u.Path, uri[0])
u.Path = abs
fullDefinitionPath = u.String()
componentName = uri[0]

View File

@@ -4,277 +4,277 @@
package index
import (
"fmt"
"github.com/pb33f/libopenapi/datamodel"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"
"io"
"io/fs"
"log/slog"
"os"
"path/filepath"
"strings"
"time"
"fmt"
"github.com/pb33f/libopenapi/datamodel"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"
"io"
"io/fs"
"log/slog"
"os"
"path/filepath"
"strings"
"time"
)
type LocalFS struct {
indexConfig *SpecIndexConfig
entryPointDirectory string
baseDirectory string
Files map[string]RolodexFile
logger *slog.Logger
readingErrors []error
indexConfig *SpecIndexConfig
entryPointDirectory string
baseDirectory string
Files map[string]RolodexFile
logger *slog.Logger
readingErrors []error
}
func (l *LocalFS) GetFiles() map[string]RolodexFile {
return l.Files
return l.Files
}
func (l *LocalFS) GetErrors() []error {
return l.readingErrors
return l.readingErrors
}
func (l *LocalFS) Open(name string) (fs.File, error) {
if l.indexConfig != nil && !l.indexConfig.AllowFileLookup {
return nil, &fs.PathError{Op: "open", Path: name,
Err: fmt.Errorf("file lookup for '%s' not allowed, set the index configuration "+
"to AllowFileLookup to be true", name)}
}
if l.indexConfig != nil && !l.indexConfig.AllowFileLookup {
return nil, &fs.PathError{Op: "open", Path: name,
Err: fmt.Errorf("file lookup for '%s' not allowed, set the index configuration "+
"to AllowFileLookup to be true", name)}
}
if !filepath.IsAbs(name) {
name, _ = filepath.Abs(filepath.Join(l.baseDirectory, name))
}
if !filepath.IsAbs(name) {
name, _ = filepath.Abs(filepath.Join(l.baseDirectory, name))
}
if f, ok := l.Files[name]; ok {
return f.(*LocalFile), nil
} else {
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist}
}
if f, ok := l.Files[name]; ok {
return f.(*LocalFile), nil
} else {
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist}
}
}
type LocalFile struct {
filename string
name string
extension FileExtension
data []byte
fullPath string
lastModified time.Time
readingErrors []error
index *SpecIndex
parsed *yaml.Node
offset int64
filename string
name string
extension FileExtension
data []byte
fullPath string
lastModified time.Time
readingErrors []error
index *SpecIndex
parsed *yaml.Node
offset int64
}
func (l *LocalFile) GetIndex() *SpecIndex {
return l.index
return l.index
}
func (l *LocalFile) Index(config *SpecIndexConfig) (*SpecIndex, error) {
if l.index != nil {
return l.index, nil
}
content := l.data
if l.index != nil {
return l.index, nil
}
content := l.data
// first, we must parse the content of the file
info, err := datamodel.ExtractSpecInfoWithDocumentCheck(content, true)
if err != nil {
return nil, err
}
// first, we must parse the content of the file
info, err := datamodel.ExtractSpecInfoWithDocumentCheck(content, true)
if err != nil {
return nil, err
}
index := NewSpecIndexWithConfig(info.RootNode, config)
index.specAbsolutePath = l.fullPath
index := NewSpecIndexWithConfig(info.RootNode, config)
index.specAbsolutePath = l.fullPath
l.index = index
return index, nil
l.index = index
return index, nil
}
func (l *LocalFile) GetContent() string {
return string(l.data)
return string(l.data)
}
func (l *LocalFile) GetContentAsYAMLNode() (*yaml.Node, error) {
if l.parsed != nil {
return l.parsed, nil
}
if l.index != nil && l.index.root != nil {
return l.index.root, nil
}
if l.data == nil {
return nil, fmt.Errorf("no data to parse for file: %s", l.fullPath)
}
var root yaml.Node
err := yaml.Unmarshal(l.data, &root)
if err != nil {
return nil, err
}
if l.index != nil && l.index.root == nil {
l.index.root = &root
}
l.parsed = &root
return &root, nil
if l.parsed != nil {
return l.parsed, nil
}
if l.index != nil && l.index.root != nil {
return l.index.root, nil
}
if l.data == nil {
return nil, fmt.Errorf("no data to parse for file: %s", l.fullPath)
}
var root yaml.Node
err := yaml.Unmarshal(l.data, &root)
if err != nil {
return nil, err
}
if l.index != nil && l.index.root == nil {
l.index.root = &root
}
l.parsed = &root
return &root, nil
}
func (l *LocalFile) GetFileExtension() FileExtension {
return l.extension
return l.extension
}
func (l *LocalFile) GetFullPath() string {
return l.fullPath
return l.fullPath
}
func (l *LocalFile) GetErrors() []error {
return l.readingErrors
return l.readingErrors
}
type LocalFSConfig struct {
// the base directory to index
BaseDirectory string
Logger *slog.Logger
FileFilters []string
DirFS fs.FS
// the base directory to index
BaseDirectory string
Logger *slog.Logger
FileFilters []string
DirFS fs.FS
}
func NewLocalFSWithConfig(config *LocalFSConfig) (*LocalFS, error) {
localFiles := make(map[string]RolodexFile)
var allErrors []error
localFiles := make(map[string]RolodexFile)
var allErrors []error
log := config.Logger
if log == nil {
log = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelError,
}))
}
log := config.Logger
if log == nil {
log = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelError,
}))
}
// if the basedir is an absolute file, we're just going to index that file.
ext := filepath.Ext(config.BaseDirectory)
file := filepath.Base(config.BaseDirectory)
// if the basedir is an absolute file, we're just going to index that file.
ext := filepath.Ext(config.BaseDirectory)
file := filepath.Base(config.BaseDirectory)
var absBaseDir string
absBaseDir, _ = filepath.Abs(config.BaseDirectory)
var absBaseDir string
absBaseDir, _ = filepath.Abs(config.BaseDirectory)
walkErr := fs.WalkDir(config.DirFS, ".", func(p string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
walkErr := fs.WalkDir(config.DirFS, ".", func(p string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
// we don't care about directories, or errors, just read everything we can.
if d.IsDir() {
return nil
}
// we don't care about directories, or errors, just read everything we can.
if d.IsDir() {
return nil
}
if len(ext) > 2 && p != file {
return nil
}
if len(ext) > 2 && p != file {
return nil
}
if strings.HasPrefix(p, ".") {
return nil
}
if strings.HasPrefix(p, ".") {
return nil
}
if len(config.FileFilters) > 0 {
if !slices.Contains(config.FileFilters, p) {
return nil
}
}
if len(config.FileFilters) > 0 {
if !slices.Contains(config.FileFilters, p) {
return nil
}
}
extension := ExtractFileType(p)
var readingErrors []error
abs, _ := filepath.Abs(filepath.Join(config.BaseDirectory, p))
extension := ExtractFileType(p)
var readingErrors []error
abs, _ := filepath.Abs(filepath.Join(config.BaseDirectory, p))
var fileData []byte
var fileData []byte
switch extension {
case YAML, JSON:
switch extension {
case YAML, JSON:
dirFile, _ := config.DirFS.Open(p)
modTime := time.Now()
stat, _ := dirFile.Stat()
if stat != nil {
modTime = stat.ModTime()
}
fileData, _ = io.ReadAll(dirFile)
log.Debug("collecting JSON/YAML file", "file", abs)
localFiles[abs] = &LocalFile{
filename: p,
name: filepath.Base(p),
extension: ExtractFileType(p),
data: fileData,
fullPath: abs,
lastModified: modTime,
readingErrors: readingErrors,
}
case UNSUPPORTED:
log.Debug("skipping non JSON/YAML file", "file", abs)
}
return nil
})
dirFile, _ := config.DirFS.Open(p)
modTime := time.Now()
stat, _ := dirFile.Stat()
if stat != nil {
modTime = stat.ModTime()
}
fileData, _ = io.ReadAll(dirFile)
log.Debug("collecting JSON/YAML file", "file", abs)
localFiles[abs] = &LocalFile{
filename: p,
name: filepath.Base(p),
extension: ExtractFileType(p),
data: fileData,
fullPath: abs,
lastModified: modTime,
readingErrors: readingErrors,
}
case UNSUPPORTED:
log.Debug("skipping non JSON/YAML file", "file", abs)
}
return nil
})
if walkErr != nil {
return nil, walkErr
}
if walkErr != nil {
return nil, walkErr
}
return &LocalFS{
Files: localFiles,
logger: log,
baseDirectory: absBaseDir,
entryPointDirectory: config.BaseDirectory,
readingErrors: allErrors,
}, nil
return &LocalFS{
Files: localFiles,
logger: log,
baseDirectory: absBaseDir,
entryPointDirectory: config.BaseDirectory,
readingErrors: allErrors,
}, nil
}
func NewLocalFS(baseDir string, dirFS fs.FS) (*LocalFS, error) {
config := &LocalFSConfig{
BaseDirectory: baseDir,
DirFS: dirFS,
}
return NewLocalFSWithConfig(config)
config := &LocalFSConfig{
BaseDirectory: baseDir,
DirFS: dirFS,
}
return NewLocalFSWithConfig(config)
}
func (l *LocalFile) FullPath() string {
return l.fullPath
return l.fullPath
}
func (l *LocalFile) Name() string {
return l.name
return l.name
}
func (l *LocalFile) Size() int64 {
return int64(len(l.data))
return int64(len(l.data))
}
func (l *LocalFile) Mode() fs.FileMode {
return fs.FileMode(0)
return fs.FileMode(0)
}
func (l *LocalFile) ModTime() time.Time {
return l.lastModified
return l.lastModified
}
func (l *LocalFile) IsDir() bool {
return false
return false
}
func (l *LocalFile) Sys() interface{} {
return nil
return nil
}
func (l *LocalFile) Close() error {
return nil
return nil
}
func (l *LocalFile) Stat() (fs.FileInfo, error) {
return l, nil
return l, nil
}
func (l *LocalFile) Read(b []byte) (int, error) {
if l.offset >= int64(len(l.GetContent())) {
return 0, io.EOF
}
if l.offset < 0 {
return 0, &fs.PathError{Op: "read", Path: l.GetFullPath(), Err: fs.ErrInvalid}
}
n := copy(b, l.GetContent()[l.offset:])
l.offset += int64(n)
return n, nil
if l.offset >= int64(len(l.GetContent())) {
return 0, io.EOF
}
if l.offset < 0 {
return 0, &fs.PathError{Op: "read", Path: l.GetFullPath(), Err: fs.ErrInvalid}
}
n := copy(b, l.GetContent()[l.offset:])
l.offset += int64(n)
return n, nil
}

View File

@@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/libopenapi/utils"
"log/slog"
"runtime"
@@ -21,13 +22,11 @@ import (
"time"
)
type RemoteURLHandler = func(url string) (*http.Response, error)
type RemoteFS struct {
indexConfig *SpecIndexConfig
rootURL string
rootURLParsed *url.URL
RemoteHandlerFunc RemoteURLHandler
RemoteHandlerFunc utils.RemoteURLHandler
Files syncmap.Map
ProcessingFiles syncmap.Map
FetchTime int64
@@ -176,6 +175,9 @@ const (
)
func NewRemoteFSWithConfig(specIndexConfig *SpecIndexConfig) (*RemoteFS, error) {
if specIndexConfig == nil {
return nil, errors.New("no spec index config provided")
}
remoteRootURL := specIndexConfig.BaseURL
log := specIndexConfig.Logger
if log == nil {
@@ -217,7 +219,7 @@ func NewRemoteFSWithRootURL(rootURL string) (*RemoteFS, error) {
return NewRemoteFSWithConfig(config)
}
func (i *RemoteFS) SetRemoteHandlerFunc(handlerFunc RemoteURLHandler) {
func (i *RemoteFS) SetRemoteHandlerFunc(handlerFunc utils.RemoteURLHandler) {
i.RemoteHandlerFunc = handlerFunc
}

View File

@@ -3,6 +3,7 @@ package utils
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"regexp"
"sort"
@@ -749,3 +750,5 @@ func CheckForMergeNodes(node *yaml.Node) {
}
}
}
type RemoteURLHandler = func(url string) (*http.Response, error)