-
Notifications
You must be signed in to change notification settings - Fork 39
/
format.go
244 lines (209 loc) · 5.03 KB
/
format.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
package schema
import (
"bytes"
"sort"
"github.com/getkin/kin-openapi/openapi3"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/seal-io/walrus/pkg/dao/types"
"github.com/seal-io/walrus/pkg/templates/loader"
"github.com/seal-io/walrus/pkg/templates/openapi"
"github.com/seal-io/walrus/utils/json"
)
var schemaSequence = []string{
"title",
"type",
"description",
"default",
"format",
"enum",
"example",
"externalDocs",
"not",
"oneOf",
"anyOf",
"allOf",
// Array related.
"uniqueItems",
"minItems",
"maxItems",
"items",
// Properties.
"nullable",
"readOnly",
"writeOnly",
"allowEmptyValue",
"deprecated",
// Number related.
"exclusiveMinimum",
"exclusiveMaximum",
"minimum",
"maximum",
"multipleOf",
// String related.
"minLength",
"maxLength",
"pattern",
// Object related.
"required",
"properties",
"minProperties",
"maxProperties",
"additionalProperties",
"discriminator",
}
// WrapOpenAPI is a wrapper of openapi3.T, used to generated formatted yaml.
type WrapOpenAPI struct {
OpenAPI string `json:"openapi" yaml:"openapi"`
Info WrapInfo `json:"info" yaml:"info"`
Components WrapComponents `json:"components,omitempty" yaml:"components,omitempty"`
}
// WrapInfo is a wrapper of openapi3.Info and removed field version, used to generated formatted yaml.
type WrapInfo struct {
Title string `json:"title" yaml:"title"`
Extensions map[string]any `json:",inline" yaml:",inline"`
}
// WrapComponents is a wrapper of openapi3.Components, used to generated formatted yaml.
type WrapComponents struct {
Schemas map[string]any `json:"schemas,omitempty" yaml:"schemas,omitempty"`
}
// FormattedOpenAPI generates formatted openapi yaml.
func FormattedOpenAPI(s *loader.SchemaGroup) ([]byte, error) {
// 1. Get variables sequence from original schema.
vs := s.Schema.VariableSchema()
oseq := openapi.GetExtOriginal(vs.Extensions).VariablesSequence
// 2. Get merged schema.
var (
merged *openapi3.Schema
err error
)
es := s.Schema.Expose(openapi.WalrusContextVariableName)
if es.IsEmpty() {
return nil, nil
}
if s.UISchema.IsEmpty() {
merged = es.VariableSchema()
} else {
merged, err = openapi.UnionSchema(es.VariableSchema(), s.UISchema.VariableSchema())
if err != nil {
return nil, err
}
}
// 3. Generate merged sequence.
seq := make([]string, 0)
for _, v := range oseq {
if v == openapi.WalrusContextVariableName {
continue
}
seq = append(seq, v)
}
exist := sets.NewString(seq...)
for n := range merged.Properties {
if !exist.Has(n) {
seq = append(seq, n)
}
}
// 4. Sorted Variables to the same sequence original defined, since the properties is a map,
// so need to generate sorted map.
// 4.1 First convert to the json.
propsJsonByte, err := json.Marshal(merged.Properties)
if err != nil {
return nil, err
}
// 4.2 Then properties convert to yaml.
propMap := make(map[string]map[string]any)
err = yaml.Unmarshal(propsJsonByte, &propMap)
if err != nil {
return nil, err
}
// 4.3 Generate key sorted properties.
sortedProps := make(yaml.MapSlice, len(propMap))
for i, v := range seq {
sortedSchema := sortWithSequence(schemaSequence, propMap[v])
sortedProps[i] = yaml.MapItem{
Key: v,
Value: sortedSchema,
}
}
// 5. Generate final schema.
w := WrapOpenAPI{
OpenAPI: es.OpenAPISchema.OpenAPI,
Info: WrapInfo{
Title: es.OpenAPISchema.Info.Title,
Extensions: es.OpenAPISchema.Info.Extensions,
},
Components: WrapComponents{
Schemas: map[string]any{
types.VariableSchemaKey: genVariable(*merged, sortedProps),
},
},
}
// 6. Convert to yaml.
var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
err = enc.Encode(w)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func genVariable(esv openapi3.Schema, props yaml.MapSlice) yaml.MapSlice {
sortedVariablesSchema := yaml.MapSlice{
{
Key: "type",
Value: "object",
},
}
if len(esv.Required) != 0 {
sortedVariablesSchema = append(sortedVariablesSchema, yaml.MapItem{
Key: "required",
Value: esv.Required,
})
}
if len(props) != 0 {
sortedVariablesSchema = append(sortedVariablesSchema, yaml.MapItem{
Key: "properties",
Value: props,
})
}
extUI := openapi.GetExtUI(esv.Extensions)
if !extUI.IsEmpty() {
sortedVariablesSchema = append(sortedVariablesSchema, yaml.MapItem{
Key: openapi.ExtUIKey,
Value: extUI,
})
}
return sortedVariablesSchema
}
func sortWithSequence(seq []string, m map[string]any) yaml.MapSlice {
seqKeys := make(map[string]int, len(seq))
for i, v := range seq {
seqKeys[v] = i
}
keys := make([]string, 0)
for k := range m {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool {
si, ok1 := seqKeys[keys[i]]
sj, ok2 := seqKeys[keys[j]]
switch {
case ok1 && ok2:
return si < sj
case ok1:
return true
case ok2:
return false
default:
return keys[i] < keys[j]
}
})
result := make(yaml.MapSlice, len(keys))
for i, v := range keys {
result[i] = yaml.MapItem{
Key: v,
Value: m[v],
}
}
return result
}