forked from lazada/swgen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator_test.go
689 lines (558 loc) · 22.5 KB
/
generator_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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
package swgen
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/swaggest/assertjson"
"github.com/swaggest/openapi-go/openapi3"
"github.com/swaggest/swgen/internal/sample"
"github.com/swaggest/swgen/internal/sample/experiment"
"github.com/swaggest/swgen/internal/sample/experiment/variation"
"github.com/yudai/gojsondiff"
"github.com/yudai/gojsondiff/formatter"
)
type TestSampleStruct struct {
SimpleString string `json:"simple_string"`
SimpleInt int `json:"simple_int"`
Sub TestSubStruct `json:"sub"`
SubSlice []TestSubStruct `json:"sub_slice"`
}
type TestSubStruct struct {
SubInt int `json:"sample_int"`
}
type testEmptyStruct struct{}
type testSimpleStruct struct {
SimpleString string `json:"simple_string"`
SimpleInt int `json:"simple_int" required:"true"`
SimpleInt32 int32 `json:"simple_int32"`
SimpleInt64 int64 `json:"simple_int64"`
SimpleUInt32 uint32 `json:"simple_uint32"`
SimpleUInt64 uint64 `json:"simple_uint64"`
SimpleFloat32 float32 `json:"simple_float32"`
SimpleFloat64 float64 `json:"simple_float64"`
SimpleBool bool `json:"simple_bool"`
IgnoreField string `json:"-"`
}
type testSimpleQueryStruct struct {
SimpleString string `query:"simple_string"`
SimpleInt int `query:"simple_int"`
SimpleInt32 int32 `query:"simple_int32"`
SimpleInt64 int64 `query:"simple_int64"`
SimpleUInt32 uint32 `query:"simple_uint32"`
SimpleUInt64 uint64 `query:"simple_uint64"`
SimpleFloat32 float32 `query:"simple_float32"`
SimpleFloat64 float64 `query:"simple_float64"`
SimpleBool bool `query:"simple_bool"`
IgnoreField string `query:"-"`
}
type testSimpleSlices struct {
ListString []string `json:"list_string"`
ListInt []int `json:"list_int"`
ListInt32 []int32 `json:"list_int32"`
ListInt64 []int64 `json:"list_int64"`
ListUInt32 []uint32 `json:"list_uint32"`
ListUInt64 []uint64 `json:"list_uint64"`
ListFloat32 []float32 `json:"list_float32"`
ListFloat64 []float64 `json:"list_float64"`
ListBool []bool `json:"list_bool"`
}
type testSimpleMaps struct {
MapString map[string]string `json:"map_string"`
MapInt map[string]int `json:"map_int"`
MapInt32 map[string]int32 `json:"map_int32"`
MapInt64 map[string]int64 `json:"map_int64"`
MapUInt32 map[string]uint32 `json:"map_uint32"`
MapUInt64 map[string]uint64 `json:"map_uint64"`
MapFloat32 map[string]float32 `json:"map_float32"`
MapFloat64 map[string]float64 `json:"map_float64"`
MapBool map[string]bool `json:"map_bool"`
}
type testSimpleMapList struct {
MapListString []map[string]string `json:"map_list_string"`
MapListInt []map[string]int `json:"map_list_int"`
MapListInt32 []map[string]int32 `json:"map_list_int32"`
MapListInt64 []map[string]int64 `json:"map_list_int64"`
MapListUInt32 []map[string]uint32 `json:"map_list_uint32"`
MapListUInt64 []map[string]uint64 `json:"map_list_uint64"`
MapListFloat32 []map[string]float32 `json:"map_list_float32"`
MapListFloat64 []map[string]float64 `json:"map_list_float64"`
MapListBool []map[string]bool `json:"map_list_bool"`
}
type testSubTypes struct {
TestSimpleStruct testSimpleStruct `json:"test_simple_struct"`
TestSimpleSlices testSimpleSlices `json:"test_simple_slices"`
TestSimpleMaps testSimpleMaps `json:"test_simple_maps"`
TestSimpleMapList testSimpleMapList `json:"test_simple_map_list"`
}
type testPathParam struct {
ID uint64 `json:"id" path:"id" required:"false"`
Cat string `json:"category" path:"category"`
}
type simpleTestReplacement struct {
ID uint64 `json:"id"`
Cat string `json:"category"`
}
type deepReplacementTag struct {
TestField1 string `json:"test_field_1" type:"number" format:"double"`
}
type testWrapParams struct {
SimpleTestReplacement simpleTestReplacement `json:"simple_test_replacement"`
ReplaceByTag int `json:"should_be_sting" type:"string" format:"-"`
DeepReplacementTag deepReplacementTag `json:"deep_replacement"`
}
type simpleDateTime struct {
Time time.Time `json:"time"`
}
type sliceDateTime struct {
Items []simpleDateTime `json:"items"`
}
type mapDateTime struct {
Items map[string]simpleDateTime `json:"items"`
}
type paramStructMap struct {
Field1 int `query:"field1"`
Field2 string `query:"field2"`
Field3 simpleTestReplacement `query:"field3"`
Field4 []int64 `query:"field4" collectionFormat:"csv"`
}
type AnonymousField struct {
AnonProp int `json:"anonProp"`
}
type mixedStruct struct {
AnonymousField
FieldQuery int `query:"fieldQuery"`
FieldBody int `json:"fieldBody"`
}
type typeMapHolder struct {
M typeMap `json:"m"`
}
type typeMap struct {
R1 int `json:"1"`
R2 int `json:"2"`
R3 int `json:"3"`
R4 int `json:"4"`
R5 int `json:"5"`
}
type Gender int
func (Gender) NamedEnum() ([]interface{}, []string) {
return []interface{}{
PreferNotToDisclose,
Male,
Female,
LGBT,
}, []string{
"PreferNotToDisclose",
"Male",
"Female",
"LGBT",
}
}
const (
PreferNotToDisclose Gender = iota
Male
Female
LGBT
)
type Flag string
func (Flag) NamedEnum() ([]interface{}, []string) {
return []interface{}{Flag("Foo"), Flag("Bar")}, []string{"Foo", "Bar"}
}
type mixedStructWithEnumer struct {
Gender Gender `query:"gender"`
Flag Flag `query:"flag"`
}
type sliceType []testSimpleStruct
type NullFloat64 struct{}
func (NullFloat64) SwaggerDef() SwaggerData {
typeDef := schemaFromCommonName(commonNameFloat)
typeDef.TypeName = "NullFloat64"
return SwaggerData{
CommonFields: typeDef.CommonFields,
SchemaObj: typeDef,
}
}
type NullBool struct{}
func (NullBool) SwaggerDef() SwaggerData {
typeDef := schemaFromCommonName(commonNameBoolean)
typeDef.TypeName = "NullBool"
return SwaggerData{
CommonFields: typeDef.CommonFields,
SchemaObj: typeDef,
}
}
type NullString struct{}
func (NullString) SwaggerDef() SwaggerData {
typeDef := schemaFromCommonName(commonNameString)
typeDef.TypeName = "NullString"
return SwaggerData{
CommonFields: typeDef.CommonFields,
SchemaObj: typeDef,
}
}
type NullInt64 struct{}
func (NullInt64) SwaggerDef() SwaggerData {
typeDef := schemaFromCommonName(commonNameLong)
typeDef.TypeName = "NullInt64"
return SwaggerData{
CommonFields: typeDef.CommonFields,
SchemaObj: typeDef,
}
}
type NullDateTime struct{}
func (NullDateTime) SwaggerDef() SwaggerData {
typeDef := schemaFromCommonName(commonNameDateTime)
typeDef.TypeName = "NullDateTime"
return SwaggerData{
CommonFields: typeDef.CommonFields,
SchemaObj: typeDef,
}
}
type NullDate struct{}
func (NullDate) SwaggerDef() SwaggerData {
typeDef := schemaFromCommonName(commonNameDate)
typeDef.TypeName = "NullDate"
return SwaggerData{
CommonFields: typeDef.CommonFields,
SchemaObj: typeDef,
}
}
type NullTimestamp struct{}
func (NullTimestamp) SwaggerDef() SwaggerData {
typeDef := schemaFromCommonName(commonNameLong)
typeDef.TypeName = "NullTimestamp"
return SwaggerData{
CommonFields: typeDef.CommonFields,
SchemaObj: typeDef,
}
}
type testDefaults struct {
Field1 int `json:"field1" default:"25"`
Field2 float64 `json:"field2" default:"25.5"`
Field3 string `json:"field3" default:"test"`
Field4 bool `json:"field4" default:"true"`
Field5 []int `json:"field5" default:"[1, 2, 3]"`
Field6 map[string]int `json:"field6" default:"{\"test\": 1}"`
Field7 *uint `json:"field7" default:"25"`
}
type NullTypes struct {
Float NullFloat64 `json:"null_float"`
Bool NullBool `json:"null_bool"`
String NullString `json:"null_string"`
Int NullInt64 `json:"null_int"`
DateTime NullDateTime `json:"null_date_time"`
Date NullDate `json:"null_date"`
Timestamp NullTimestamp `json:"null_timestamp"`
}
type Unknown struct {
Anything interface{} `json:"anything"`
Whatever *json.RawMessage `json:"whatever"`
}
var _ SchemaDefinition = definitionExample{}
type definitionExample struct{}
func (defEx definitionExample) SwaggerDef() SwaggerData {
return SwaggerData{CommonFields: CommonFields{
Type: "string",
Format: "byte",
}}
}
func createPathItemInfo(path, method, title, description, tag string, deprecated bool, request, response interface{}) PathItemInfo {
return PathItemInfo{
Path: path,
Method: method,
Title: title,
Description: description,
Tag: tag,
Deprecated: deprecated,
Request: request,
Response: response,
}
}
func TestREST(t *testing.T) {
gen := NewGenerator()
oas3 := openapi3.Reflector{}
gen.SetOAS3Proxy(&oas3)
gen.SetHost("localhost").
SetBasePath("/").
SetInfo("swgen title", "swgen description", "term", "2.0").
SetLicense("BEER-WARE", "https://fedoraproject.org/wiki/Licensing/Beerware").
SetContact("Dylan Noblitt", "http://example.com", "[email protected]").
ReflectGoTypes(true).
IndentJSON(true)
gen.AddTypeMap(simpleTestReplacement{}, "")
gen.AddTypeMap(sliceType{}, float64(0))
gen.AddTypeMap(typeMap{}, map[string]int{})
var emptyInterface interface{}
gen.SetPathItem(createPathItemInfo("/V1/test1", "GET", "test1 name",
"test1 description", "v1", false, emptyInterface, testSimpleStruct{}))
gen.SetPathItem(createPathItemInfo("/V1/test2", "GET", "test2 name",
"test2 description", "v1", false, testSimpleQueryStruct{}, testSimpleSlices{}))
gen.SetPathItem(createPathItemInfo("/V1/test3", "PUT", "test3 name",
"test3 description", "v1", false, testSimpleSlices{}, testSimpleMaps{}))
gen.SetPathItem(createPathItemInfo("/V1/test4", "POST", "test4 name",
"test4 description", "v1", false, testSimpleMaps{}, testSimpleMapList{}))
gen.SetPathItem(createPathItemInfo("/V1/test5", "POST", "test5 name",
"test5 description", "v1", false, testSimpleMapList{}, testSubTypes{}))
gen.SetPathItem(createPathItemInfo("/V1/test6", "PATCH", "test6 name",
"test6 description", "v1", false, testSubTypes{}, testSimpleStruct{}))
gen.SetPathItem(createPathItemInfo("/V1/test7", "OPTIONS", "test7 name",
"test7 description", "v1", false, emptyInterface, testSimpleSlices{}))
gen.SetPathItem(createPathItemInfo("/V1/test8", "GET", "test8v1 name",
"test8v1 description", "v1", false, paramStructMap{}, map[string]testSimpleStruct{}))
gen.SetPathItem(createPathItemInfo("/V1/test9", "POST", "test9 name",
"test9 description", "v1", false, mixedStruct{}, map[string]testSimpleStruct{}))
gen.SetPathItem(createPathItemInfo("/V1/combine", "GET", "test1 name",
"test1 description", "v1", true, emptyInterface, testSimpleStruct{}))
gen.SetPathItem(createPathItemInfo("/V1/combine", "PUT", "test3 name",
"test3 description", "v1", true, testSimpleSlices{}, testSimpleMaps{}))
gen.SetPathItem(createPathItemInfo("/V1/combine", "POST", "test4 name",
"test4 description", "v1", true, testSimpleMaps{}, testSimpleMapList{}))
gen.SetPathItem(createPathItemInfo("/V1/combine", "DELETE", "test5 name",
"test5 description", "v1", true, testSimpleMapList{}, testSubTypes{}))
gen.SetPathItem(createPathItemInfo("/V1/combine", "PATCH", "test6 name",
"test6 description", "v1", true, testSubTypes{}, testSimpleStruct{}))
gen.SetPathItem(createPathItemInfo("/V1/combine", "OPTIONS", "test7 name",
"test7 description", "v1", true, testSubTypes{}, testSimpleStruct{}))
gen.SetPathItem(createPathItemInfo("/V1/pathParams/{category:[a-zA-Z]{32}}/{id:[0-9]+}", "GET", "test8 name",
"test8 description", "V1", false, testPathParam{}, testSimpleStruct{}))
// anonymous types:
gen.SetPathItem(createPathItemInfo("/V1/anonymous1", "POST", "test10 name",
"test10 description", "v1", false, testSimpleStruct{}, map[string]int64{}))
gen.SetPathItem(createPathItemInfo("/V1/anonymous2", "POST", "test11 name",
"test11 description", "v1", false, testSimpleStruct{}, map[float64]string{}))
gen.SetPathItem(createPathItemInfo("/V1/anonymous3", "POST", "test12 name",
"test12 description", "v1", false, testSimpleStruct{}, []string{}))
gen.SetPathItem(createPathItemInfo("/V1/anonymous4", "POST", "test13 name",
"test13 description", "v1", false, testSimpleStruct{}, []int{}))
gen.SetPathItem(createPathItemInfo("/V1/anonymous5", "POST", "test14 name",
"test14 description", "v1", false, testSimpleStruct{}, ""))
gen.SetPathItem(createPathItemInfo("/V1/anonymous6", "POST", "test15 name",
"test15 description", "v1", false, testSimpleStruct{}, true))
gen.SetPathItem(createPathItemInfo("/V1/anonymous7", "POST", "test16 name",
"test16 description", "v1", false, testSimpleStruct{}, map[string]testSimpleStruct{}))
gen.SetPathItem(createPathItemInfo("/V1/typeReplacement1", "POST", "test9 name",
"test9 description", "v1", false, testSubTypes{}, testWrapParams{}))
gen.SetPathItem(createPathItemInfo("/V1/date1", "POST", "test date 1 name",
"test date 1 description", "v1", false, testSimpleStruct{}, simpleDateTime{}))
gen.SetPathItem(createPathItemInfo("/V1/date2", "POST", "test date 2 name",
"test date 2 description", "v1", false, testSimpleStruct{}, sliceDateTime{}))
gen.SetPathItem(createPathItemInfo("/V1/date3", "POST", "test date 3 name",
"test date 3 description", "v1", false, testSimpleStruct{}, mapDateTime{}))
gen.SetPathItem(createPathItemInfo("/V1/date4", "POST", "test date 4 name",
"test date 4 description", "v1", false, testSimpleStruct{}, []mapDateTime{}))
gen.SetPathItem(createPathItemInfo("/V1/slice1", "POST", "test slice 1 name",
"test slice 1 description", "v1", false, testSimpleStruct{}, []mapDateTime{}))
gen.SetPathItem(createPathItemInfo("/V1/slice2", "POST", "test slice 2 name",
"test slice 2 description", "v1", false, testSimpleStruct{}, sliceType{}))
gen.SetPathItem(createPathItemInfo("/V1/IDefinition1", "POST", "test IDefinition1 name",
"test IDefinition1 description", "v1", false, definitionExample{}, definitionExample{}))
gen.SetPathItem(createPathItemInfo("/V1/nullTypes", "POST", "test nulltypes",
"test nulltypes", "v1", false, NullTypes{}, NullTypes{}))
gen.SetPathItem(createPathItemInfo("/V1/primitiveTypes1", "POST", "testPrimitives",
"test Primitives", "v1", false, "", 10))
gen.SetPathItem(createPathItemInfo("/V1/primitiveTypes2", "POST", "testPrimitives",
"test Primitives", "v1", false, true, 1.1))
gen.SetPathItem(createPathItemInfo("/V1/primitiveTypes3", "POST", "testPrimitives",
"test Primitives", "v1", false, int64(10), ""))
gen.SetPathItem(createPathItemInfo("/V1/primitiveTypes4", "POST", "testPrimitives",
"test Primitives", "v1", false, int64(10), ""))
gen.SetPathItem(createPathItemInfo("/V1/defaults1", "GET", "default",
"test defaults", "v1", false, emptyInterface, testDefaults{}))
gen.SetPathItem(createPathItemInfo("/V1/unknown", "POST", "test unknown types",
"test unknown types", "v1", false, Unknown{}, Unknown{}))
gen.SetPathItem(createPathItemInfo("/V1/empty", "POST", "test empty struct",
"test empty struct", "v1", false, testEmptyStruct{}, testEmptyStruct{}))
gen.SetPathItem(createPathItemInfo("/V1/struct-collision", "POST", "test struct name collision",
"test struct name collision", "v1", false, TestSampleStruct{}, TestSampleStruct{}))
gen.SetPathItem(createPathItemInfo("/V2/struct-collision", "POST", "test struct name collision",
"test struct name collision", "v2", false, sample.TestSampleStruct{}, sample.TestSampleStruct{}))
gen.SetPathItem(createPathItemInfo("/V1/type-map", "POST", "test type mapping",
"test type mapping", "v1", false, nil, typeMapHolder{}))
bytes, err := gen.GenDocument()
assert.NoError(t, err)
assert.NoError(t, writeLastRun("test_REST_last_run.json", bytes))
expected := readTestFile(t, "test_REST.json")
assertjson.Equal(t, []byte(expected), bytes)
bytes, err = json.MarshalIndent(oas3.Spec, "", " ")
assert.NoError(t, err)
assert.NoError(t, writeLastRun("test_REST_OAS3_last_run.json", bytes))
expected = readTestFile(t, "test_REST_OAS3.json")
assertjson.Equal(t, []byte(expected), bytes)
}
func getTestDataDir(filename string) string {
pwd, err := os.Getwd()
if err != nil {
return filename
}
return path.Join(pwd, "testdata", filename)
}
func writeLastRun(filename string, data []byte) error {
return ioutil.WriteFile(getTestDataDir(filename), data, os.ModePerm)
}
func readTestFile(t *testing.T, filename string) string {
t.Helper()
bytes, readError := ioutil.ReadFile(getTestDataDir(filename))
assert.NoError(t, readError)
return string(bytes)
}
func coloredJSONDiff(expected, generated string) string {
expectedData := make(map[string]interface{})
generatedData := make(map[string]interface{})
expectedBytes := []byte(expected)
generatedBytes := []byte(generated)
if err := json.Unmarshal(expectedBytes, &expectedData); err != nil {
return fmt.Sprintf("can not unmarshal expected data: %s", err.Error())
}
if err := json.Unmarshal(generatedBytes, &generatedData); err != nil {
return fmt.Sprintf("can not unmarshal generated data: %s", err.Error())
}
diff, err := gojsondiff.New().Compare(expectedBytes, generatedBytes)
if err != nil {
return err.Error()
}
if diff.Modified() {
config := formatter.AsciiFormatterConfig{
Coloring: true,
ShowArrayIndex: true,
}
f := formatter.NewAsciiFormatter(expectedData, config)
diffString, err := f.Format(diff)
if err != nil {
panic(err)
}
return diffString
}
return ""
}
func TestCORSSupport(t *testing.T) {
g := NewGenerator()
g.EnableCORS(true, "X-ABC-Test").
SetHost("localhost:1234")
info := PathItemInfo{
Path: "/v1/test/handler",
Title: "TestHandler",
Description: "This is just a test handler with GET request",
Method: "GET",
}
g.SetPathItem(info)
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "http://localhost:1234/docs/swagger.json", nil)
assert.NoError(t, err)
g.ServeHTTP(w, r)
assert.Equal(t, "*", w.Header().Get("Access-Control-Allow-Origin"))
assert.Equal(t, "GET, POST, DELETE, PUT, PATCH, OPTIONS", w.Header().Get("Access-Control-Allow-Methods"))
assert.Equal(t, "Content-Type, api_key, Authorization, X-ABC-Test", w.Header().Get("Access-Control-Allow-Headers"))
}
func TestGenerator_JsonSchema(t *testing.T) {
gen := NewGenerator()
gen.SetHost("localhost")
gen.SetInfo("swgen title", "swgen description", "term", "2.0")
gen.IndentJSON(true)
info := PathItemInfo{
Method: "GET",
Path: "/any",
Request: new(mixedStructWithEnumer),
Response: new(map[string]testSimpleStruct),
}
obj := gen.SetPathItem(info)
jsonSchema, err := gen.JSONSchema(*obj.Responses[http.StatusOK].Schema)
assert.NoError(t, err)
jsonSchemaJSON, err := json.MarshalIndent(jsonSchema, "", " ")
assert.NoError(t, err)
assert.NoError(t, writeLastRun("test_ResponseJsonSchema_last_run.json", jsonSchemaJSON))
expected := readTestFile(t, "test_ResponseJsonSchema.json")
assert.JSONEq(t, expected, string(jsonSchemaJSON), coloredJSONDiff(expected, string(jsonSchemaJSON)))
jsonSchema, err = gen.ParamJSONSchema(obj.Parameters[0])
assert.NoError(t, err)
jsonSchemaJSON, err = json.MarshalIndent(jsonSchema, "", " ")
assert.NoError(t, err)
assert.NoError(t, writeLastRun("test_Param0JsonSchema_last_run.json", jsonSchemaJSON))
expected = readTestFile(t, "test_Param0JsonSchema.json")
assert.JSONEq(t, expected, string(jsonSchemaJSON), coloredJSONDiff(expected, string(jsonSchemaJSON)))
}
func TestGenerator_GenDocument_StructCollision(t *testing.T) {
gen := NewGenerator()
gen.SetHost("localhost")
gen.SetInfo("swgen title", "swgen description", "term", "2.0")
gen.IndentJSON(true)
gen.ReflectGoTypes(true)
info := PathItemInfo{
Method: http.MethodPost,
Path: "/any",
Request: new(experiment.PostRequest),
Response: new(experiment.Entity),
}
gen.SetPathItem(info)
generatedBytes, err := gen.GenDocument()
assert.NoError(t, err)
assert.NoError(t, writeLastRun("struct_collision_last_run.json", generatedBytes))
expected := readTestFile(t, "struct_collision.json")
assert.JSONEq(t, expected, string(generatedBytes), coloredJSONDiff(expected, string(generatedBytes)))
}
func TestGenerator_GenDocument_StructCollisionPackagePrefix(t *testing.T) {
gen := NewGenerator()
gen.SetHost("localhost")
gen.SetInfo("swgen title", "swgen description", "term", "2.0")
gen.IndentJSON(true)
gen.ReflectGoTypes(true)
gen.AddPackagePrefix(true)
info := PathItemInfo{
Method: http.MethodPost,
Path: "/any",
Request: new(experiment.PostRequest),
Response: new(experiment.Entity),
}
gen.SetPathItem(info)
generatedBytes, err := gen.GenDocument()
assert.NoError(t, err)
assert.NoError(t, writeLastRun("struct_collision_package_prefix_last_run.json", generatedBytes))
expected := readTestFile(t, "struct_collision_package_prefix.json")
assert.JSONEq(t, expected, string(generatedBytes), coloredJSONDiff(expected, string(generatedBytes)))
}
type (
experimentEntity experiment.Entity
experimentMetadata experiment.Metadata
experimentVariationEntity variation.Entity
experimentVariationMetadata variation.Metadata
)
func TestGenerator_GenDocument_StructCollisionWithExplicitRemapping(t *testing.T) {
gen := NewGenerator()
gen.SetHost("localhost")
gen.SetInfo("swgen title", "swgen description", "term", "2.0")
gen.IndentJSON(true)
// gen.ReflectGoTypes(true)
gen.AddTypeMap(new(experiment.Entity), new(experimentEntity))
gen.AddTypeMap(new(variation.Entity), new(experimentVariationEntity))
gen.AddTypeMap(new(experiment.Metadata), new(experimentMetadata))
gen.AddTypeMap(new(variation.Metadata), new(experimentVariationMetadata))
info := PathItemInfo{
Method: http.MethodPost,
Path: "/any",
Request: new(experiment.PostRequest),
Response: new(experiment.Entity),
}
gen.SetPathItem(info)
generatedBytes, err := gen.GenDocument()
assert.NoError(t, err)
assert.NoError(t, writeLastRun("struct_collision_mapped_last_run.json", generatedBytes))
expected := readTestFile(t, "struct_collision_mapped.json")
assert.JSONEq(t, expected, string(generatedBytes), coloredJSONDiff(expected, string(generatedBytes)))
}
func TestGenerator_GenDocument_CorrectRef(t *testing.T) {
gen := NewGenerator()
gen.IndentJSON(true)
gen.ReflectGoTypes(true)
gen.SetPathItem(createPathItemInfo("/V1/struct-collision", "POST", "test struct name collision", "test struct name collision", "v1", false, TestSampleStruct{}, TestSampleStruct{}))
gen.SetPathItem(createPathItemInfo("/V2/struct-collision", "POST", "test struct name collision", "test struct name collision", "v2", false, sample.TestSampleStruct{}, sample.TestSampleStruct{}))
bytes, err := gen.GenDocument()
assert.NoError(t, err)
assert.NoError(t, writeLastRun("struct_collision_correct_ref_last_run.json", bytes))
expected := readTestFile(t, "struct_collision_correct_ref.json")
assert.JSONEq(t, expected, string(bytes), coloredJSONDiff(expected, string(bytes)))
}