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

Resolve external ref from data #286

Open
wangyoucao577 opened this issue Jan 12, 2021 · 2 comments
Open

Resolve external ref from data #286

wangyoucao577 opened this issue Jan 12, 2021 · 2 comments

Comments

@wangyoucao577
Copy link

When using external ref, currently there're two ways to resolve the references when loading swagger: #245 (comment) and #245 (comment). However, both ways requires path(URL or local file) for the resolving.

Is it possible to provide another API (e.g., LoadSwaggerFromDataWithData) that able to resolve external ref by loaded data?

Thanks!

@wangyoucao577
Copy link
Author

I'm using deepmap/oapi-codegen to generate Go codes from OpenAPI 3, which will generate swaggerSpec along with code, e.g., https://github.com/deepmap/oapi-codegen/blob/fbeefa1d08cc055289fd6d7c10dee27088b51136/examples/petstore-expanded/chi/api/petstore.gen.go#L252.

In my usage, there's no more yaml files when application run, only all the swagger spec data. This workaround is not work for me.

Thanks!

@fenollp
Copy link
Collaborator

fenollp commented Apr 23, 2021

Here's an example of how to use loader.ReadFromURIFunc and/or go embed

import (
"embed"
"fmt"
"net/url"
"github.com/getkin/kin-openapi/openapi3"
)
//go:embed recursiveRef/*
var fs embed.FS
func Example() {
loader := openapi3.NewSwaggerLoader()
loader.IsExternalRefsAllowed = true
loader.ReadFromURIFunc = func(loader *openapi3.SwaggerLoader, uri *url.URL) ([]byte, error) {
return fs.ReadFile(uri.Path)
}
doc, err := loader.LoadSwaggerFromFile("recursiveRef/openapi.yml")
if err != nil {
panic(err)
}
if err = doc.Validate(loader.Context); err != nil {
panic(err)
}
fmt.Println(doc.Paths["/foo"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties["foo"].Value.Properties["bar"].Value.Type)
// Output: array

If that doesn't work for you, can you explain further your LoadSwaggerFromDataWithData idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants