-
-
Notifications
You must be signed in to change notification settings - Fork 437
/
Copy pathopenapi3gen_test.go
111 lines (104 loc) · 2.26 KB
/
openapi3gen_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
package openapi3gen
import (
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/require"
)
type CyclicType0 struct {
CyclicField *CyclicType1 `json:"a"`
}
type CyclicType1 struct {
CyclicField *CyclicType0 `json:"b"`
}
func TestCyclic(t *testing.T) {
schema, refsMap, err := NewSchemaRefForValue(&CyclicType0{})
require.IsType(t, &CycleError{}, err)
require.Nil(t, schema)
require.Empty(t, refsMap)
}
func TestSimple(t *testing.T) {
type ExampleChild string
type Example struct {
Bool bool `json:"bool"`
Int int `json:"int"`
Int64 int64 `json:"int64"`
Float64 float64 `json:"float64"`
String string `json:"string"`
Bytes []byte `json:"bytes"`
JSON json.RawMessage `json:"json"`
Time time.Time `json:"time"`
Slice []*ExampleChild `json:"slice"`
Map map[string]*ExampleChild `json:"map"`
Struct struct {
X string `json:"x"`
} `json:"struct"`
EmptyStruct struct {
X string
} `json:"structWithoutFields"`
Ptr *ExampleChild `json:"ptr"`
}
schema, refsMap, err := NewSchemaRefForValue(&Example{})
require.NoError(t, err)
require.Len(t, refsMap, 14)
data, err := json.Marshal(schema)
require.NoError(t, err)
require.JSONEq(t, expectedSimple, string(data))
}
const expectedSimple = `
{
"type": "object",
"properties": {
"bool": {
"type": "boolean"
},
"int": {
"type": "integer",
"format": "int64"
},
"int64": {
"type": "integer",
"format": "int64"
},
"float64": {
"type": "number"
},
"time": {
"type": "string",
"format": "date-time"
},
"string": {
"type": "string"
},
"bytes": {
"type": "string",
"format": "byte"
},
"json": {},
"slice": {
"type": "array",
"items": {
"type": "string"
}
},
"map": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"struct": {
"type": "object",
"properties": {
"x": {
"type": "string"
}
}
},
"structWithoutFields": {},
"ptr": {
"type": "string"
}
}
}
`