forked from ogen-go/ogen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_test.go
224 lines (199 loc) · 5.87 KB
/
gen_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package ogen_test
import (
"net/url"
"path"
"runtime/debug"
"strings"
"testing"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
"github.com/ogen-go/ogen"
"github.com/ogen-go/ogen/gen"
"github.com/ogen-go/ogen/gen/genfs"
"github.com/ogen-go/ogen/gen/ir"
"github.com/ogen-go/ogen/location"
"github.com/ogen-go/ogen/openapi/parser"
)
func testGenerate(t *testing.T, dir, filename string, data []byte, aliases ctAliases, ignore ...string) {
t.Helper()
t.Parallel()
log := zaptest.NewLogger(t)
spec, err := ogen.Parse(data)
require.NoError(t, err)
notImplemented := map[string]struct{}{}
opt := gen.Options{
Parser: gen.ParseOptions{
InferSchemaType: true,
File: location.NewFile(filename, filename, data),
},
Generator: gen.GenerateOptions{
IgnoreNotImplemented: ignore,
NotImplementedHook: func(name string, err error) {
notImplemented[name] = struct{}{}
},
ContentTypeAliases: aliases,
},
Logger: log,
}
if filename == "file_reference.yml" { // HACK
opt.Parser.AllowRemote = true
opt.Parser.RootURL = &url.URL{
Scheme: "file",
Path: "/" + path.Join(dir, filename),
}
opt.Parser.Remote = gen.RemoteOptions{
ReadFile: func(p string) ([]byte, error) {
p = strings.TrimPrefix(p, "/")
return testdata.ReadFile(p)
},
URLToFilePath: func(u *url.URL) (string, error) {
// By default, urlpath.URLToFilePath output depends on the OS.
//
// But we use virtual filesystem, so we should use the fs.FS path.
if u.Path == "" {
return u.Opaque, nil
}
return u.Path, nil
},
}
}
if path.Base(dir) == "convenient_errors" {
require.NoError(t, opt.Generator.ConvenientErrors.Set("on"))
}
t.Run("Gen", func(t *testing.T) {
defer func() {
if rr := recover(); rr != nil {
t.Fatalf("panic: %+v\n%s", rr, debug.Stack())
}
}()
g, err := gen.NewGenerator(spec, opt)
require.NoError(t, err)
require.NoError(t, g.WriteSource(genfs.CheckFS{}, "api"))
if len(opt.Generator.IgnoreNotImplemented) > 0 {
// Check that all ignore rules are necessary.
for _, feature := range ignore {
if _, ok := notImplemented[feature]; !ok {
t.Errorf("Ignore rule %q hasn't been used", feature)
}
}
}
})
t.Run("Full", func(t *testing.T) {
t.Skipf("Ignoring: [%s]", strings.Join(opt.Generator.IgnoreNotImplemented, ", "))
})
}
type ctAliases = map[string]ir.Encoding
func runPositive(root string,
aliases map[string]ctAliases,
skipSets map[string][]string,
) func(t *testing.T) {
return func(t *testing.T) {
t.Helper()
// Ensure that all skipSets schemas are present.
for file := range skipSets {
_, err := testdata.ReadFile(path.Join(root, file))
require.NoErrorf(t, err, "skip file %s", file)
}
walkTestdata(t, root, func(t *testing.T, file string, data []byte) {
dir := path.Dir(file)
if parent := path.Base(dir); parent == "file_reference_external" {
t.Skip("Special directory for testing remote references.")
return
}
file = strings.TrimPrefix(file, root+"/")
skip := skipSets[file]
testGenerate(t, dir, file, data, aliases[file], skip...)
})
}
}
func TestGenerate(t *testing.T) {
t.Run("Positive", runPositive("_testdata/positive", nil,
map[string][]string{
"sample.json": {
"enum format",
},
"content_header_response.json": {
"parameter content encoding",
},
}))
t.Run("Examples", runPositive("_testdata/examples",
map[string]ctAliases{
"autorest/ApiManagementClient-openapi.json": {
"text/json": ir.EncodingJSON,
"application/vnd.swagger.doc+json": ir.EncodingJSON,
},
"api.github.com.json": {
"text/x-markdown": ir.EncodingTextPlain,
"text/html": ir.EncodingTextPlain,
"application/octocat-stream": ir.EncodingTextPlain,
// FIXME(tdakkota): multiple response types makes wrapper cry about
// type name conflict.
// "application/vnd.github.v3.star+json": ir.EncodingJSON,
"application/vnd.github.v3.object": ir.EncodingJSON,
"application/scim+json": ir.EncodingJSON,
},
"k8s.json": {
"application/jwk-set+json": ir.EncodingJSON,
"application/merge-patch+json": ir.EncodingJSON,
"application/strategic-merge-patch+json": ir.EncodingJSON,
},
},
map[string][]string{
"autorest/additionalProperties.json": {},
"autorest/lro.json": {},
"autorest/storage.json": {},
"autorest/xms-error-responses.json": {},
"2ch.yml": {},
"api.github.com.json": {
"complex anyOf",
"discriminator inference",
"sum types with same names",
"sum type parameter",
"array defaults",
},
"manga.json": {},
"telegram_bot_api.json": {},
"gotd_bot_api.json": {},
"k8s.json": {
"unsupported content types",
},
"petstore-expanded.yml": {},
"redoc/discriminator.json": {
"unsupported content types",
},
}))
}
func TestNegative(t *testing.T) {
walkTestdata(t, "_testdata/negative", func(t *testing.T, file string, data []byte) {
log := zaptest.NewLogger(t)
a := require.New(t)
dir, name := path.Split(file)
spec, err := ogen.Parse(data)
a.NoError(err)
f := location.NewFile(name, name, data)
_, err = parser.Parse(spec, parser.Settings{
InferTypes: true,
File: f,
})
a.NoError(err, "If the error is related to parser, move this test to parser package testdata")
opt := gen.Options{
Parser: gen.ParseOptions{
InferSchemaType: true,
File: f,
},
Logger: log,
}
t.Logf("Dir: %q, file: %q", dir, name)
if strings.Contains(dir, "convenient_errors") {
require.NoError(t, opt.Generator.ConvenientErrors.Set("on"))
}
_, err = gen.NewGenerator(spec, opt)
a.Error(err)
var buf strings.Builder
if location.PrintPrettyError(&buf, true, err) {
t.Log(buf.String())
} else {
t.Logf("%+v", err)
}
})
}