-
Notifications
You must be signed in to change notification settings - Fork 0
/
document.go
375 lines (317 loc) · 9.07 KB
/
document.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
package lib
import (
"github.com/cubahno/connexions/config"
"github.com/cubahno/connexions/internal"
"github.com/cubahno/connexions/openapi"
"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi/datamodel/high/base"
"log"
"os"
"strconv"
"strings"
)
// NewDocumentFromFile creates a new Document from a file path.
// It uses libopenapi to parse the file and then builds a model.
// Circular references are handled by logging the error and returning Document without errors.
func NewDocumentFromFile(filePath string) (openapi.Document, error) {
src, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
lib, err := libopenapi.NewDocument(src)
if err != nil {
return nil, err
}
if strings.HasPrefix(lib.GetVersion(), "2.") {
model, errs := lib.BuildV2Model()
if len(errs) > 0 {
if model == nil {
return nil, errs[0]
}
for _, err := range errs {
log.Printf("Ignored error in %s: %v\n", filePath, err)
}
// if models is there we can ignore the errors
return &V2Document{
DocumentModel: model,
}, nil
}
return &V2Document{
DocumentModel: model,
}, nil
}
model, errs := lib.BuildV3Model()
if len(errs) > 0 {
if model == nil {
return nil, errs[0]
}
for _, err := range errs {
log.Printf("Ignored error in %s: %v\n", filePath, err)
}
// if models is there we can ignore the errors
return &V3Document{
DocumentModel: model,
}, nil
}
return &V3Document{
DocumentModel: model,
}, nil
}
// NewSchema creates a new Schema from a libopenapi Schema.
func NewSchema(schema *base.Schema, parseConfig *config.ParseConfig) *openapi.Schema {
if parseConfig == nil {
parseConfig = config.NewParseConfig()
}
return newSchema(schema, parseConfig, nil, nil)
}
func newSchema(schema *base.Schema, parseConfig *config.ParseConfig, refPath []string, namePath []string) *openapi.Schema {
if schema == nil {
return nil
}
if len(refPath) == 0 {
refPath = make([]string, 0)
}
if len(namePath) == 0 {
namePath = make([]string, 0)
}
if parseConfig.MaxLevels > 0 && len(namePath) > parseConfig.MaxLevels {
return nil
}
if internal.GetSliceMaxRepetitionNumber(refPath) > parseConfig.MaxRecursionLevels {
return nil
}
merged, mergedReference := mergeSubSchemas(schema)
typ := ""
if len(merged.Type) > 0 {
typ = merged.Type[0]
}
typ = openapi.FixSchemaTypeTypos(typ)
var items *openapi.Schema
if merged.Items != nil && merged.Items.IsA() {
libItems := merged.Items.A
sub := libItems.Schema()
ref := libItems.GetReference()
// detect circular reference early
if parseConfig.MaxRecursionLevels == 0 && internal.SliceContains(refPath, ref) {
return nil
}
items = newSchema(sub,
parseConfig,
internal.AppendSliceFirstNonEmpty(refPath, ref, mergedReference),
namePath)
}
var properties map[string]*openapi.Schema
if len(merged.Properties) > 0 {
properties = make(map[string]*openapi.Schema)
for propName, sProxy := range merged.Properties {
if parseConfig.OnlyRequired && !internal.SliceContains(merged.Required, propName) {
continue
}
properties[propName] = newSchema(sProxy.Schema(),
parseConfig,
internal.AppendSliceFirstNonEmpty(refPath, sProxy.GetReference(), mergedReference),
append(namePath, propName))
}
}
// add additional properties
additionalProps := getAdditionalProperties(merged.AdditionalProperties)
if additionalProps != nil {
if properties == nil {
properties = make(map[string]*openapi.Schema)
}
// TODO(cubahno): find out if this the correct property, or one from AdditionalProperties should be used
minProperties := internal.RemovePointer(merged.MinProperties)
// TODO(cubahno): move to config
additionalNum := 3
if minProperties > 0 {
additionalNum = int(minProperties)
}
additionalPrefix := "extra-"
for i := 0; i < additionalNum; i++ {
propName := additionalPrefix + strconv.Itoa(i+1)
propSchema := newSchema(
additionalProps,
parseConfig,
append(refPath, "additionalProperties"), // this will solve circular reference
append(namePath, propName),
)
if propSchema != nil {
properties[propName] = propSchema
}
}
if len(properties) == 0 {
properties = nil
}
}
var not *openapi.Schema
if merged.Not != nil {
not = newSchema(merged.Not.Schema(), parseConfig, refPath, namePath)
}
if typ == openapi.TypeArray && items == nil {
items = &openapi.Schema{Type: openapi.TypeString}
}
return &openapi.Schema{
Type: typ,
Items: items,
MultipleOf: internal.RemovePointer(merged.MultipleOf),
Maximum: internal.RemovePointer(merged.Maximum),
Minimum: internal.RemovePointer(merged.Minimum),
MaxLength: internal.RemovePointer(merged.MaxLength),
MinLength: internal.RemovePointer(merged.MinLength),
Pattern: merged.Pattern,
Format: merged.Format,
MaxItems: internal.RemovePointer(merged.MaxItems),
MinItems: internal.RemovePointer(merged.MinItems),
MaxProperties: internal.RemovePointer(merged.MaxProperties),
MinProperties: internal.RemovePointer(merged.MinProperties),
Required: merged.Required,
Enum: merged.Enum,
Properties: properties,
Not: not,
Default: merged.Default,
Nullable: internal.RemovePointer(merged.Nullable),
ReadOnly: merged.ReadOnly,
WriteOnly: merged.WriteOnly,
Example: merged.Example,
Deprecated: internal.RemovePointer(merged.Deprecated),
}
}
// mergeSubSchemas merges allOf, anyOf, oneOf and not into a single schema.
func mergeSubSchemas(schema *base.Schema) (*base.Schema, string) {
allOf := schema.AllOf
anyOf := schema.AnyOf
oneOf := schema.OneOf
not := schema.Not
// base case: schema is flat
if len(allOf) == 0 && len(anyOf) == 0 && len(oneOf) == 0 && not == nil {
if schema != nil && len(schema.Type) == 0 {
typ := openapi.TypeObject
if len(schema.Enum) > 0 {
enumType := openapi.GetOpenAPITypeFromValue(schema.Enum[0])
if enumType != "" {
typ = enumType
}
}
schema.Type = []string{typ}
}
return schema, ""
}
// reset
schema.AllOf = nil
schema.AnyOf = nil
schema.OneOf = nil
schema.Not = nil
properties := schema.Properties
if len(properties) == 0 {
properties = make(map[string]*base.SchemaProxy)
}
required := schema.Required
if len(required) == 0 {
required = make([]string, 0)
}
impliedType := ""
if len(allOf) > 0 {
impliedType = openapi.TypeObject
}
// pick one from each
allOf = append(allOf,
pickSchemaProxy(anyOf),
pickSchemaProxy(oneOf),
)
subRef := ""
for _, schemaRef := range allOf {
if schemaRef == nil {
continue
}
subSchema := schemaRef.Schema()
if subRef == "" && schemaRef.IsReference() {
subRef = schemaRef.GetReference()
}
if impliedType == "" {
if len(subSchema.Type) > 0 {
impliedType = subSchema.Type[0]
}
if impliedType == "" && subSchema.Items != nil && subSchema.Items.IsA() && len(subSchema.Items.A.Schema().Properties) > 0 {
impliedType = openapi.TypeArray
}
if impliedType == "" {
impliedType = openapi.TypeObject
}
}
if impliedType == openapi.TypeObject {
for propertyName, property := range subSchema.Properties {
if subRef == "" {
subRef = property.GetReference()
}
properties[propertyName] = property
}
}
if impliedType == openapi.TypeArray && subSchema.Items != nil && subSchema.Items.IsA() {
if subRef == "" {
subRef = subSchema.Items.A.GetReference()
}
schema.Items = subSchema.Items
}
// gather fom the sub
schema.AllOf = append(schema.AllOf, subSchema.AllOf...)
schema.AnyOf = append(schema.AnyOf, subSchema.AnyOf...)
schema.OneOf = append(schema.OneOf, subSchema.OneOf...)
schema.Required = append(schema.Required, subSchema.Required...)
required = append(required, subSchema.Required...)
}
// make required unique
required = internal.SliceUnique(required)
if not != nil {
resultNot, _ := mergeSubSchemas(not.Schema())
if resultNot != nil {
// not is always an object
resultNot.Type = []string{openapi.TypeObject}
}
schema.Not = base.CreateSchemaProxy(resultNot)
}
schema.Type = []string{impliedType}
schema.Properties = properties
schema.Required = required
if len(schema.AllOf) > 0 {
return mergeSubSchemas(schema)
}
return schema, subRef
}
// pickSchemaProxy returns the first non-nil schema proxy with reference
// or the first non-nil schema proxy if none of them have reference.
func pickSchemaProxy(items []*base.SchemaProxy) *base.SchemaProxy {
if len(items) == 0 {
return nil
}
var fstNonEmpty *base.SchemaProxy
for _, item := range items {
if item == nil {
continue
}
if fstNonEmpty == nil {
fstNonEmpty = item
}
// prefer reference
if item.GetReference() != "" {
return item
}
}
return fstNonEmpty
}
// getAdditionalProperties returns the additionalProperties of a libopenapi Schema.
func getAdditionalProperties(source any) *base.Schema {
if source == nil {
return nil
}
switch v := source.(type) {
case bool:
if !v {
return nil
}
// default dictionary
return &base.Schema{Type: []string{openapi.TypeString}}
case *base.SchemaProxy:
return v.Schema()
}
return nil
}