-
-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathload_cicular_ref_with_external_file_test.go
84 lines (73 loc) · 2.08 KB
/
load_cicular_ref_with_external_file_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//go:build go1.16
// +build go1.16
package openapi3_test
import (
"embed"
"encoding/json"
"net/url"
"testing"
"github.com/stretchr/testify/require"
"github.com/getkin/kin-openapi/openapi3"
)
//go:embed testdata/circularRef/*
var circularResSpecs embed.FS
func TestLoadCircularRefFromFile(t *testing.T) {
loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true
loader.ReadFromURIFunc = func(loader *openapi3.Loader, uri *url.URL) ([]byte, error) {
return circularResSpecs.ReadFile(uri.Path)
}
got, err := loader.LoadFromFile("testdata/circularRef/base.yml")
require.NoError(t, err)
foo := &openapi3.SchemaRef{
Value: &openapi3.Schema{
Properties: map[string]*openapi3.SchemaRef{
"foo2": {
Ref: "other.yml#/components/schemas/Foo2", // reference to an external file
Value: &openapi3.Schema{
Properties: map[string]*openapi3.SchemaRef{
"id": {
Value: &openapi3.Schema{Type: &openapi3.Types{"string"}}},
},
},
},
},
},
}
bar := &openapi3.SchemaRef{Value: &openapi3.Schema{Properties: make(map[string]*openapi3.SchemaRef)}}
// circular reference
bar.Value.Properties["foo"] = &openapi3.SchemaRef{Ref: "#/components/schemas/Foo", Value: foo.Value}
foo.Value.Properties["bar"] = &openapi3.SchemaRef{Ref: "#/components/schemas/Bar", Value: bar.Value}
bazNestedRef := &openapi3.SchemaRef{Ref: "./baz.yml#/BazNested"}
array := openapi3.NewArraySchema()
array.Items = bazNestedRef
bazNested := &openapi3.Schema{Properties: map[string]*openapi3.SchemaRef{
"bazArray": {
Value: &openapi3.Schema{
Items: bazNestedRef,
},
},
"baz": bazNestedRef,
}}
bazNestedRef.Value = bazNested
want := &openapi3.T{
OpenAPI: "3.0.3",
Info: &openapi3.Info{
Title: "Recursive cyclic refs example",
Version: "1.0",
},
Components: &openapi3.Components{
Schemas: openapi3.Schemas{
"Foo": foo,
"Bar": bar,
"Baz": bazNestedRef,
},
},
}
jsoner := func(doc *openapi3.T) string {
data, err := json.Marshal(doc)
require.NoError(t, err)
return string(data)
}
require.JSONEq(t, jsoner(want), jsoner(got))
}