@@ -100,12 +100,32 @@ func constructParamTypes(paths map[string]*openapi3.PathItem) []TypeTemplate {
100
100
sort .Strings (keys )
101
101
for _ , op := range keys {
102
102
o := ops [op ]
103
+ requiredFields := ""
104
+
105
+ // Some required fields are defined in vendor extensions
106
+ for k , v := range o .Extensions {
107
+ if k == "x-dropshot-pagination" {
108
+ for i , j := range v .(map [string ]interface {}) {
109
+ if i == "required" {
110
+ values , ok := j .([]interface {})
111
+ if ok {
112
+ for _ , field := range values {
113
+ str , ok := field .(string )
114
+ if ok {
115
+ requiredFields = requiredFields + fmt .Sprintf ("\n // - %v" , strcase .ToCamel (str ))
116
+ }
117
+ }
118
+ }
119
+ }
120
+ }
121
+ }
122
+ }
123
+
103
124
if len (o .Parameters ) > 0 || o .RequestBody != nil {
104
125
paramsTypeName := strcase .ToCamel (o .OperationID ) + "Params"
105
126
paramsTpl := TypeTemplate {
106
- Type : "struct" ,
107
- Name : paramsTypeName ,
108
- Description : "// " + paramsTypeName + " is the request parameters for " + strcase .ToCamel (o .OperationID ),
127
+ Type : "struct" ,
128
+ Name : paramsTypeName ,
109
129
}
110
130
111
131
fields := make ([]TypeFields , 0 )
@@ -121,6 +141,10 @@ func constructParamTypes(paths map[string]*openapi3.PathItem) []TypeTemplate {
121
141
Type : convertToValidGoType ("" , p .Value .Schema ),
122
142
}
123
143
144
+ if p .Value .Required {
145
+ requiredFields = requiredFields + fmt .Sprintf ("\n // - %s" , paramName )
146
+ }
147
+
124
148
serInfo := fmt .Sprintf ("`json:\" %s,omitempty\" yaml:\" %s,omitempty\" `" , p .Value .Name , p .Value .Name )
125
149
field .SerializationInfo = serInfo
126
150
@@ -147,9 +171,18 @@ func constructParamTypes(paths map[string]*openapi3.PathItem) []TypeTemplate {
147
171
SerializationInfo : "`json:\" body,omitempty\" yaml:\" body,omitempty\" `" ,
148
172
}
149
173
}
174
+ // Body is always a required field
175
+ requiredFields = requiredFields + "\n // - Body"
150
176
fields = append (fields , field )
151
177
}
152
178
paramsTpl .Fields = fields
179
+
180
+ description := "// " + paramsTypeName + " is the request parameters for " +
181
+ strcase .ToCamel (o .OperationID )
182
+ if requiredFields != "" {
183
+ description = description + "\n //\n // Required fields:" + requiredFields
184
+ }
185
+ paramsTpl .Description = description
153
186
paramTypes = append (paramTypes , paramsTpl )
154
187
}
155
188
}
@@ -400,7 +433,7 @@ func populateTypeTemplates(name string, s *openapi3.Schema, enumFieldName string
400
433
typeTpl .Type = fmt .Sprintf ("[]%s" , s .Items .Value .Type )
401
434
typeTpl .Name = typeName
402
435
case "object" :
403
- typeTpl = createTypeObject (s . Properties , name , typeName , formatTypeDescription (typeName , s ))
436
+ typeTpl = createTypeObject (s , name , typeName , formatTypeDescription (typeName , s ))
404
437
405
438
// Iterate over the properties and append the types, if we need to.
406
439
for k , v := range s .Properties {
@@ -439,7 +472,7 @@ func populateTypeTemplates(name string, s *openapi3.Schema, enumFieldName string
439
472
return types , enumTypes
440
473
}
441
474
442
- func createTypeObject (schemas map [ string ] * openapi3.SchemaRef , name , typeName , description string ) TypeTemplate {
475
+ func createTypeObject (schema * openapi3.Schema , name , typeName , description string ) TypeTemplate {
443
476
// TODO: Create types out of the schemas instead of plucking them out of the objects
444
477
// will leave this for another PR, because the yak shaving is getting ridiculous.
445
478
// Tracked -> https://github.com/oxidecomputer/oxide.go/issues/110
@@ -450,11 +483,11 @@ func createTypeObject(schemas map[string]*openapi3.SchemaRef, name, typeName, de
450
483
}
451
484
452
485
typeTpl := TypeTemplate {
453
- Description : description ,
454
- Name : typeName ,
455
- Type : "struct" ,
486
+ Name : typeName ,
487
+ Type : "struct" ,
456
488
}
457
489
490
+ schemas := schema .Properties
458
491
// We want to ensure we keep the order
459
492
keys := make ([]string , 0 )
460
493
for k := range schemas {
@@ -496,6 +529,14 @@ func createTypeObject(schemas map[string]*openapi3.SchemaRef, name, typeName, de
496
529
}
497
530
typeTpl .Fields = fields
498
531
532
+ if len (schema .Required ) > 0 {
533
+ description = description + "\n //\n // Required fields:"
534
+ for _ , r := range schema .Required {
535
+ description = description + fmt .Sprintf ("\n // - %s" , strcase .ToCamel (r ))
536
+ }
537
+ }
538
+ typeTpl .Description = description
539
+
499
540
return typeTpl
500
541
}
501
542
0 commit comments