Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openapi: Add support for external refs #11076

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 14 additions & 12 deletions tpl/openapi/openapi3/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 --
Expand Down
22 changes: 21 additions & 1 deletion tpl/openapi/openapi3/openapi3.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package openapi3
import (
"fmt"
"io"
"net/url"
"strings"

gyaml "github.com/ghodss/yaml"

Expand Down Expand Up @@ -90,7 +92,25 @@ 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) {
relativePath := url.Path

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

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing defer file.Close()

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just throwing this out there, but it looks like the contributor added the defer file.Close() on line 108

return io.ReadAll(file)
}

err = loader.ResolveRefsIn(s, nil)

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