mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-07 04:20:14 +00:00
expose optional function to resolve remote files
This commit is contained in:
@@ -3,7 +3,10 @@
|
|||||||
|
|
||||||
package datamodel
|
package datamodel
|
||||||
|
|
||||||
import "net/url"
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
// DocumentConfiguration is used to configure the document creation process. It was added in v0.6.0 to allow
|
// DocumentConfiguration is used to configure the document creation process. It was added in v0.6.0 to allow
|
||||||
// for more fine-grained control over controls and new features.
|
// for more fine-grained control over controls and new features.
|
||||||
@@ -15,6 +18,10 @@ type DocumentConfiguration struct {
|
|||||||
// Schema must be set to "http/https".
|
// Schema must be set to "http/https".
|
||||||
BaseURL *url.URL
|
BaseURL *url.URL
|
||||||
|
|
||||||
|
// RemoteDocumentGetter is a function that will be used to retrieve remote documents. If not set, the default
|
||||||
|
// remote document getter will be used.
|
||||||
|
RemoteDocumentGetter func(url string) (*http.Response, error)
|
||||||
|
|
||||||
// If resolving locally, the BasePath will be the root from which relative references will be resolved from.
|
// 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.
|
// It's usually the location of the root specification.
|
||||||
BasePath string // set the Base Path for resolving relative references if the spec is exploded.
|
BasePath string // set the Base Path for resolving relative references if the spec is exploded.
|
||||||
|
|||||||
@@ -144,6 +144,7 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
|
|||||||
// 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,
|
||||||
|
RemoteDocumentGetter: config.RemoteDocumentGetter,
|
||||||
AllowRemoteLookup: config.AllowRemoteReferences,
|
AllowRemoteLookup: config.AllowRemoteReferences,
|
||||||
AllowFileLookup: config.AllowFileReferences,
|
AllowFileLookup: config.AllowFileReferences,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
|
|||||||
// 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,
|
||||||
|
RemoteDocumentGetter: config.RemoteDocumentGetter,
|
||||||
BasePath: cwd,
|
BasePath: cwd,
|
||||||
AllowFileLookup: config.AllowFileReferences,
|
AllowFileLookup: config.AllowFileReferences,
|
||||||
AllowRemoteLookup: config.AllowRemoteReferences,
|
AllowRemoteLookup: config.AllowRemoteReferences,
|
||||||
|
|||||||
@@ -5,9 +5,6 @@ package index
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/pb33f/libopenapi/utils"
|
|
||||||
"github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath"
|
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -15,6 +12,10 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/pb33f/libopenapi/utils"
|
||||||
|
"github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FindComponent will locate a component by its reference, returns nil if nothing is found.
|
// FindComponent will locate a component by its reference, returns nil if nothing is found.
|
||||||
@@ -87,8 +88,10 @@ func (index *SpecIndex) FindComponent(componentId string, parent *yaml.Node) *Re
|
|||||||
|
|
||||||
var httpClient = &http.Client{Timeout: time.Duration(60) * time.Second}
|
var httpClient = &http.Client{Timeout: time.Duration(60) * time.Second}
|
||||||
|
|
||||||
func getRemoteDoc(u string, d chan []byte, e chan error) {
|
type RemoteDocumentGetter = func(url string) (*http.Response, error)
|
||||||
resp, err := httpClient.Get(u)
|
|
||||||
|
func getRemoteDoc(g RemoteDocumentGetter, u string, d chan []byte, e chan error) {
|
||||||
|
resp, err := g(u)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
e <- err
|
e <- err
|
||||||
close(e)
|
close(e)
|
||||||
@@ -121,7 +124,11 @@ func (index *SpecIndex) lookupRemoteReference(ref string) (*yaml.Node, *yaml.Nod
|
|||||||
go func(uri string) {
|
go func(uri string) {
|
||||||
bc := make(chan []byte)
|
bc := make(chan []byte)
|
||||||
ec := make(chan error)
|
ec := make(chan error)
|
||||||
go getRemoteDoc(uri, bc, ec)
|
var getter RemoteDocumentGetter = httpClient.Get
|
||||||
|
if index.config != nil && index.config.RemoteDocumentGetter != nil {
|
||||||
|
getter = index.config.RemoteDocumentGetter
|
||||||
|
}
|
||||||
|
go getRemoteDoc(getter, uri, bc, ec)
|
||||||
select {
|
select {
|
||||||
case v := <-bc:
|
case v := <-bc:
|
||||||
body = v
|
body = v
|
||||||
|
|||||||
@@ -4,12 +4,13 @@
|
|||||||
package index
|
package index
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"golang.org/x/sync/syncmap"
|
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"golang.org/x/sync/syncmap"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Constants used to determine if resolving is local, file based or remote file based.
|
// Constants used to determine if resolving is local, file based or remote file based.
|
||||||
@@ -60,6 +61,10 @@ type SpecIndexConfig struct {
|
|||||||
// More details on relative references can be found in issue #73: https://github.com/pb33f/libopenapi/issues/73
|
// More details on relative references can be found in issue #73: https://github.com/pb33f/libopenapi/issues/73
|
||||||
BaseURL *url.URL // set the Base URL for resolving relative references if the spec is exploded.
|
BaseURL *url.URL // set the Base URL for resolving relative references if the spec is exploded.
|
||||||
|
|
||||||
|
// If resolving remotely, the RemoteDocumentGetter will be used to fetch the remote document.
|
||||||
|
// If not set, the default http client will be used.
|
||||||
|
RemoteDocumentGetter func(url string) (*http.Response, error)
|
||||||
|
|
||||||
// If resolving locally, the BasePath will be the root from which relative references will be resolved from
|
// If resolving locally, the BasePath will be the root from which relative references will be resolved from
|
||||||
BasePath string // set the Base Path for resolving relative references if the spec is exploded.
|
BasePath string // set the Base Path for resolving relative references if the spec is exploded.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user