@@ -268,24 +268,32 @@ func generateRequestsFromOp(opts *generateReqOptions) error {
268268 for content , value := range opts .op .RequestBody .Value .Content {
269269 cloned := req .Clone (req .Context ())
270270
271- example , err := generateExampleFromSchema (value .Schema .Value )
272- if err != nil {
273- continue
271+ var val interface {}
272+
273+ if value .Schema == nil || value .Schema .Value == nil {
274+ val = generateEmptySchemaValue (content )
275+ } else {
276+ var err error
277+
278+ val , err = generateExampleFromSchema (value .Schema .Value )
279+ if err != nil {
280+ continue
281+ }
274282 }
275283
276284 // var body string
277285 switch content {
278286 case "application/json" :
279- if marshalled , err := json .Marshal (example ); err == nil {
287+ if marshalled , err := json .Marshal (val ); err == nil {
280288 // body = string(marshalled)
281289 cloned .Body = io .NopCloser (bytes .NewReader (marshalled ))
282290 cloned .ContentLength = int64 (len (marshalled ))
283291 cloned .Header .Set ("Content-Type" , "application/json" )
284292 }
285293 case "application/xml" :
286- exampleVal := mxj .Map (example .(map [string ]interface {}))
294+ values := mxj .Map (val .(map [string ]interface {}))
287295
288- if marshalled , err := exampleVal .Xml (); err == nil {
296+ if marshalled , err := values .Xml (); err == nil {
289297 // body = string(marshalled)
290298 cloned .Body = io .NopCloser (bytes .NewReader (marshalled ))
291299 cloned .ContentLength = int64 (len (marshalled ))
@@ -294,7 +302,7 @@ func generateRequestsFromOp(opts *generateReqOptions) error {
294302 gologger .Warning ().Msgf ("openapi: could not encode xml" )
295303 }
296304 case "application/x-www-form-urlencoded" :
297- if values , ok := example .(map [string ]interface {}); ok {
305+ if values , ok := val .(map [string ]interface {}); ok {
298306 cloned .Form = url.Values {}
299307 for k , v := range values {
300308 cloned .Form .Set (k , types .ToString (v ))
@@ -306,7 +314,7 @@ func generateRequestsFromOp(opts *generateReqOptions) error {
306314 cloned .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
307315 }
308316 case "multipart/form-data" :
309- if values , ok := example .(map [string ]interface {}); ok {
317+ if values , ok := val .(map [string ]interface {}); ok {
310318 buffer := & bytes.Buffer {}
311319 multipartWriter := multipart .NewWriter (buffer )
312320 for k , v := range values {
@@ -326,13 +334,13 @@ func generateRequestsFromOp(opts *generateReqOptions) error {
326334 cloned .Header .Set ("Content-Type" , multipartWriter .FormDataContentType ())
327335 }
328336 case "text/plain" :
329- str := types .ToString (example )
337+ str := types .ToString (val )
330338 // body = str
331339 cloned .Body = io .NopCloser (strings .NewReader (str ))
332340 cloned .ContentLength = int64 (len (str ))
333341 cloned .Header .Set ("Content-Type" , "text/plain" )
334342 case "application/octet-stream" :
335- str := types .ToString (example )
343+ str := types .ToString (val )
336344 if str == "" {
337345 // use two strings
338346 str = "string1\n string2"
0 commit comments