Skip to content

Commit 3794d14

Browse files
authored
go:embed loader.ReadFromURIFunc example (#319)
1 parent d28d2bb commit 3794d14

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/getkin/kin-openapi
22

3-
go 1.14
3+
go 1.16
44

55
require (
66
github.com/ghodss/yaml v1.0.0

openapi3/load_with_go_embed_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package openapi3_test
2+
3+
import (
4+
"embed"
5+
"fmt"
6+
"net/url"
7+
8+
"github.com/getkin/kin-openapi/openapi3"
9+
)
10+
11+
//go:embed testdata/recursiveRef/*
12+
var fs embed.FS
13+
14+
func Example() {
15+
loader := openapi3.NewSwaggerLoader()
16+
loader.IsExternalRefsAllowed = true
17+
loader.ReadFromURIFunc = func(loader *openapi3.SwaggerLoader, uri *url.URL) ([]byte, error) {
18+
return fs.ReadFile(uri.Path)
19+
}
20+
21+
doc, err := loader.LoadSwaggerFromFile("testdata/recursiveRef/openapi.yml")
22+
if err != nil {
23+
panic(err)
24+
}
25+
26+
if err = doc.Validate(loader.Context); err != nil {
27+
panic(err)
28+
}
29+
30+
fmt.Println(doc.Paths["/foo"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties["foo"].Value.Properties["bar"].Value.Type)
31+
// Output: array
32+
}

0 commit comments

Comments
 (0)