/
schemav3.go
141 lines (121 loc) · 2.97 KB
/
schemav3.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
package swaggo
import (
"errors"
"github.com/sv-tools/openapi/spec"
)
// PrimitiveSchemaV3 build a primitive schema.
func PrimitiveSchemaV3(refType string) *spec.RefOrSpec[spec.Schema] {
result := spec.NewSchemaSpec()
result.Spec.Type = spec.SingleOrArray[string]{refType}
return result
}
// IsComplexSchemaV3 whether a schema is complex and should be a ref schema
func IsComplexSchemaV3(schema *SchemaV3) bool {
// a enum type should be complex
if len(schema.Enum) > 0 {
return true
}
// a deep array type is complex, how to determine deep? here more than 2 ,for example: [][]object,[][][]int
if len(schema.Type) > 2 {
return true
}
//Object included, such as Object or []Object
for _, st := range schema.Type {
if st == OBJECT {
return true
}
}
return false
}
// RefSchemaV3 build a reference schema.
func RefSchemaV3(refType string) *spec.RefOrSpec[spec.Schema] {
return spec.NewRefOrSpec[spec.Schema](spec.NewRef("#/components/schemas/"+refType), nil)
}
// BuildCustomSchemaV3 build custom schema specified by tag swaggertype.
func BuildCustomSchemaV3(types []string) (*spec.RefOrSpec[spec.Schema], error) {
if len(types) == 0 {
return nil, nil
}
switch types[0] {
case PRIMITIVE:
if len(types) == 1 {
return nil, errors.New("need primitive type after primitive")
}
return BuildCustomSchemaV3(types[1:])
case ARRAY:
if len(types) == 1 {
return nil, errors.New("need array item type after array")
}
schema, err := BuildCustomSchemaV3(types[1:])
if err != nil {
return nil, err
}
// TODO: check if this is correct
result := spec.NewSchemaSpec()
result.Spec.Type = []string{"array"}
result.Spec.AdditionalProperties = spec.NewBoolOrSchema(true, schema)
return result, nil
case OBJECT:
if len(types) == 1 {
return PrimitiveSchemaV3(types[0]), nil
}
schema, err := BuildCustomSchemaV3(types[1:])
if err != nil {
return nil, err
}
result := spec.NewSchemaSpec()
result.Spec.AdditionalProperties = spec.NewBoolOrSchema(true, schema)
result.Spec.Type = spec.NewSingleOrArray("object")
return result, nil
default:
err := CheckSchemaType(types[0])
if err != nil {
return nil, err
}
return PrimitiveSchemaV3(types[0]), nil
}
}
// TransToValidCollectionFormatV3 determine valid collection format.
func TransToValidCollectionFormatV3(format, in string) string {
switch in {
case "query":
switch format {
case "form", "spaceDelimited", "pipeDelimited", "deepObject":
return format
case "ssv":
return "spaceDelimited"
case "pipes":
return "pipe"
case "multi":
return "form"
case "csv":
return "form"
default:
return ""
}
case "path":
switch format {
case "matrix", "label", "simple":
return format
case "csv":
return "simple"
default:
return ""
}
case "header":
switch format {
case "form", "simple":
return format
case "csv":
return "simple"
default:
return ""
}
case "cookie":
switch format {
case "form":
return format
}
}
return ""
}