88 isText ,
99 isXML ,
1010} from './contentTypeChecks' ;
11+ import { json2xml } from './json2xml' ;
1112import { stringifyOpenAPI } from './stringifyOpenAPI' ;
1213
1314export interface CodeSampleInput {
@@ -238,7 +239,10 @@ const BodyGenerators = {
238239 : String ( body ) ;
239240 } else if ( isText ( contentType ) ) {
240241 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 ) ) {
242246 // We use --data-binary to avoid cURL converting newlines to \r\n
243247 body = `--data-binary $'${ stringifyOpenAPI ( body ) . replace ( / " / g, '' ) . replace ( / \\ n / g, '\n' ) } '` ;
244248 } else if ( isGraphQL ( contentType ) ) {
@@ -312,7 +316,9 @@ const BodyGenerators = {
312316 body = 'formData' ;
313317 } else if ( isXML ( contentType ) ) {
314318 code += 'const xml = `\n' ;
315- code += indent ( String ( body ) , 4 ) ;
319+
320+ // Convert JSON to XML if needed
321+ code += indent ( convertBodyToXML ( body ) , 4 ) ;
316322 code += '`;\n\n' ;
317323 body = 'xml' ;
318324 } else if ( isText ( contentType ) ) {
@@ -346,6 +352,11 @@ const BodyGenerators = {
346352 body = 'files' ;
347353 }
348354
355+ if ( isXML ( contentType ) ) {
356+ // Convert JSON to XML if needed
357+ body = convertBodyToXML ( body ) ;
358+ }
359+
349360 return { body, code, headers } ;
350361 } ,
351362 getHTTPBody : ( body : any , headers ?: Record < string , string > ) => {
@@ -358,23 +369,48 @@ const BodyGenerators = {
358369 formUrlEncoded : ( ) => {
359370 const encoded = isPlainObject ( body )
360371 ? Object . entries ( body )
361- . map ( ( [ key , value ] ) => `${ key } =${ String ( value ) } ` )
372+ . map ( ( [ key , value ] ) => `${ key } =${ stringifyOpenAPI ( value ) } ` )
362373 . join ( '&' )
363- : String ( body ) ;
364- return `"${ encoded } "` ;
374+ : stringifyOpenAPI ( body ) ;
375+ return `"${ encoded . replace ( / " / g , "'" ) } "` ;
365376 } ,
366377 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, '' ) } "` ,
368383 default : ( ) => `${ stringifyOpenAPI ( body , null , 2 ) } ` ,
369384 } ;
370385
371386 if ( isPDF ( contentType ) ) return typeHandlers . pdf ( ) ;
372387 if ( isFormUrlEncoded ( contentType ) ) return typeHandlers . formUrlEncoded ( ) ;
373388 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 ( ) ;
377391
378392 return typeHandlers . default ( ) ;
379393 } ,
380394} ;
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