-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathoastools_schema.go
More file actions
360 lines (315 loc) · 9.67 KB
/
oastools_schema.go
File metadata and controls
360 lines (315 loc) · 9.67 KB
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
package restfulspec
import (
"log"
"reflect"
"strconv"
"strings"
"github.com/erraggy/oastools/parser"
)
// legacyOptionalExtension is the extension key used to mark fields as optional
// during schema generation. This is cleaned up in post-processing.
const legacyOptionalExtension = "x-restful-optional"
// legacyTagFieldProcessor is a schema field processor that applies go-restful-openapi's
// legacy struct tags to schemas. This is used with oastools' WithSchemaFieldProcessor option.
// Legacy tags are only applied to fields that do NOT have an oas:"..." tag.
func legacyTagFieldProcessor(schema *parser.Schema, field reflect.StructField) *parser.Schema {
// Only apply legacy tags if the field doesn't have an oas:"..." tag
if hasOASTag(field) {
return schema
}
return applyLegacyTags(schema, field)
}
// hasOASTag returns true if the field has an oas:"..." struct tag.
func hasOASTag(field reflect.StructField) bool {
return field.Tag.Get("oas") != ""
}
// applyLegacyTags applies go-restful-openapi's legacy struct tags to a schema.
// This is only called when the field does NOT have an oas:"..." tag.
// Supported legacy tags: description, minimum, maximum, enum, format, type,
// unique, readOnly, optional, example, default, x-nullable, x-go-name
func applyLegacyTags(schema *parser.Schema, field reflect.StructField) *parser.Schema {
if schema == nil {
return nil
}
// Make a copy to avoid mutating the original
result := shallowCopySchema(schema)
// description tag
if tag := field.Tag.Get("description"); tag != "" {
result.Description = tag
}
// minimum tag
if tag := field.Tag.Get("minimum"); tag != "" {
if f, err := strconv.ParseFloat(tag, 64); err == nil {
result.Minimum = &f
} else {
log.Printf("restfulspec: field %q has invalid minimum tag %q: %v", field.Name, tag, err)
}
}
// maximum tag
if tag := field.Tag.Get("maximum"); tag != "" {
if f, err := strconv.ParseFloat(tag, 64); err == nil {
result.Maximum = &f
} else {
log.Printf("restfulspec: field %q has invalid maximum tag %q: %v", field.Name, tag, err)
}
}
// enum tag (pipe-separated values)
if tag := field.Tag.Get("enum"); tag != "" {
enumValues := strings.Split(tag, "|")
result.Enum = make([]any, len(enumValues))
for i, v := range enumValues {
result.Enum[i] = strings.TrimSpace(v)
}
}
// format tag
if tag := field.Tag.Get("format"); tag != "" {
result.Format = tag
}
// type tag - can override the Go type
if tag := field.Tag.Get("type"); tag != "" {
// Check if the type is intended to be an array (e.g., "[]string")
if len(tag) > 2 && tag[0:2] == "[]" {
result.Type = "array"
result.Items = &parser.Schema{Type: tag[2:]}
} else {
result.Type = tag
}
}
// unique tag
if tag := field.Tag.Get("unique"); tag == "true" {
result.UniqueItems = true
}
// readOnly tag
if tag := field.Tag.Get("readOnly"); tag == "true" {
result.ReadOnly = true
}
// example tag
if tag := field.Tag.Get("example"); tag != "" {
result.Example = tag
}
// default tag
if tag := field.Tag.Get("default"); tag != "" {
result.Default = parseLegacyDefaultValue(tag, result.Type)
}
// x-nullable tag - stored as extension
if tag := field.Tag.Get("x-nullable"); tag != "" {
if result.Extra == nil {
result.Extra = make(map[string]any)
}
result.Extra["x-nullable"] = tag == "true"
}
// x-go-name tag - stored as extension
if tag := field.Tag.Get("x-go-name"); tag != "" {
if result.Extra == nil {
result.Extra = make(map[string]any)
}
result.Extra["x-go-name"] = tag
}
// optional tag - marks field as not required
// This is processed via extension and cleaned up in post-processing
// because the required array is on the parent schema, not the field schema.
if tag := field.Tag.Get("optional"); tag == "true" {
if result.Extra == nil {
result.Extra = make(map[string]any)
}
// Get the JSON field name for removal from required array
jsonName := field.Name
if jsonTag := field.Tag.Get("json"); jsonTag != "" {
parts := strings.Split(jsonTag, ",")
if parts[0] != "" && parts[0] != "-" {
jsonName = parts[0]
}
}
result.Extra[legacyOptionalExtension] = jsonName
}
return result
}
// isLegacyFieldRequired determines if a field should be required based on legacy tags.
// Returns true if the field should be required, false otherwise.
// Note: This function is reserved for future integration when oastools adds support
// for customizing required field behavior. Currently, required fields are determined
// by oastools based on pointer types and omitempty tags.
//
//nolint:unused
func isLegacyFieldRequired(field reflect.StructField) bool {
// Check for explicit optional tag
if optionalTag := field.Tag.Get("optional"); optionalTag == "true" {
return false
}
// Check json tag for omitempty
if jsonTag := field.Tag.Get("json"); jsonTag != "" {
parts := strings.Split(jsonTag, ",")
for _, part := range parts[1:] {
if part == "omitempty" {
return false
}
}
}
// Pointer fields are optional by default
if field.Type.Kind() == reflect.Pointer {
return false
}
return true
}
// shallowCopySchema creates a shallow copy of a schema for modification.
func shallowCopySchema(s *parser.Schema) *parser.Schema {
if s == nil {
return nil
}
result := &parser.Schema{
Ref: s.Ref,
Type: s.Type,
Format: s.Format,
Title: s.Title,
Description: s.Description,
Default: s.Default,
Nullable: s.Nullable,
ReadOnly: s.ReadOnly,
WriteOnly: s.WriteOnly,
Deprecated: s.Deprecated,
Pattern: s.Pattern,
UniqueItems: s.UniqueItems,
Example: s.Example,
}
// Copy pointer fields
if s.Minimum != nil {
minCopy := *s.Minimum
result.Minimum = &minCopy
}
if s.Maximum != nil {
maxCopy := *s.Maximum
result.Maximum = &maxCopy
}
if s.MinLength != nil {
minLenCopy := *s.MinLength
result.MinLength = &minLenCopy
}
if s.MaxLength != nil {
maxLenCopy := *s.MaxLength
result.MaxLength = &maxLenCopy
}
if s.MinItems != nil {
minItemsCopy := *s.MinItems
result.MinItems = &minItemsCopy
}
if s.MaxItems != nil {
maxItemsCopy := *s.MaxItems
result.MaxItems = &maxItemsCopy
}
// Copy slices
if s.Enum != nil {
result.Enum = make([]any, len(s.Enum))
copy(result.Enum, s.Enum)
}
if s.Required != nil {
result.Required = make([]string, len(s.Required))
copy(result.Required, s.Required)
}
// Reference shared structures (immutable from our perspective)
result.Properties = s.Properties
result.Items = s.Items
result.AdditionalProperties = s.AdditionalProperties
result.AllOf = s.AllOf
result.AnyOf = s.AnyOf
result.OneOf = s.OneOf
// Deep-copy Extra map to avoid shared mutation
if s.Extra != nil {
result.Extra = make(map[string]any, len(s.Extra))
for k, v := range s.Extra {
result.Extra[k] = v
}
}
return result
}
// parseLegacyDefaultValue parses a default value string based on the schema type.
func parseLegacyDefaultValue(value string, schemaType any) any {
typeStr, ok := schemaType.(string)
if !ok {
return value
}
switch typeStr {
case "integer":
if n, err := strconv.ParseInt(value, 10, 64); err == nil {
return n
}
log.Printf("restfulspec: invalid default value %q for integer type: falling back to string", value)
case "number":
if f, err := strconv.ParseFloat(value, 64); err == nil {
return f
}
log.Printf("restfulspec: invalid default value %q for number type: falling back to string", value)
case "boolean":
return value == "true"
}
return value
}
// applyLegacyOptionalToSchemas processes all schemas in a map and removes fields
// marked with x-restful-optional from the required arrays, then cleans up the extension.
func applyLegacyOptionalToSchemas(schemas map[string]*parser.Schema) {
for _, schema := range schemas {
applyLegacyOptionalToSchema(schema)
}
}
// applyLegacyOptionalToSchema processes a single schema and its nested properties.
func applyLegacyOptionalToSchema(schema *parser.Schema) {
if schema == nil {
return
}
// Collect field names that should be removed from required
var optionalFields []string
// Process properties
for propName, propSchema := range schema.Properties {
if propSchema == nil {
continue
}
// Check for the optional extension
if propSchema.Extra != nil {
if jsonName, ok := propSchema.Extra[legacyOptionalExtension].(string); ok {
optionalFields = append(optionalFields, jsonName)
// Clean up the extension
delete(propSchema.Extra, legacyOptionalExtension)
if len(propSchema.Extra) == 0 {
propSchema.Extra = nil
}
}
}
// Recursively process nested schemas
applyLegacyOptionalToSchema(propSchema)
// Also check if the property name matches what we collected
_ = propName // propName is used implicitly via propSchema
}
// Remove optional fields from the required array
if len(optionalFields) > 0 && len(schema.Required) > 0 {
optionalSet := make(map[string]bool, len(optionalFields))
for _, f := range optionalFields {
optionalSet[f] = true
}
newRequired := make([]string, 0, len(schema.Required))
for _, req := range schema.Required {
if !optionalSet[req] {
newRequired = append(newRequired, req)
}
}
schema.Required = newRequired
}
// Process nested schemas in Items, AllOf, AnyOf, OneOf
if schema.Items != nil {
if itemSchema, ok := schema.Items.(*parser.Schema); ok {
applyLegacyOptionalToSchema(itemSchema)
}
}
for _, s := range schema.AllOf {
applyLegacyOptionalToSchema(s)
}
for _, s := range schema.AnyOf {
applyLegacyOptionalToSchema(s)
}
for _, s := range schema.OneOf {
applyLegacyOptionalToSchema(s)
}
if schema.AdditionalProperties != nil {
if addPropSchema, ok := schema.AdditionalProperties.(*parser.Schema); ok {
applyLegacyOptionalToSchema(addPropSchema)
}
}
}