Skip to content

Commit

Permalink
openapi: Add support for external refs
Browse files Browse the repository at this point in the history
Set IsExternalRefsAllowed to true and provide a ReadFromURIFunc.

Fixes #8067
  • Loading branch information
morysh committed Jun 8, 2023
1 parent 7377970 commit 645aa72
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion tpl/openapi/openapi3/openapi3.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ package openapi3
import (
"fmt"
"io"
"io/ioutil"
"net/url"
"strings"

gyaml "github.com/ghodss/yaml"

Expand Down Expand Up @@ -90,7 +93,30 @@ func (ns *Namespace) Unmarshal(r resource.UnmarshableResource) (*OpenAPIDocument
return nil, err
}

err = kopenapi3.NewLoader().ResolveRefsIn(s, nil)
loader := kopenapi3.NewLoader()
loader.IsExternalRefsAllowed = true
loader.ReadFromURIFunc = func(loader *kopenapi3.Loader, url *url.URL) ([]byte, error) {
var relativePath string

if strings.HasPrefix(url.Path, "./") {
relativePath = strings.TrimRightFunc(key, func(r rune) bool { return r != '/' }) + strings.TrimPrefix(url.Path, "./")
} else {
relativePath = url.Path
}

fmt.Println(relativePath)

file, err := ns.deps.SourceFilesystems.Assets.Fs.Open(relativePath)
if err != nil {
return nil, err
}

return ioutil.ReadAll(file)
}

fmt.Println(ns.deps.PathSpec.Paths.GetBasePath(false))

err = loader.ResolveRefsIn(s, nil)

return &OpenAPIDocument{T: s}, err
})
Expand Down

0 comments on commit 645aa72

Please sign in to comment.