Skip to content

Commit

Permalink
expose optional function to resolve remote files
Browse files Browse the repository at this point in the history
  • Loading branch information
FedeBev committed Jun 21, 2023
1 parent f790174 commit 7125304
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
9 changes: 8 additions & 1 deletion datamodel/document_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

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
// for more fine-grained control over controls and new features.
Expand All @@ -15,6 +18,10 @@ type DocumentConfiguration struct {
// Schema must be set to "http/https".
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.
// It's usually the location of the root specification.
BasePath string // set the Base Path for resolving relative references if the spec is exploded.
Expand Down
7 changes: 4 additions & 3 deletions datamodel/low/v2/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur

// build an index
idx := index.NewSpecIndexWithConfig(info.RootNode, &index.SpecIndexConfig{
BaseURL: config.BaseURL,
AllowRemoteLookup: config.AllowRemoteReferences,
AllowFileLookup: config.AllowFileReferences,
BaseURL: config.BaseURL,
RemoteDocumentGetter: config.RemoteDocumentGetter,
AllowRemoteLookup: config.AllowRemoteReferences,
AllowFileLookup: config.AllowFileReferences,
})
doc.Index = idx
doc.SpecInfo = info
Expand Down
11 changes: 6 additions & 5 deletions datamodel/low/v3/create_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
}
// build an index
idx := index.NewSpecIndexWithConfig(info.RootNode, &index.SpecIndexConfig{
BaseURL: config.BaseURL,
BasePath: cwd,
AllowFileLookup: config.AllowFileReferences,
AllowRemoteLookup: config.AllowRemoteReferences,
AvoidBuildIndex: config.AvoidIndexBuild,
BaseURL: config.BaseURL,
RemoteDocumentGetter: config.RemoteDocumentGetter,
BasePath: cwd,
AllowFileLookup: config.AllowFileReferences,
AllowRemoteLookup: config.AllowRemoteReferences,
AvoidBuildIndex: config.AvoidIndexBuild,
})
doc.Index = idx

Expand Down
19 changes: 13 additions & 6 deletions index/find_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ package index

import (
"fmt"
"github.com/pb33f/libopenapi/utils"
"github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath"
"gopkg.in/yaml.v3"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"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.
Expand Down Expand Up @@ -87,8 +88,10 @@ func (index *SpecIndex) FindComponent(componentId string, parent *yaml.Node) *Re

var httpClient = &http.Client{Timeout: time.Duration(60) * time.Second}

func getRemoteDoc(u string, d chan []byte, e chan error) {
resp, err := httpClient.Get(u)
type RemoteDocumentGetter = func(url string) (*http.Response, error)

func getRemoteDoc(g RemoteDocumentGetter, u string, d chan []byte, e chan error) {
resp, err := g(u)
if err != nil {
e <- err
close(e)
Expand Down Expand Up @@ -121,7 +124,11 @@ func (index *SpecIndex) lookupRemoteReference(ref string) (*yaml.Node, *yaml.Nod
go func(uri string) {
bc := make(chan []byte)
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 {
case v := <-bc:
body = v
Expand Down
9 changes: 7 additions & 2 deletions index/index_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
package index

import (
"golang.org/x/sync/syncmap"
"gopkg.in/yaml.v3"
"net/http"
"net/url"
"os"
"sync"

"golang.org/x/sync/syncmap"
"gopkg.in/yaml.v3"
)

// Constants used to determine if resolving is local, file based or remote file based.
Expand Down Expand Up @@ -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
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
BasePath string // set the Base Path for resolving relative references if the spec is exploded.

Expand Down

0 comments on commit 7125304

Please sign in to comment.