diff --git a/tpl/openapi/openapi3/integration_test.go b/tpl/openapi/openapi3/integration_test.go index d3be0eda94b..47f2fd700cc 100644 --- a/tpl/openapi/openapi3/integration_test.go +++ b/tpl/openapi/openapi3/integration_test.go @@ -37,18 +37,20 @@ servers: description: Optional server description, e.g. Internal staging server for testing paths: /users: - get: - summary: Returns a list of users. - description: Optional extended description in CommonMark or HTML. - responses: - '200': # status code - description: A JSON array of user names - content: - application/json: - schema: - type: array - items: - type: string + $ref: ./methods/get.yaml +-- assets/api/methods/get.yaml -- +get: + summary: Returns a list of users. + description: Optional extended description in CommonMark or HTML. + responses: + '200': # status code + description: A JSON array of user names + content: + application/json: + schema: + type: array + items: + type: string -- config.toml -- baseURL = 'http://example.com/' -- layouts/index.html -- diff --git a/tpl/openapi/openapi3/openapi3.go b/tpl/openapi/openapi3/openapi3.go index 38857dd9893..f2d506e7e26 100644 --- a/tpl/openapi/openapi3/openapi3.go +++ b/tpl/openapi/openapi3/openapi3.go @@ -17,6 +17,9 @@ package openapi3 import ( "fmt" "io" + "io/ioutil" + "net/url" + "strings" gyaml "github.com/ghodss/yaml" @@ -90,7 +93,26 @@ 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 + } + + file, err := ns.deps.SourceFilesystems.Assets.Fs.Open(relativePath) + if err != nil { + return nil, err + } + + return ioutil.ReadAll(file) + } + + err = loader.ResolveRefsIn(s, nil) return &OpenAPIDocument{T: s}, err })