8
8
isText ,
9
9
isXML ,
10
10
} from './contentTypeChecks' ;
11
+ import { json2xml } from './json2xml' ;
11
12
import { stringifyOpenAPI } from './stringifyOpenAPI' ;
12
13
13
14
export interface CodeSampleInput {
@@ -238,7 +239,10 @@ const BodyGenerators = {
238
239
: String ( body ) ;
239
240
} else if ( isText ( contentType ) ) {
240
241
body = `--data '${ String ( body ) . replace ( / " / g, '' ) } '` ;
241
- } else if ( isXML ( contentType ) || isCSV ( contentType ) ) {
242
+ } else if ( isXML ( contentType ) ) {
243
+ // Convert to XML and ensure proper formatting
244
+ body = `--data-binary $'${ convertBodyToXML ( body ) } '` ;
245
+ } else if ( isCSV ( contentType ) ) {
242
246
// We use --data-binary to avoid cURL converting newlines to \r\n
243
247
body = `--data-binary $'${ stringifyOpenAPI ( body ) . replace ( / " / g, '' ) . replace ( / \\ n / g, '\n' ) } '` ;
244
248
} else if ( isGraphQL ( contentType ) ) {
@@ -312,7 +316,9 @@ const BodyGenerators = {
312
316
body = 'formData' ;
313
317
} else if ( isXML ( contentType ) ) {
314
318
code += 'const xml = `\n' ;
315
- code += indent ( String ( body ) , 4 ) ;
319
+
320
+ // Convert JSON to XML if needed
321
+ code += indent ( convertBodyToXML ( body ) , 4 ) ;
316
322
code += '`;\n\n' ;
317
323
body = 'xml' ;
318
324
} else if ( isText ( contentType ) ) {
@@ -346,6 +352,11 @@ const BodyGenerators = {
346
352
body = 'files' ;
347
353
}
348
354
355
+ if ( isXML ( contentType ) ) {
356
+ // Convert JSON to XML if needed
357
+ body = convertBodyToXML ( body ) ;
358
+ }
359
+
349
360
return { body, code, headers } ;
350
361
} ,
351
362
getHTTPBody : ( body : any , headers ?: Record < string , string > ) => {
@@ -358,23 +369,48 @@ const BodyGenerators = {
358
369
formUrlEncoded : ( ) => {
359
370
const encoded = isPlainObject ( body )
360
371
? Object . entries ( body )
361
- . map ( ( [ key , value ] ) => `${ key } =${ String ( value ) } ` )
372
+ . map ( ( [ key , value ] ) => `${ key } =${ stringifyOpenAPI ( value ) } ` )
362
373
. join ( '&' )
363
- : String ( body ) ;
364
- return `"${ encoded } "` ;
374
+ : stringifyOpenAPI ( body ) ;
375
+ return `"${ encoded . replace ( / " / g , "'" ) } "` ;
365
376
} ,
366
377
text : ( ) => `"${ String ( body ) } "` ,
367
- xmlOrCsv : ( ) => `"${ stringifyOpenAPI ( body ) . replace ( / " / g, '' ) } "` ,
378
+ xml : ( ) => {
379
+ // Convert JSON to XML if needed
380
+ return `"${ convertBodyToXML ( body ) } "` ;
381
+ } ,
382
+ csv : ( ) => `"${ stringifyOpenAPI ( body ) . replace ( / " / g, '' ) } "` ,
368
383
default : ( ) => `${ stringifyOpenAPI ( body , null , 2 ) } ` ,
369
384
} ;
370
385
371
386
if ( isPDF ( contentType ) ) return typeHandlers . pdf ( ) ;
372
387
if ( isFormUrlEncoded ( contentType ) ) return typeHandlers . formUrlEncoded ( ) ;
373
388
if ( isText ( contentType ) ) return typeHandlers . text ( ) ;
374
- if ( isXML ( contentType ) || isCSV ( contentType ) ) {
375
- return typeHandlers . xmlOrCsv ( ) ;
376
- }
389
+ if ( isXML ( contentType ) ) return typeHandlers . xml ( ) ;
390
+ if ( isCSV ( contentType ) ) return typeHandlers . csv ( ) ;
377
391
378
392
return typeHandlers . default ( ) ;
379
393
} ,
380
394
} ;
395
+
396
+ /**
397
+ * Converts a body to XML format
398
+ */
399
+ function convertBodyToXML ( body : any ) : string {
400
+ // If body is already a string and looks like XML, return it as is
401
+ if ( typeof body === 'string' && body . trim ( ) . startsWith ( '<' ) ) {
402
+ return body ;
403
+ }
404
+
405
+ // If body is not an object, try to parse it as JSON
406
+ if ( typeof body !== 'object' || body === null ) {
407
+ try {
408
+ body = JSON . parse ( body ) ;
409
+ } catch {
410
+ // If parsing fails, return the original body
411
+ return body ;
412
+ }
413
+ }
414
+
415
+ return json2xml ( body ) . replace ( / " / g, '' ) . replace ( / \\ n / g, '\n' ) . replace ( / \\ t / g, '\t' ) ;
416
+ }
0 commit comments