/
expand.go
334 lines (284 loc) · 8.8 KB
/
expand.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
package coverage
import (
"fmt"
"path/filepath"
"strings"
"github.com/go-openapi/loads"
openapiSpec "github.com/go-openapi/spec"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/sirupsen/logrus"
)
// http://azure.github.io/autorest/extensions/#x-ms-discriminator-value
const msExtensionDiscriminator = "x-ms-discriminator-value"
const msExtensionSecret = "x-ms-secret"
var (
// {swaggerPath: doc Object}
swaggerCache, _ = lru.New[string, *loads.Document](30)
// {swaggerPath: {parentModelName: {childModelName: nil}}}
allOfTableCache, _ = lru.New[string, map[string]map[string]interface{}](10)
)
func loadSwagger(swaggerPath string) (*loads.Document, error) {
if doc, ok := swaggerCache.Get(swaggerPath); ok {
return doc, nil
}
doc, err := loads.JSONSpec(swaggerPath)
if err != nil {
return nil, err
}
swaggerCache.Add(swaggerPath, doc)
return doc, nil
}
func getAllOfTable(swaggerPath string) (map[string]map[string]interface{}, error) {
if vt, ok := allOfTableCache.Get(swaggerPath); ok {
return vt, nil
}
doc, err := loadSwagger(swaggerPath)
if err != nil {
return nil, err
}
spec := doc.Spec()
allOfTable := map[string]map[string]interface{}{}
for k, v := range spec.Definitions {
if len(v.AllOf) > 0 {
for _, allOf := range v.AllOf {
if allOf.Ref.String() != "" {
modelName, absPath := SchemaNamePathFromRef(swaggerPath, allOf.Ref)
if absPath != swaggerPath {
continue
}
if _, ok := allOfTable[modelName]; !ok {
allOfTable[modelName] = map[string]interface{}{}
}
allOfTable[modelName][k] = nil
}
}
}
}
allOfTableCache.Add(swaggerPath, allOfTable)
return allOfTable, nil
}
func Expand(modelName, swaggerPath string) (*Model, error) {
if modelName == "" {
return nil, fmt.Errorf("modelName is empty")
}
doc, err := loadSwagger(swaggerPath)
if err != nil {
return nil, err
}
spec := doc.Spec()
modelSchema, ok := spec.Definitions[modelName]
if !ok {
return nil, fmt.Errorf("%s not found in the definition of %s", modelName, swaggerPath)
}
output := expandSchema(modelSchema, swaggerPath, modelName, "#", spec, map[string]interface{}{}, map[string]interface{}{})
return output, nil
}
func expandSchema(input openapiSpec.Schema, swaggerPath, modelName, identifier string, root interface{}, resolvedDiscriminator map[string]interface{}, resolvedModel map[string]interface{}) *Model {
output := Model{
Identifier: identifier,
ModelName: modelName,
SourceFile: swaggerPath,
}
if _, ok := resolvedModel[modelName]; ok {
return &output
}
resolvedModel[modelName] = nil
if len(input.Type) > 0 {
output.Type = &input.Type[0]
if *output.Type == "boolean" {
boolMap := make(map[string]bool)
boolMap["true"] = false
boolMap["false"] = false
output.Bool = &boolMap
}
}
if input.AdditionalProperties != nil {
output.HasAdditionalProperties = true
}
if input.Format != "" {
output.Format = &input.Format
}
if input.ReadOnly {
output.IsReadOnly = input.ReadOnly
}
if isSecretRaw, ok := input.Extensions[msExtensionSecret]; ok && isSecretRaw != nil {
if isSecret, ok := isSecretRaw.(bool); ok {
output.IsSecret = isSecret
}
}
if input.Enum != nil {
enumMap := make(map[string]bool)
for _, v := range input.Enum {
switch t := v.(type) {
case string:
enumMap[t] = false
case float64:
enumMap[fmt.Sprintf("%v", t)] = false
case int:
enumMap[fmt.Sprintf("%v", t)] = false
default:
logrus.Errorf("unknown enum type %T", t)
enumMap[fmt.Sprintf("%v", t)] = false
}
}
output.Enum = &enumMap
}
properties := make(map[string]*Model)
// expand ref
if input.Ref.String() != "" {
resolved, err := openapiSpec.ResolveRefWithBase(root, &input.Ref, &openapiSpec.ExpandOptions{RelativeBase: swaggerPath})
if err != nil {
logrus.Panicf("resolve ref %s from %s: %+v", input.Ref.String(), swaggerPath, err)
}
modelName, refSwaggerPath := SchemaNamePathFromRef(swaggerPath, input.Ref)
refRoot := root
if refSwaggerPath != swaggerPath {
doc, err := loadSwagger(refSwaggerPath)
if err != nil {
logrus.Panicf("load swagger %s: %+v", refSwaggerPath, err)
}
refRoot = doc.Spec()
}
referenceModel := expandSchema(*resolved, refSwaggerPath, modelName, identifier, refRoot, resolvedDiscriminator, resolvedModel)
if referenceModel.Properties != nil {
for k, v := range *referenceModel.Properties {
properties[k] = v
}
}
if referenceModel.Enum != nil {
output.Enum = referenceModel.Enum
}
if referenceModel.Type != nil {
output.Type = referenceModel.Type
}
if referenceModel.Format != nil {
output.Format = referenceModel.Format
}
if referenceModel.HasAdditionalProperties {
output.HasAdditionalProperties = true
}
if referenceModel.Bool != nil {
output.Bool = referenceModel.Bool
}
if referenceModel.IsReadOnly {
output.IsReadOnly = referenceModel.IsReadOnly
}
if referenceModel.IsRequired {
output.IsRequired = referenceModel.IsRequired
}
if referenceModel.Discriminator != nil {
output.Discriminator = referenceModel.Discriminator
}
if referenceModel.Variants != nil {
output.Variants = referenceModel.Variants
}
if referenceModel.Item != nil {
output.Item = referenceModel.Item
}
}
// expand properties
for k, v := range input.Properties {
properties[k] = expandSchema(v, swaggerPath, fmt.Sprintf("%s.%s", modelName, k), fmt.Sprintf("%s.%s", identifier, k), root, resolvedDiscriminator, resolvedModel)
}
// expand composition
for _, v := range input.AllOf {
allOf := expandSchema(v, swaggerPath, fmt.Sprintf("%s.allOf", modelName), identifier, root, resolvedDiscriminator, resolvedModel)
if allOf.Properties != nil {
for k, v := range *allOf.Properties {
properties[k] = v
}
}
// the model should be a variant if its allOf contains a discriminator
if allOf.Discriminator != nil {
output.Discriminator = allOf.Discriminator
variantName := modelName
if variantNameRaw, ok := input.Extensions[msExtensionDiscriminator]; ok && variantNameRaw != nil {
variantName = variantNameRaw.(string)
}
output.VariantType = &variantName
}
}
if len(properties) > 0 {
for _, v := range input.Required {
if p, ok := properties[v]; ok {
p.IsRequired = true
} else {
logrus.Warnf("required property %s not found in %s", v, modelName)
}
}
// check if all properties are readonly
allReadOnly := true
for _, v := range properties {
if !v.IsReadOnly {
allReadOnly = false
break
}
}
if allReadOnly {
output.IsReadOnly = true
}
output.Properties = &properties
}
// expand items
if input.Items != nil {
item := expandSchema(*input.Items.Schema, swaggerPath, fmt.Sprintf("%s[]", modelName), fmt.Sprintf("%s[]", identifier), root, resolvedDiscriminator, resolvedModel)
output.Item = item
}
delete(resolvedModel, modelName)
// expand variants
if input.Discriminator != "" || output.Discriminator != nil {
if _, hasResolvedDiscriminator := resolvedDiscriminator[modelName]; !hasResolvedDiscriminator {
allOfTable, err := getAllOfTable(swaggerPath)
if err != nil {
logrus.Panicf("get variant table %s: %+v", swaggerPath, err)
}
varSet, ok := allOfTable[modelName]
if ok {
resolvedDiscriminator[modelName] = nil
variants := map[string]*Model{}
// level order traverse to find all variants
for len(varSet) > 0 {
tempVarSet := make(map[string]interface{})
for variantModelName := range varSet {
schema := root.(*openapiSpec.Swagger).Definitions[variantModelName]
variantName := variantModelName
if variantNameRaw, ok := schema.Extensions[msExtensionDiscriminator]; ok && variantNameRaw != nil {
variantName = variantNameRaw.(string)
}
resolved := expandSchema(schema, swaggerPath, variantModelName, fmt.Sprintf("%s{%s}", identifier, variantName), root, resolvedDiscriminator, resolvedModel)
resolved.VariantType = &variantName
// in case of https://github.com/Azure/azure-rest-api-specs/issues/25104, use modelName as key
variants[variantModelName] = resolved
if varVarSet, ok := allOfTable[variantModelName]; ok {
for v := range varVarSet {
tempVarSet[v] = nil
}
}
}
varSet = tempVarSet
}
delete(resolvedDiscriminator, modelName)
if input.Discriminator != "" {
output.Discriminator = &input.Discriminator
}
output.Variants = &variants
}
}
}
return &output
}
func SchemaNamePathFromRef(swaggerPath string, ref openapiSpec.Ref) (schemaName string, schemaPath string) {
refUrl := ref.GetURL()
if refUrl == nil {
return "", ""
}
schemaPath = refUrl.Path
if schemaPath == "" {
schemaPath = swaggerPath
} else {
swaggerPath, _ := filepath.Split(swaggerPath)
schemaPath = swaggerPath + schemaPath
}
fragments := strings.Split(refUrl.Fragment, "/")
return fragments[len(fragments)-1], schemaPath
}