-
-
Notifications
You must be signed in to change notification settings - Fork 432
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
Support loading via http.FileSystem #311
Comments
Is this not doable today with a |
Unfortunately it doesn't works. package main
import (
"context"
"embed"
"fmt"
"net/url"
"path/filepath"
"github.com/getkin/kin-openapi/openapi3"
)
//go:embed spec/*
var spec embed.FS
func main() {
ctx := context.Background()
loader := openapi3.NewSwaggerLoader()
loader.IsExternalRefsAllowed = true
loader.LoadSwaggerFromURIFunc = func(loader *openapi3.SwaggerLoader, url *url.URL) (*openapi3.Swagger, error) {
data, err := spec.ReadFile(filepath.Join("spec", url.Path))
if err != nil {
panic(err)
}
swagger, err := loader.LoadSwaggerFromData(data)
if err != nil {
panic(err)
}
return swagger, nil
}
swagger, err := loader.LoadSwaggerFromFile("openapi.yml")
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", swagger.Paths["/foo"].Get.Responses["200"].Value.Content["application/json"].Schema)
if err = swagger.Validate(ctx); err != nil {
panic(err)
}
}
It looks kin-openapi/openapi3/swagger_loader.go Line 126 in 89012b7
|
Then kin-openapi/openapi3/swagger_loader.go Line 126 in 89012b7
swaggerLoader :)Could you update your PR to do this change and provide an example func that uses http.FS? |
Doing this actually also solves #231 |
OK, I'll try. |
I prepared another PR #316 package main
import (
"context"
"embed"
"fmt"
"github.com/getkin/kin-openapi/openapi3"
"net/url"
)
//go:embed spec/*
var spec embed.FS
func main() {
ctx := context.Background()
loader := openapi3.NewSwaggerLoader()
loader.IsExternalRefsAllowed = true
loader.ReadFromURIFunc = func(loader *openapi3.SwaggerLoader, url *url.URL) ([]byte, error) {
fmt.Println(url.Path)
return spec.ReadFile(url.Path)
}
swagger, err := loader.LoadSwaggerFromFile("spec/openapi.yml")
if err != nil {
panic(err)
}
if err = swagger.Validate(ctx); err != nil {
panic(err)
}
fmt.Printf("%+v\n", swagger.Paths["/foo"].Get.Responses["200"].Value.Content["application/json"].Schema.Value)
}
|
Very nice, thank you! I like this a lot. |
It is great if this library can load with http.FileSystem because we can embed specification files into single binary with Go1.16's embed feature
The text was updated successfully, but these errors were encountered: