@@ -2,6 +2,7 @@ package openapi
2
2
3
3
import (
4
4
"bytes"
5
+ "fmt"
5
6
"strconv"
6
7
7
8
"github.com/brianvoe/gofakeit/v7"
@@ -10,21 +11,37 @@ import (
10
11
11
12
const maximumDepth = 4
12
13
14
+ const (
15
+ FloatParamType = "float"
16
+ DoubleParamType = "double"
17
+ Int32ParamFormat = "int32"
18
+ Int64ParamFormat = "int64"
19
+ )
20
+
21
+ func NewErrNoSupportedBodyMediaType () error {
22
+ return fmt .Errorf ("no supported body media type" )
23
+ }
24
+
13
25
func getParameterValue (param * openapi3.Parameter ) string {
14
26
if param .Schema != nil {
15
27
value := getSchemaValue (param .Schema .Value , 0 )
16
28
switch {
17
29
case param .Schema .Value .Type .Is ("string" ):
18
30
return value .(string )
19
31
case param .Schema .Value .Type .Is ("number" ):
20
- return strconv .FormatFloat (value .(float64 ), 'f' , - 1 , 64 )
32
+ switch param .Schema .Value .Format {
33
+ case FloatParamType :
34
+ return strconv .FormatFloat (value .(float64 ), 'f' , - 1 , 32 )
35
+ case DoubleParamType :
36
+ default :
37
+ return strconv .FormatFloat (value .(float64 ), 'f' , - 1 , 64 )
38
+ }
21
39
case param .Schema .Value .Type .Is ("integer" ):
22
- return strconv .Itoa (value .(int ) )
40
+ return strconv .FormatInt (value .(int64 ), 10 )
23
41
case param .Schema .Value .Type .Is ("boolean" ):
24
42
return strconv .FormatBool (value .(bool ))
25
43
}
26
44
}
27
-
28
45
return ""
29
46
}
30
47
@@ -36,7 +53,7 @@ func mapRequestBodyFakeValueToJSON(schema *openapi3.Schema, fakeValue interface{
36
53
case schema .Type .Is ("number" ):
37
54
jsonResponse = []byte (strconv .FormatFloat (fakeValue .(float64 ), 'f' , - 1 , 64 ))
38
55
case schema .Type .Is ("integer" ):
39
- jsonResponse = []byte (strconv .Itoa (fakeValue .(int ) ))
56
+ jsonResponse = []byte (strconv .FormatInt (fakeValue .(int64 ), 10 ))
40
57
case schema .Type .Is ("boolean" ):
41
58
jsonResponse = []byte (strconv .FormatBool (fakeValue .(bool )))
42
59
case schema .Type .Is ("array" ):
@@ -64,35 +81,83 @@ func mapRequestBodyFakeValueToJSON(schema *openapi3.Schema, fakeValue interface{
64
81
return bytes .NewBuffer (jsonResponse )
65
82
}
66
83
67
- func getRequestBodyValue (requestBody * openapi3.RequestBody ) (* bytes.Buffer , string ) {
68
- if requestBody .Content != nil {
69
- for mediaType , mediaTypeValue := range requestBody .Content {
70
- if mediaTypeValue .Schema != nil {
71
- body := getSchemaValue (mediaTypeValue .Schema .Value , 0 )
72
- switch mediaType {
73
- case "application/json" :
74
- return mapRequestBodyFakeValueToJSON (mediaTypeValue .Schema .Value , body ), "application/json"
75
- default :
76
- return bytes .NewBuffer ([]byte (body .(string ))), mediaType
77
- }
84
+ func getRequestBodyValue (requestBody * openapi3.RequestBody ) (* bytes.Buffer , string , error ) {
85
+ if requestBody == nil || requestBody .Content == nil {
86
+ return nil , "" , nil
87
+ }
88
+ for mediaType , mediaTypeValue := range requestBody .Content {
89
+ if mediaTypeValue .Schema != nil {
90
+ body := getSchemaValue (mediaTypeValue .Schema .Value , 0 )
91
+ if mediaType == "application/json" {
92
+ return mapRequestBodyFakeValueToJSON (mediaTypeValue .Schema .Value , body ), mediaType , nil
78
93
}
79
94
}
80
95
}
81
-
82
- return bytes .NewBuffer (nil ), ""
96
+ return nil , "" , NewErrNoSupportedBodyMediaType ()
83
97
}
84
98
85
- func getSchemaValue (schema * openapi3.Schema , depth int ) interface {} {
99
+ func parseSchemaExample (schema * openapi3.Schema ) (interface {}, error ) {
100
+ var example interface {}
86
101
if schema .Example != nil {
87
- return schema .Example
102
+ example = schema .Example
88
103
} else if len (schema .Enum ) > 0 {
89
- return schema .Enum [gofakeit .Number (0 , len (schema .Enum )- 1 )]
104
+ example = schema .Enum [gofakeit .Number (0 , len (schema .Enum )- 1 )]
105
+ }
106
+ if example == nil {
107
+ return nil , nil
108
+ }
109
+
110
+ var ok bool
111
+ _ , ok = example .(string )
112
+ if ok && ! schema .Type .Is ("string" ) {
113
+ switch {
114
+ case schema .Type .Is ("number" ):
115
+ return strconv .ParseFloat (example .(string ), 64 )
116
+ case schema .Type .Is ("integer" ):
117
+ return strconv .ParseInt (example .(string ), 10 , 64 )
118
+ case schema .Type .Is ("boolean" ):
119
+ return strconv .ParseBool (example .(string ))
120
+ }
121
+ }
122
+
123
+ switch {
124
+ case schema .Type .Is ("string" ):
125
+ example , ok = example .(string )
126
+ case schema .Type .Is ("number" ):
127
+ example , ok = example .(float64 )
128
+ case schema .Type .Is ("integer" ):
129
+ switch schema .Format {
130
+ case Int32ParamFormat :
131
+ example , ok = example .(int32 )
132
+ case Int64ParamFormat :
133
+ default :
134
+ example , ok = example .(int64 )
135
+ }
136
+ case schema .Type .Is ("boolean" ):
137
+ example , ok = example .(bool )
138
+ case schema .Type .Is ("array" ):
139
+ example , ok = example .([]interface {})
140
+ case schema .Type .Is ("object" ):
141
+ example , ok = example .(map [string ]interface {})
142
+ }
143
+ if ! ok {
144
+ return nil , fmt .Errorf ("invalid example type" )
145
+ }
146
+ return example , nil
147
+ }
148
+
149
+ func getSchemaValue (schema * openapi3.Schema , depth int ) interface {} {
150
+ example , err := parseSchemaExample (schema )
151
+ if err == nil && example != nil {
152
+ return example
90
153
}
91
154
92
155
// if there is no example generate random param
93
156
switch {
94
- case schema .Type .Is ("number" ) || schema .Type .Is ("integer" ):
95
- return gofakeit .Number (0 , 10 )
157
+ case schema .Type .Is ("number" ):
158
+ return gofakeit .Float64 ()
159
+ case schema .Type .Is ("integer" ):
160
+ return gofakeit .Int64 ()
96
161
case schema .Type .Is ("boolean" ):
97
162
return gofakeit .Bool ()
98
163
case schema .Type .Is ("array" ):
0 commit comments