-
Notifications
You must be signed in to change notification settings - Fork 802
/
schema.go
726 lines (645 loc) · 23.4 KB
/
schema.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
package codegen
import (
"errors"
"fmt"
"strings"
"github.com/getkin/kin-openapi/openapi3"
)
// Schema describes an OpenAPI schema, with lots of helper fields to use in the
// templating engine.
type Schema struct {
GoType string // The Go type needed to represent the schema
RefType string // If the type has a type name, this is set
ArrayType *Schema // The schema of array element
EnumValues map[string]string // Enum values
Properties []Property // For an object, the fields with names
HasAdditionalProperties bool // Whether we support additional properties
AdditionalPropertiesType *Schema // And if we do, their type
AdditionalTypes []TypeDefinition // We may need to generate auxiliary helper types, stored here
SkipOptionalPointer bool // Some types don't need a * in front when they're optional
Description string // The description of the element
UnionElements []string // Possible elements of oneOf/anyOf union
Discriminator *Discriminator // Describes which value is stored in a union
// If this is set, the schema will declare a type via alias, eg,
// `type Foo = bool`. If this is not set, we will define this type via
// type definition `type Foo bool`
DefineViaAlias bool
// The original OpenAPIv3 Schema.
OAPISchema *openapi3.Schema
}
func (s Schema) IsRef() bool {
return s.RefType != ""
}
func (s Schema) TypeDecl() string {
if s.IsRef() {
return s.RefType
}
return s.GoType
}
// AddProperty adds a new property to the current Schema, and returns an error
// if it collides. Two identical fields will not collide, but two properties by
// the same name, but different definition, will collide. It's safe to merge the
// fields of two schemas with overalapping properties if those properties are
// identical.
func (s *Schema) AddProperty(p Property) error {
// Scan all existing properties for a conflict
for _, e := range s.Properties {
if e.JsonFieldName == p.JsonFieldName && !PropertiesEqual(e, p) {
return errors.New(fmt.Sprintf("property '%s' already exists with a different type", e.JsonFieldName))
}
}
s.Properties = append(s.Properties, p)
return nil
}
func (s Schema) GetAdditionalTypeDefs() []TypeDefinition {
var result []TypeDefinition
for _, p := range s.Properties {
result = append(result, p.Schema.GetAdditionalTypeDefs()...)
}
result = append(result, s.AdditionalTypes...)
return result
}
type Property struct {
Description string
JsonFieldName string
Schema Schema
Required bool
Nullable bool
ReadOnly bool
WriteOnly bool
NeedsFormTag bool
ExtensionProps *openapi3.ExtensionProps
}
func (p Property) GoFieldName() string {
return SchemaNameToTypeName(p.JsonFieldName)
}
func (p Property) GoTypeDef() string {
typeDef := p.Schema.TypeDecl()
if !p.Schema.SkipOptionalPointer &&
(!p.Required || p.Nullable || p.ReadOnly || p.WriteOnly) {
typeDef = "*" + typeDef
}
return typeDef
}
// EnumDefinition holds type information for enum
type EnumDefinition struct {
// Schema is the scheme of a type which has a list of enum values, eg, the
// "container" of the enum.
Schema Schema
// TypeName is the name of the enum's type, usually aliased from something.
TypeName string
// ValueWrapper wraps the value. It's used to conditionally apply quotes
// around strings.
ValueWrapper string
// PrefixTypeName determines if the enum value is prefixed with its TypeName.
// This is set to true when this enum conflicts with another in terms of
// TypeNames or when explicitly requested via the
// `compatibility.always-prefix-enum-values` option.
PrefixTypeName bool
}
// GetValues generates enum names in a way to minimize global conflicts
func (e *EnumDefinition) GetValues() map[string]string {
// in case there are no conflicts, it's safe to use the values as-is
if !e.PrefixTypeName {
return e.Schema.EnumValues
}
// If we do have conflicts, we will prefix the enum's typename to the values.
newValues := make(map[string]string, len(e.Schema.EnumValues))
for k, v := range e.Schema.EnumValues {
newName := e.TypeName + UppercaseFirstCharacter(k)
newValues[newName] = v
}
return newValues
}
type Constants struct {
// SecuritySchemeProviderNames holds all provider names for security schemes.
SecuritySchemeProviderNames []string
// EnumDefinitions holds type and value information for all enums
EnumDefinitions []EnumDefinition
}
// TypeDefinition describes a Go type definition in generated code.
//
// Let's use this example schema:
// components:
// schemas:
// Person:
// type: object
// properties:
// name:
// type: string
type TypeDefinition struct {
// The name of the type, eg, type <...> Person
TypeName string
// The name of the corresponding JSON description, as it will sometimes
// differ due to invalid characters.
JsonName string
// This is the Schema wrapper is used to populate the type description
Schema Schema
}
// ResponseTypeDefinition is an extension of TypeDefinition, specifically for
// response unmarshaling in ClientWithResponses.
type ResponseTypeDefinition struct {
TypeDefinition
// The content type name where this is used, eg, application/json
ContentTypeName string
// The type name of a response model.
ResponseName string
}
func (t *TypeDefinition) IsAlias() bool {
return !globalState.options.Compatibility.OldAliasing && t.Schema.DefineViaAlias
}
type Discriminator struct {
// maps discriminator value to go type
Mapping map[string]string
// JSON property name that holds the discriminator
Property string
}
func (d *Discriminator) JSONTag() string {
return fmt.Sprintf("`json:\"%s\"`", d.Property)
}
func (d *Discriminator) PropertyName() string {
return SchemaNameToTypeName(d.Property)
}
func PropertiesEqual(a, b Property) bool {
return a.JsonFieldName == b.JsonFieldName && a.Schema.TypeDecl() == b.Schema.TypeDecl() && a.Required == b.Required
}
func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
// Add a fallback value in case the sref is nil.
// i.e. the parent schema defines a type:array, but the array has
// no items defined. Therefore we have at least valid Go-Code.
if sref == nil {
return Schema{GoType: "interface{}"}, nil
}
schema := sref.Value
// If Ref is set on the SchemaRef, it means that this type is actually a reference to
// another type. We're not de-referencing, so simply use the referenced type.
if IsGoTypeReference(sref.Ref) {
// Convert the reference path to Go type
refType, err := RefPathToGoType(sref.Ref)
if err != nil {
return Schema{}, fmt.Errorf("error turning reference (%s) into a Go type: %s",
sref.Ref, err)
}
return Schema{
GoType: refType,
Description: StringToGoComment(schema.Description),
DefineViaAlias: true,
}, nil
}
outSchema := Schema{
Description: StringToGoComment(schema.Description),
OAPISchema: schema,
}
// AllOf is interesting, and useful. It's the union of a number of other
// schemas. A common usage is to create a union of an object with an ID,
// so that in a RESTful paradigm, the Create operation can return
// (object, id), so that other operations can refer to (id)
if schema.AllOf != nil {
mergedSchema, err := MergeSchemas(schema.AllOf, path)
if err != nil {
return Schema{}, fmt.Errorf("error merging schemas: %w", err)
}
mergedSchema.OAPISchema = schema
return mergedSchema, nil
}
// Check for custom Go type extension
if extension, ok := schema.Extensions[extPropGoType]; ok {
typeName, err := extTypeName(extension)
if err != nil {
return outSchema, fmt.Errorf("invalid value for %q: %w", extPropGoType, err)
}
outSchema.GoType = typeName
return outSchema, nil
}
// Schema type and format, eg. string / binary
t := schema.Type
// Handle objects and empty schemas first as a special case
if t == "" || t == "object" {
var outType string
if len(schema.Properties) == 0 && !SchemaHasAdditionalProperties(schema) && schema.AnyOf == nil && schema.OneOf == nil {
// If the object has no properties or additional properties, we
// have some special cases for its type.
if t == "object" {
// We have an object with no properties. This is a generic object
// expressed as a map.
outType = "map[string]interface{}"
} else { // t == ""
// If we don't even have the object designator, we're a completely
// generic type.
outType = "interface{}"
}
outSchema.GoType = outType
outSchema.DefineViaAlias = true
} else {
// When we define an object, we want it to be a type definition,
// not a type alias, eg, "type Foo struct {...}"
outSchema.DefineViaAlias = false
// If the schema has additional properties, we need to special case
// a lot of behaviors.
outSchema.HasAdditionalProperties = SchemaHasAdditionalProperties(schema)
// Until we have a concrete additional properties type, we default to
// any schema.
outSchema.AdditionalPropertiesType = &Schema{
GoType: "interface{}",
}
// If additional properties are defined, we will override the default
// above with the specific definition.
if schema.AdditionalProperties != nil {
additionalSchema, err := GenerateGoSchema(schema.AdditionalProperties, path)
if err != nil {
return Schema{}, fmt.Errorf("error generating type for additional properties: %w", err)
}
if additionalSchema.HasAdditionalProperties || len(additionalSchema.UnionElements) != 0 {
// If we have fields present which have additional properties or union values,
// but are not a pre-defined type, we need to define a type
// for them, which will be based on the field names we followed
// to get to the type.
typeName := PathToTypeName(append(path, "AdditionalProperties"))
typeDef := TypeDefinition{
TypeName: typeName,
JsonName: strings.Join(append(path, "AdditionalProperties"), "."),
Schema: additionalSchema,
}
additionalSchema.RefType = typeName
additionalSchema.AdditionalTypes = append(additionalSchema.AdditionalTypes, typeDef)
}
outSchema.AdditionalPropertiesType = &additionalSchema
outSchema.AdditionalTypes = append(outSchema.AdditionalTypes, additionalSchema.AdditionalTypes...)
}
// If the schema has no properties, and only additional properties, we will
// early-out here and generate a map[string]<schema> instead of an object
// that contains this map. We skip over anyOf/oneOf here because they can
// introduce properties. allOf was handled above.
if !globalState.options.Compatibility.DisableFlattenAdditionalProperties &&
len(schema.Properties) == 0 && schema.AnyOf == nil && schema.OneOf == nil {
// We have a dictionary here. Returns the goType to be just a map from
// string to the property type. HasAdditionalProperties=false means
// that we won't generate custom json.Marshaler and json.Unmarshaler functions,
// since we don't need them for a simple map.
outSchema.HasAdditionalProperties = false
outSchema.GoType = fmt.Sprintf("map[string]%s", additionalPropertiesType(outSchema))
return outSchema, nil
}
// We've got an object with some properties.
for _, pName := range SortedSchemaKeys(schema.Properties) {
p := schema.Properties[pName]
propertyPath := append(path, pName)
pSchema, err := GenerateGoSchema(p, propertyPath)
if err != nil {
return Schema{}, fmt.Errorf("error generating Go schema for property '%s': %w", pName, err)
}
required := StringInArray(pName, schema.Required)
if (pSchema.HasAdditionalProperties || len(pSchema.UnionElements) != 0) && pSchema.RefType == "" {
// If we have fields present which have additional properties or union values,
// but are not a pre-defined type, we need to define a type
// for them, which will be based on the field names we followed
// to get to the type.
typeName := PathToTypeName(propertyPath)
typeDef := TypeDefinition{
TypeName: typeName,
JsonName: strings.Join(propertyPath, "."),
Schema: pSchema,
}
pSchema.AdditionalTypes = append(pSchema.AdditionalTypes, typeDef)
pSchema.RefType = typeName
}
description := ""
if p.Value != nil {
description = p.Value.Description
}
prop := Property{
JsonFieldName: pName,
Schema: pSchema,
Required: required,
Description: description,
Nullable: p.Value.Nullable,
ReadOnly: p.Value.ReadOnly,
WriteOnly: p.Value.WriteOnly,
ExtensionProps: &p.Value.ExtensionProps,
}
outSchema.Properties = append(outSchema.Properties, prop)
}
if schema.AnyOf != nil {
if err := generateUnion(&outSchema, schema.AnyOf, schema.Discriminator, path); err != nil {
return Schema{}, fmt.Errorf("error generating type for anyOf: %w", err)
}
}
if schema.OneOf != nil {
if err := generateUnion(&outSchema, schema.OneOf, schema.Discriminator, path); err != nil {
return Schema{}, fmt.Errorf("error generating type for oneOf: %w", err)
}
}
outSchema.GoType = GenStructFromSchema(outSchema)
}
return outSchema, nil
} else if len(schema.Enum) > 0 {
err := oapiSchemaToGoType(schema, path, &outSchema)
// Enums need to be typed, so that the values aren't interchangeable,
// so no matter what schema conversion thinks, we need to define a
// new type.
outSchema.DefineViaAlias = false
if err != nil {
return Schema{}, fmt.Errorf("error resolving primitive type: %w", err)
}
enumValues := make([]string, len(schema.Enum))
for i, enumValue := range schema.Enum {
enumValues[i] = fmt.Sprintf("%v", enumValue)
}
sanitizedValues := SanitizeEnumNames(enumValues)
outSchema.EnumValues = make(map[string]string, len(sanitizedValues))
for k, v := range sanitizedValues {
var enumName string
if v == "" {
enumName = "Empty"
} else {
enumName = k
}
if globalState.options.Compatibility.OldEnumConflicts {
outSchema.EnumValues[SchemaNameToTypeName(PathToTypeName(append(path, enumName)))] = v
} else {
outSchema.EnumValues[SchemaNameToTypeName(k)] = v
}
}
if len(path) > 1 { // handle additional type only on non-toplevel types
typeName := SchemaNameToTypeName(PathToTypeName(path))
typeDef := TypeDefinition{
TypeName: typeName,
JsonName: strings.Join(path, "."),
Schema: outSchema,
}
outSchema.AdditionalTypes = append(outSchema.AdditionalTypes, typeDef)
outSchema.RefType = typeName
}
// outSchema.RefType = typeName
} else {
err := oapiSchemaToGoType(schema, path, &outSchema)
if err != nil {
return Schema{}, fmt.Errorf("error resolving primitive type: %w", err)
}
}
return outSchema, nil
}
// oapiSchemaToGoType converts an OpenApi schema into a Go type definition for
// all non-object types.
func oapiSchemaToGoType(schema *openapi3.Schema, path []string, outSchema *Schema) error {
f := schema.Format
t := schema.Type
switch t {
case "array":
// For arrays, we'll get the type of the Items and throw a
// [] in front of it.
arrayType, err := GenerateGoSchema(schema.Items, path)
if err != nil {
return fmt.Errorf("error generating type for array: %w", err)
}
if (arrayType.HasAdditionalProperties || len(arrayType.UnionElements) != 0) && arrayType.RefType == "" {
// If we have items which have additional properties or union values,
// but are not a pre-defined type, we need to define a type
// for them, which will be based on the field names we followed
// to get to the type.
typeName := PathToTypeName(append(path, "Item"))
typeDef := TypeDefinition{
TypeName: typeName,
JsonName: strings.Join(append(path, "Item"), "."),
Schema: arrayType,
}
arrayType.AdditionalTypes = append(arrayType.AdditionalTypes, typeDef)
arrayType.RefType = typeName
}
outSchema.ArrayType = &arrayType
outSchema.GoType = "[]" + arrayType.TypeDecl()
outSchema.AdditionalTypes = arrayType.AdditionalTypes
outSchema.Properties = arrayType.Properties
outSchema.DefineViaAlias = true
case "integer":
// We default to int if format doesn't ask for something else.
if f == "int64" {
outSchema.GoType = "int64"
} else if f == "int32" {
outSchema.GoType = "int32"
} else if f == "int16" {
outSchema.GoType = "int16"
} else if f == "int8" {
outSchema.GoType = "int8"
} else if f == "int" {
outSchema.GoType = "int"
} else if f == "uint64" {
outSchema.GoType = "uint64"
} else if f == "uint32" {
outSchema.GoType = "uint32"
} else if f == "uint16" {
outSchema.GoType = "uint16"
} else if f == "uint8" {
outSchema.GoType = "uint8"
} else if f == "uint" {
outSchema.GoType = "uint"
} else if f == "" {
outSchema.GoType = "int"
} else {
return fmt.Errorf("invalid integer format: %s", f)
}
outSchema.DefineViaAlias = true
case "number":
// We default to float for "number"
if f == "double" {
outSchema.GoType = "float64"
} else if f == "float" || f == "" {
outSchema.GoType = "float32"
} else {
return fmt.Errorf("invalid number format: %s", f)
}
outSchema.DefineViaAlias = true
case "boolean":
if f != "" {
return fmt.Errorf("invalid format (%s) for boolean", f)
}
outSchema.GoType = "bool"
outSchema.DefineViaAlias = true
case "string":
// Special case string formats here.
switch f {
case "byte":
outSchema.GoType = "[]byte"
case "email":
outSchema.GoType = "openapi_types.Email"
case "date":
outSchema.GoType = "openapi_types.Date"
case "date-time":
outSchema.GoType = "time.Time"
case "json":
outSchema.GoType = "json.RawMessage"
outSchema.SkipOptionalPointer = true
case "uuid":
outSchema.GoType = "openapi_types.UUID"
case "binary":
outSchema.GoType = "openapi_types.File"
default:
// All unrecognized formats are simply a regular string.
outSchema.GoType = "string"
}
outSchema.DefineViaAlias = true
default:
return fmt.Errorf("unhandled Schema type: %s", t)
}
return nil
}
// SchemaDescriptor describes a Schema, a type definition.
type SchemaDescriptor struct {
Fields []FieldDescriptor
HasAdditionalProperties bool
AdditionalPropertiesType string
}
type FieldDescriptor struct {
Required bool // Is the schema required? If not, we'll pass by pointer
GoType string // The Go type needed to represent the json type.
GoName string // The Go compatible type name for the type
JsonName string // The json type name for the type
IsRef bool // Is this schema a reference to predefined object?
}
// Given a list of schema descriptors, produce corresponding field names with
// JSON annotations
func GenFieldsFromProperties(props []Property) []string {
var fields []string
for i, p := range props {
field := ""
// Add a comment to a field in case we have one, otherwise skip.
if p.Description != "" {
// Separate the comment from a previous-defined, unrelated field.
// Make sure the actual field is separated by a newline.
if i != 0 {
field += "\n"
}
field += fmt.Sprintf("%s\n", StringToGoComment(p.Description))
}
goFieldName := p.GoFieldName()
if _, ok := p.ExtensionProps.Extensions[extGoName]; ok {
if extGoFieldName, err := extParseGoFieldName(p.ExtensionProps.Extensions[extGoName]); err == nil {
goFieldName = extGoFieldName
}
}
field += fmt.Sprintf(" %s %s", goFieldName, p.GoTypeDef())
// Support x-omitempty
overrideOmitEmpty := true
if _, ok := p.ExtensionProps.Extensions[extPropOmitEmpty]; ok {
if extOmitEmpty, err := extParseOmitEmpty(p.ExtensionProps.Extensions[extPropOmitEmpty]); err == nil {
overrideOmitEmpty = extOmitEmpty
}
}
fieldTags := make(map[string]string)
if (p.Required && !p.ReadOnly && !p.WriteOnly) || p.Nullable || !overrideOmitEmpty {
fieldTags["json"] = p.JsonFieldName
if p.NeedsFormTag {
fieldTags["form"] = p.JsonFieldName
}
} else {
fieldTags["json"] = p.JsonFieldName + ",omitempty"
if p.NeedsFormTag {
fieldTags["form"] = p.JsonFieldName + ",omitempty"
}
}
if extension, ok := p.ExtensionProps.Extensions[extPropExtraTags]; ok {
if tags, err := extExtraTags(extension); err == nil {
keys := SortedStringKeys(tags)
for _, k := range keys {
fieldTags[k] = tags[k]
}
}
}
// Convert the fieldTags map into Go field annotations.
keys := SortedStringKeys(fieldTags)
tags := make([]string, len(keys))
for i, k := range keys {
tags[i] = fmt.Sprintf(`%s:"%s"`, k, fieldTags[k])
}
field += "`" + strings.Join(tags, " ") + "`"
fields = append(fields, field)
}
return fields
}
func additionalPropertiesType(schema Schema) string {
addPropsType := schema.AdditionalPropertiesType.GoType
if schema.AdditionalPropertiesType.RefType != "" {
addPropsType = schema.AdditionalPropertiesType.RefType
}
return addPropsType
}
func GenStructFromSchema(schema Schema) string {
// Start out with struct {
objectParts := []string{"struct {"}
// Append all the field definitions
objectParts = append(objectParts, GenFieldsFromProperties(schema.Properties)...)
// Close the struct
if schema.HasAdditionalProperties {
objectParts = append(objectParts,
fmt.Sprintf("AdditionalProperties map[string]%s `json:\"-\"`",
additionalPropertiesType(schema)))
}
if len(schema.UnionElements) != 0 {
objectParts = append(objectParts, "union json.RawMessage")
}
objectParts = append(objectParts, "}")
return strings.Join(objectParts, "\n")
}
// This constructs a Go type for a parameter, looking at either the schema or
// the content, whichever is available
func paramToGoType(param *openapi3.Parameter, path []string) (Schema, error) {
if param.Content == nil && param.Schema == nil {
return Schema{}, fmt.Errorf("parameter '%s' has no schema or content", param.Name)
}
// We can process the schema through the generic schema processor
if param.Schema != nil {
return GenerateGoSchema(param.Schema, path)
}
// At this point, we have a content type. We know how to deal with
// application/json, but if multiple formats are present, we can't do anything,
// so we'll return the parameter as a string, not bothering to decode it.
if len(param.Content) > 1 {
return Schema{
GoType: "string",
Description: StringToGoComment(param.Description),
}, nil
}
// Otherwise, look for application/json in there
mt, found := param.Content["application/json"]
if !found {
// If we don't have json, it's a string
return Schema{
GoType: "string",
Description: StringToGoComment(param.Description),
}, nil
}
// For json, we go through the standard schema mechanism
return GenerateGoSchema(mt.Schema, path)
}
func generateUnion(outSchema *Schema, elements openapi3.SchemaRefs, discriminator *openapi3.Discriminator, path []string) error {
if discriminator != nil {
outSchema.Discriminator = &Discriminator{
Property: discriminator.PropertyName,
Mapping: make(map[string]string),
}
}
refToGoTypeMap := make(map[string]string)
for i, element := range elements {
elementSchema, err := GenerateGoSchema(element, path)
if err != nil {
return err
}
if element.Ref == "" {
td := TypeDefinition{Schema: elementSchema, TypeName: SchemaNameToTypeName(PathToTypeName(append(path, fmt.Sprint(i))))}
outSchema.AdditionalTypes = append(outSchema.AdditionalTypes, td)
elementSchema.GoType = td.TypeName
} else {
refToGoTypeMap[element.Ref] = elementSchema.GoType
}
if discriminator != nil {
for k, v := range discriminator.Mapping {
if v == element.Ref {
outSchema.Discriminator.Mapping[k] = elementSchema.GoType
break
}
}
}
outSchema.UnionElements = append(outSchema.UnionElements, elementSchema.GoType)
}
return nil
}