-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathgen_test.go
More file actions
301 lines (269 loc) · 7.69 KB
/
gen_test.go
File metadata and controls
301 lines (269 loc) · 7.69 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
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,
WildcardContentTypeDefault: ir.EncodingJSON,
},
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,
},
"problemjson.yml": {
"application/problem+json": ir.EncodingProblemJSON,
},
},
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",
"array defaults",
"type-based discrimination with same jxType",
},
"manga.json": {},
"telegram_bot_api.json": {},
"gotd_bot_api.json": {},
"k8s.json": {},
"petstore-expanded.yml": {},
"problemjson.yml": {},
"redoc/discriminator.json": {},
"swagger-petstore-1.0.27.yaml": {
"nested objects in form parameters",
},
}))
}
// TestDuplicatePathsDifferentMethods tests that the generator correctly handles
// paths that normalize to the same structure but have different HTTP methods.
func TestDuplicatePathsDifferentMethods(t *testing.T) {
log := zaptest.NewLogger(t)
a := require.New(t)
specYAML := `
openapi: 3.0.3
info:
title: Test API
version: 1.0.0
paths:
/pets/{petId}:
get:
operationId: getPet
parameters:
- name: petId
in: path
required: true
schema:
type: string
responses:
'200':
description: OK
/pets/{id}:
post:
operationId: createPet
parameters:
- name: id
in: path
required: true
schema:
type: string
responses:
'200':
description: OK
`
spec, err := ogen.Parse([]byte(specYAML))
a.NoError(err)
opt := gen.Options{
Parser: gen.ParseOptions{
File: location.NewFile("test.yaml", "test.yaml", []byte(specYAML)),
},
Logger: log,
}
g, err := gen.NewGenerator(spec, opt)
a.NoError(err)
// Verify both operations were generated
ops := g.Operations()
a.Len(ops, 2)
var foundGet, foundPost bool
for _, op := range ops {
switch op.Spec.OperationID {
case "getPet":
foundGet = true
a.Equal("get", op.Spec.HTTPMethod)
case "createPet":
foundPost = true
a.Equal("post", op.Spec.HTTPMethod)
}
}
a.True(foundGet, "GET operation not found")
a.True(foundPost, "POST operation not found")
// Also verify that we can write the generated files without error
err = g.WriteSource(genfs.CheckFS{}, "api")
a.NoError(err)
}
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)
}
})
}